<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NinjaHive.Contract.Models;
using NinjaHive.Contract.Queries;
using NinjaHive.Core;
using NinjaHive.WebApp.Helpers;
using NinjaHive.WebApp.Models;

namespace NinjaHive.WebApp.Controllers
{
    [Authorize]
    public class ItemsController : BaseController
    {
        private readonly IQueryProcessor queryProcessor;
        private readonly IWriteOnlyRepository<GameItemModel> repository; 

        public ItemsController(IQueryProcessor queryProcessor,
            IWriteOnlyRepository<GameItemModel> repository)
        {
            this.queryProcessor = queryProcessor;
            this.repository = repository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            var model = new GameItemModel();
            var viewModel = this.PrepareViewModel(model);
            return View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(GameItemViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                this.repository.Create(viewModel.GameItem);
                return base.Home();
            }

            viewModel.mainCategories = this.GetMainCategories();
            viewModel.categories = this.GetCategories();
            return View(viewModel);
        }

        public ActionResult Edit(Guid id)
        {
            var query = new GetEntityByIdQuery<GameItemModel>(id);
            var model = this.queryProcessor.Execute(query);
            var viewModel = this.PrepareViewModel(model);
            return this.View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(GameItemViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                this.repository.Update(viewModel.GameItem);
                return base.Home();
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(Guid id)
        {
            this.repository.Delete(id);
            return base.Home();
        }

        public ActionResult GetGameItems()
        {
            var query = new GetAllGameItemsQuery();
            var gameItems = this.queryProcessor.Execute(query);

            if (gameItems.Any())
            {
                return this.PartialView(Partials.GameItems, gameItems);
            }

            return base.NoResults();
        }

        private GameItemViewModel PrepareViewModel(GameItemModel model)
        {
            var mainCategories = this.GetMainCategories();
            var firstCategory = mainCategories.First();
            
            return new GameItemViewModel
            {
                mainCategories = mainCategories,
                categories = firstCategory.SubCategories,
                GameItem = model,
            };
        }

        private IEnumerable<MainCategoryModel> GetMainCategories()
        {
            return this.queryProcessor.Execute(new GetAllCategoriesQuery())
                .Where(c => c.SubCategories.Count() > 0);
        }

        private IEnumerable<SubCategoryModel> GetCategories()
        {
            return this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                      .SelectMany(c => c.SubCategories);
        }

        [HttpGet]
        public JsonResult GetMainCategory(string main)
        {
            var category = this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                .First(c => c.Name.Equals(main));
            return Json(category, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetSubCategories(Guid parent)
        {
            var subCategories = this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                      .First(c => c.Id == parent)
                                      .SubCategories;<commit_msg>Fixed an error that occurred if no categories exist.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NinjaHive.Contract.Models;
using NinjaHive.Contract.Queries;
using NinjaHive.Core;
using NinjaHive.WebApp.Helpers;
using NinjaHive.WebApp.Models;

namespace NinjaHive.WebApp.Controllers
{
    [Authorize]
    public class ItemsController : BaseController
    {
        private readonly IQueryProcessor queryProcessor;
        private readonly IWriteOnlyRepository<GameItemModel> repository; 

        public ItemsController(IQueryProcessor queryProcessor,
            IWriteOnlyRepository<GameItemModel> repository)
        {
            this.queryProcessor = queryProcessor;
            this.repository = repository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            var model = new GameItemModel();
            var viewModel = this.PrepareViewModel(model);
            return View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(GameItemViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                this.repository.Create(viewModel.GameItem);
                return base.Home();
            }

            viewModel.mainCategories = this.GetMainCategories();
            viewModel.categories = this.GetCategories();
            return View(viewModel);
        }

        public ActionResult Edit(Guid id)
        {
            var query = new GetEntityByIdQuery<GameItemModel>(id);
            var model = this.queryProcessor.Execute(query);
            var viewModel = this.PrepareViewModel(model);
            return this.View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(GameItemViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                this.repository.Update(viewModel.GameItem);
                return base.Home();
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(Guid id)
        {
            this.repository.Delete(id);
            return base.Home();
        }

        public ActionResult GetGameItems()
        {
            var query = new GetAllGameItemsQuery();
            var gameItems = this.queryProcessor.Execute(query);

            if (gameItems.Any())
            {
                return this.PartialView(Partials.GameItems, gameItems);
            }

            return base.NoResults();
        }

        private GameItemViewModel PrepareViewModel(GameItemModel model)
        {
            var mainCategories = this.GetMainCategories();
            var firstCategory = mainCategories.Count() > 0 ? mainCategories.First().SubCategories : null;
            
            return new GameItemViewModel
            {
                mainCategories = mainCategories,
                categories = firstCategory,
                GameItem = model,
            };
        }

        private IEnumerable<MainCategoryModel> GetMainCategories()
        {
            return this.queryProcessor.Execute(new GetAllCategoriesQuery())
                .Where(c => c.SubCategories.Count() > 0);
        }

        private IEnumerable<SubCategoryModel> GetCategories()
        {
            return this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                      .SelectMany(c => c.SubCategories);
        }

        [HttpGet]
        public JsonResult GetMainCategory(string main)
        {
            var category = this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                .First(c => c.Name.Equals(main));
            return Json(category, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetSubCategories(Guid parent)
        {
            var subCategories = this.queryProcessor.Execute(new GetAllCategoriesQuery())
                                      .First(c => c.Id == parent)
                                      .SubCategories;
{'fraction_non_alphanumeric': 0.07922809540932518, 'fraction_numerical': 0.0003595828838547285, 'mean_word_length': 1.7510715463237718, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884154', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1953, 'n_words': 398}