using InventoryManager.Data; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System; using InventoryManager.Models; using System.Linq; using Microsoft.EntityFrameworkCore; using InventoryManager.Interfaces; using InventoryManager.Tools; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.Rendering; using InventoryManager.Repositories; namespace InventoryManager.Controllers { [Authorize] public class ProductsController : Controller { private readonly IProducts _repo; private readonly IUnit _unitRepo; private readonly IMaterials _materialsRepo; private readonly ICollections _collectionsRepo; private readonly IColors _colorsRepo; public ProductsController(IProducts repo, IUnit unitRepo, IMaterials materialsRepo, ICollections collectionsRepo,IColors colorsRepo) { _repo = repo; _unitRepo = unitRepo; _materialsRepo = materialsRepo; _collectionsRepo = collectionsRepo; _colorsRepo = colorsRepo; } public IActionResult Index(string sortExpression = "", string SearchText = "",int pg = 1, int pageSize = 5) { SortModel sortModel = new SortModel(); sortModel.AddColumn("name"); sortModel.AddColumn("description"); sortModel.AddColumn("color"); sortModel.AddColumn("qty"); sortModel.AddColumn("unit"); sortModel.AddColumn("collection"); sortModel.AddColumn("material"); sortModel.ApplySort(sortExpression); ViewData["sortModel"] = sortModel; ViewBag.SearchText = SearchText; PaginatedList<Products> items = _repo.GetItems(sortModel.SortedProperty, sortModel.SortedOrder, SearchText, pg, pageSize); var pager = new PagerModel(items.TotalRecords, pg, pageSize); pager.SortExpression = sortExpression; this.ViewBag.Pager = pager; TempData["CurrentPage"] = pg; return View(items); } public IActionResult Create() { Products item = new Products(); ViewBag.Units = GetUnitList(); ViewBag.Collections = GetCollectionList(); ViewBag.Materials = GetMaterialList(); ViewBag.Colors = GetColorList(); return View(item); } [HttpPost] public IActionResult Create(Products item) { bool bolret = false; string errMessage = ""; try { if (item.Description.Length < 4 || item.Description == null) errMessage = "Description Must be at least 4 Characters"; if (_repo.IsItemUnique(item.Name) == true) errMessage += " Sorry I Already Exist"; if (errMessage == "") { item = _repo.Create(item); bolret = true; } } catch(Exception ex) { errMessage += " " + ex.Message; } if (bolret == false) { TempData["ErrorMessage"] = errMessage; ModelState.AddModelError("", errMessage); return View(item); } else { TempData["SuccessMessage"] = item.Name + " Created Successfully"; return RedirectToAction(nameof(Index)); } } public IActionResult Details(int id) { Products item = _repo.GetItem(id); ViewBag.Units = GetUnitList(); ViewBag.Collections = GetCollectionList(); ViewBag.Materials = GetMaterialList(); ViewBag.Colors = GetColorList(); return View(item); } [HttpGet] public IActionResult Edit(int id) { Products item = _repo.GetItem(id); ViewBag.Units = GetUnitList(); ViewBag.Collections = GetCollectionList(); ViewBag.Materials = GetMaterialList(); ViewBag.Colors = GetColorList(); TempData.Keep("CurrentPage"); return View(item); } [HttpPost] public IActionResult Edit(Products item) { bool bolret = false; string errMessage = ""; try { if(item.Description.Length < 4 || item.Description == null) errMessage = "Description Must be at least 4 Characters"; if (_repo.IsItemUnique(item.Name, item.Id) == true) errMessage = errMessage + "Sorry I Already Exist"; if (errMessage == "") { item = _repo.Edit(item); TempData["SuccessMessage"] = item.Name + " Updated Successfully"; bolret = true; } } catch(Exception ex) { errMessage = errMessage = " " + ex.Message; } int currentPage = 1; if (TempData["CurrentPage"] != null) currentPage = (int)TempData["CurrentPage"]; if(bolret == false) { TempData["ErrorMessage"] = errMessage; ModelState.AddModelError("", errMessage); return View(item); } else { TempData["SuccessMessage"] = item.Name + " Saved Successfully"; return RedirectToAction(nameof(Index), new { pg = currentPage }); } } [HttpGet] public IActionResult Delete(int id) { Products item = _repo.GetItem(id); ViewBag.Units = GetUnitList(); ViewBag.Collections = GetCollectionList(); ViewBag.Materials = GetMaterialList(); ViewBag.Colors = GetColorList(); TempData.Keep("CurrentPage"); return View(item); } [HttpPost] public IActionResult Delete(Products item) { try { item = _repo.Delete(item); } catch(Exception ex) { string errMessage = ex.Message; TempData["ErrorMessage"] = errMessage; ModelState.AddModelError("", errMessage); return View(item); } int currentPage = 1; if (TempData["CurrentPage"] != null) currentPage = (int)TempData["CurrentPage"]; TempData["SuccessMessage"] = item.Name + " Deleted Successfully"; return RedirectToAction(nameof(Index), new { pg = currentPage }); } private List<SelectListItem>GetUnitList() { var list = new List<SelectListItem>(); PaginatedList<Unit> units = _unitRepo.GetItems("Name", SortOrder.Ascending, "", 1, 100); list = units.Select(u => new SelectListItem() { Value = u.Id.ToString(), Text = u.Name }).ToList(); var define = new SelectListItem() { Value = "", Text = " Select Unit" }; list.Insert(0,define); return list; } private List<SelectListItem> GetMaterialList() { var list = new List<SelectListItem>(); PaginatedList<Materials> materials = _materialsRepo.GetItems("Name", SortOrder.Ascending,"", 1, 100); list = materials.Select(u => new SelectListItem() { Value = u.Id.ToString(), Text = u.Name }).ToList(); var define = new SelectListItem() { Value = "", Text = " Select Material" }; list.Insert(0, define); return list; } private List<SelectListItem> GetCollectionList() { var list = new List<SelectListItem>(); PaginatedList<Collections> collections = _collectionsRepo.GetItems("Name", SortOrder.Ascending, "", 1, 100); list = collections.Select(u => new SelectListItem() { Value = u.Id.ToString(), Text = u.Name }).ToList(); var define = new SelectListItem() { Value = "", Text = " Select Collection" }; list.Insert(0, define); return list; } private List<SelectListItem> GetColorList() { var list = new List<SelectListItem>(); PaginatedList<Colors> colors = _colorsRepo.GetItems("Name", SortOrder.Ascending, "", 1, 100); list = colors.Select(u => new SelectListItem() { Value = u.Id.ToString(), Text = u.Name }).ToList(); var define = new SelectListItem() { Value = "", Text = " Select Color" }; list.Insert(0, define); return list; } } }