using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using rgbc_sds.services.Models; using rgbc_sds.services.Services; namespace rgbc_sds.website.Controllers.Suppliers { public class SupplierController : Controller { #region [ Variables ] private readonly SupplierService _supplierService; #endregion [ Variables ] #region [ Constructor ] public SupplierController(SupplierService supplierService) { _supplierService = supplierService; } #endregion [ Constructor ] #region [ Get ] [HttpGet] [Route("supplier/supplierlist")] public ActionResult SupplierList() { var model = _supplierService.GetSupplierList(); // populate supplier statuses model = PopulateSupplierStatus(model); model.LoggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); model.LoggedInUserName = HttpContext.Session.GetString("LoggedInUsername"); return View("~/Views/Maintenance/Suppliers/SupplierList.cshtml", model); } [HttpGet] [Route("supplier/filtersupplierlist")] public ActionResult FilterSupplierList(SupplierModel model) { var returnModel = _supplierService.FilterSupplierList(model.SearchString, model.ActiveStatus); // populate supplier statuses returnModel = PopulateSupplierStatus(returnModel); returnModel.LoggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); returnModel.LoggedInUserName = HttpContext.Session.GetString("LoggedInUsername"); return View("~/Views/Maintenance/Suppliers/SupplierList.cshtml", returnModel); } [HttpGet] [Route("supplier/getsuppliertoupdate/{id}")] public ActionResult GetSupplierToUpdate(int id) { var model = new SupplierViewModel(); var supplier = _supplierService.GetSupplier(id); if (supplier != null) { model.Process = "EDIT"; model.Id = supplier.Id; model.Name = supplier.Name; model.isActive = (bool)supplier.IsActive; } model.LoggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); model.LoggedInUserName = HttpContext.Session.GetString("LoggedInUsername"); return View("~/Views/Maintenance/Suppliers/UpdateSupplier.cshtml", model); } [HttpGet] [Route("supplier/addsupplier")] public IActionResult AddSupplier() { var model = new SupplierViewModel(); model.Process = "ADD"; model.LoggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); model.LoggedInUserName = HttpContext.Session.GetString("LoggedInUsername"); return View("~/Views/Maintenance/Suppliers/AddSupplier.cshtml", model); } #endregion [ Get ] #region [ Post ] [HttpPost] [Route("supplier/savesupplier")] public ActionResult SaveSupplier(SupplierViewModel model) { if (model.Name.IsNullOrEmpty()) { TempData["ErrorMessage"] = "Please enter a valid Supplier Name."; return View("~/Views/Maintenance/Suppliers/AddSupplier.cshtml", model); } var supplierModel = new SupplierModel(); supplierModel.Name = model.Name; supplierModel.CreatedBy = HttpContext.Session.GetString("LoggedInUsername"); supplierModel.CreatedAt = DateTime.Now; _supplierService.AddSupplier(supplierModel); TempData["SuccessMessage"] = "Supplier Created Successfully"; return RedirectToAction("SupplierList", "Supplier"); } [HttpPost] [Route("supplier/updatesupplier")] public IActionResult UpdateSupplier(SupplierViewModel model) { if (model.Name.IsNullOrEmpty()) { TempData["ErrorMessage"] = "Please enter a valid Supplier Name"; return View("~/Views/Maintenance/Suppliers/UpdateSupplier.cshtml", model); } var newSupplierModel = new SupplierModel(); newSupplierModel.Id = model.Id; newSupplierModel.Name = model.Name; newSupplierModel.IsActive = model.isActive; newSupplierModel.UpdatedBy = HttpContext.Session.GetString("LoggedInUsername"); newSupplierModel.UpdatedAt = DateTime.Now; var result = _supplierService.UpdateSupplier(newSupplierModel); TempData["SuccessMessage"] = "Supplier Updated Successfully."; return RedirectToAction("SupplierList", "Supplier"); } [HttpPost] [Route("supplier/deletesupplier/{id}")] public IActionResult DeleteSupplier(int id) { var loggedInUserName = HttpContext.Session.GetString("LoggedInUsername"); var result = _supplierService.DeleteSupplier(id, loggedInUserName); TempData["SuccessMessage"] = "Supplier Deleted Successfully."; return RedirectToAction("SupplierList", "Supplier"); } #endregion [ Post ] #region [ Helper Methods ] public SupplierModel PopulateSupplierStatus(SupplierModel model) { // create list of ports to be selected model.SupplierActiveList.Add(new SupplierActiveModel { Id = 1, Active = "Active" }); model.SupplierActiveList.Add(new SupplierActiveModel { Id = 2, Active = "Inactive" }); return model; } #endregion [ Helper Methods ] } }