<commit_before>﻿using Microsoft.AspNet.Identity;
using SpaceBlog.Models;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using System;

namespace SpaceBlog.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class AdminController : Controller
    {

        private BlogDBContext db = new BlogDBContext();

        // GET: Admin/Index
        public ActionResult Index()
        {

            var userViewModels = db.Users
                .Select(UserToUserViewModel)
                .ToArray();

            return View(userViewModels);
        }

        // GET: Admin/Suspend/5
        public ActionResult Suspend(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = db.Users.Find(id);

            // The Administartor should not be able to suspend himself
            //if (user.Roles.Contains("Administrators"))
            //{
            //    return RedirectToAction("Index");
            //}

            return View(user);
        }

        // POST: Admin/Suspend/5
        [HttpPost, ActionName("Suspend")]
        public ActionResult SuspendConfirmed(string id)
        {
            var user = db.Users.Find(id);
            user.Suspended = true;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // GET: Admin/Activate/5
        public ActionResult Activate(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = db.Users.Find(id);

            return View(user);
        }

        // POST: Admin/Activate/5
        [HttpPost, ActionName("Activate")]
        public ActionResult ActivateConfirmed(string id)
        {
            var user = db.Users.Find(id);
            user.Suspended = false;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Details(string id)
        {
            var user = db.Users.Find(id);

            var userModel = new UserViewModel();

            var allArticles = db.Articles.Include(a => a.Author).ToList();
            var allComments = db.Comments.Include(a => a.Author).ToList();

            userModel.Id = user.Id;
            userModel.UserName = user.UserName;
            userModel.FullName = user.FullName;
            userModel.Articles = allArticles.Where(b => b.Author == user).ToList();
            userModel.Comments = allComments.Where(b => b.Author == user).ToList();
            userModel.DateRegistered = user.DateRegistered;

            return View(userModel);
        }

        private UserViewModel UserToUserViewModel(ApplicationUser user)
        {
            var db = new BlogDBContext();

            var userViewModel = new UserViewModel();

            userViewModel.Id = user.Id;
            userViewModel.UserName = user.UserName;
            userViewModel.FullName = user.FullName;
            userViewModel.ArticlesCreated = db.Articles.Include(a => a.Author).Count(a => a.Author.Id == user.Id);
            userViewModel.CommentsMade = db.Comments.Include(a => a.Author).Count(a => a.Author.Id == user.Id);
            userViewModel.DateRegistered = user.DateRegistered;

            var userRoles = user.Roles;
            
            //if (userRoles.Contains("Moderators"))
            //{
            //    userViewModel.UserType = "Moderator";
            //}<commit_msg>Another line had to be commented in order Users to work<commit_after>﻿using Microsoft.AspNet.Identity;
using SpaceBlog.Models;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using System;

namespace SpaceBlog.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class AdminController : Controller
    {

        private BlogDBContext db = new BlogDBContext();

        // GET: Admin/Index
        public ActionResult Index()
        {

            var userViewModels = db.Users
                .Select(UserToUserViewModel)
                .ToArray();

            return View(userViewModels);
        }

        // GET: Admin/Suspend/5
        public ActionResult Suspend(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = db.Users.Find(id);

            // The Administartor should not be able to suspend himself
            //if (user.Roles.Contains("Administrators"))
            //{
            //    return RedirectToAction("Index");
            //}

            return View(user);
        }

        // POST: Admin/Suspend/5
        [HttpPost, ActionName("Suspend")]
        public ActionResult SuspendConfirmed(string id)
        {
            var user = db.Users.Find(id);
            user.Suspended = true;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // GET: Admin/Activate/5
        public ActionResult Activate(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = db.Users.Find(id);

            return View(user);
        }

        // POST: Admin/Activate/5
        [HttpPost, ActionName("Activate")]
        public ActionResult ActivateConfirmed(string id)
        {
            var user = db.Users.Find(id);
            user.Suspended = false;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Details(string id)
        {
            var user = db.Users.Find(id);

            var userModel = new UserViewModel();

            var allArticles = db.Articles.Include(a => a.Author).ToList();
            var allComments = db.Comments.Include(a => a.Author).ToList();

            userModel.Id = user.Id;
            userModel.UserName = user.UserName;
            userModel.FullName = user.FullName;
            userModel.Articles = allArticles.Where(b => b.Author == user).ToList();
            userModel.Comments = allComments.Where(b => b.Author == user).ToList();
            userModel.DateRegistered = user.DateRegistered;

            return View(userModel);
        }

        private UserViewModel UserToUserViewModel(ApplicationUser user)
        {
            var db = new BlogDBContext();

            var userViewModel = new UserViewModel();

            userViewModel.Id = user.Id;
            userViewModel.UserName = user.UserName;
            userViewModel.FullName = user.FullName;
            userViewModel.ArticlesCreated = db.Articles.Include(a => a.Author).Count(a => a.Author.Id == user.Id);
            userViewModel.CommentsMade = db.Comments.Include(a => a.Author).Count(a => a.Author.Id == user.Id);
            userViewModel.DateRegistered = user.DateRegistered;

            //var userRoles = user.Roles;
            
            //if (userRoles.Contains("Moderators"))
            //{
            //    userViewModel.UserType = "Moderator";
            //}
{'fraction_non_alphanumeric': 0.10608983190961697, 'fraction_numerical': 0.0011022320198401765, 'mean_word_length': 1.7382119954734063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6031391', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1894, 'n_words': 398}