using GreenTree.Nachtragsmanagement.Services.Appendix; using GreenTree.Nachtragsmanagement.Services.Deviation; using GreenTree.Nachtragsmanagement.Web.Framework.Authorization; using GreenTree.Nachtragsmanagement.Web.Models.Appendix; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace GreenTree.Nachtragsmanagement.Web.Controllers { public class AppendixController : Controller { private readonly IDeviationService _deviationSerivce; private readonly IAppendixService _appendixService; public AppendixController( IDeviationService deviationService, IAppendixService appendixService) { _deviationSerivce = deviationService; _appendixService = appendixService; ViewData["AllDeviations"] = _deviationSerivce.GetAllDeviations(); ViewData["AllCategories"] = _appendixService.GetAllCategories(); } #region Appendices /// /// Basic appendix view function /// [FunctionAuthorize(true, "Appendix-Appendices")] public ActionResult ViewAppendices() { var appendices = _appendixService.GetAllAppendices(); var appendixModels = appendices .Select(u => AppendixDataModel.FromAppendix(u, false)) .ToList(); return View("~/Views/Appendices/View.cshtml", appendixModels); } /// /// Get JSON data of specific appendix /// /// Appendix id. public ActionResult GetAppendix(int id = -1) { var appendix = _appendixService.GetAppendixById(id); if (appendix == null) return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; var appendixModel = AppendixDataModel.FromAppendix(appendix, false); return new JsonResult { Data = JsonConvert.SerializeObject(appendixModel), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } /// /// Callback result for appendix grid /// public ActionResult PartialAppendices() { var appendices = _appendixService.GetAllAppendices(); var appendixModels = appendices .Select(u => AppendixDataModel.FromAppendix(u, false)) .ToList(); return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels); } /// /// Partial edit for editing of existing or for new appendix /// /// Id for existing appendix, otherweise -1. public ActionResult EditAppendix(int id = -1) { var appendix = _appendixService.GetAppendixById(id); var appendixModel = AppendixDataModel.FromAppendix(appendix, true); return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel); } /// /// Partial edit result if ModelState is valid, otherwise simple JSON result for success /// /// Appendix model to be saved. [HttpPost, ValidateInput(false)] public ActionResult EditAppendix(AppendixDataModel appendixModel) { if (!ModelState.IsValid) return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel); if (appendixModel.Id == -1) { var appendix = appendixModel.ToAppendix(); _appendixService.InsertAppendix(appendix); } else { var appendix = _appendixService.GetAppendixById(appendixModel.Id); appendix.CustomNumber = appendixModel.CustomNumber; appendix.Lot = appendixModel.Lot; appendix.Probability = appendixModel.Probability; appendix.OfferingNumber = appendixModel.OfferingNumber; appendix.OfferingDate = appendixModel.OfferingDate; appendix.NegotiationDate = appendixModel.NegotiationDate; appendix.NegotiationValue = appendixModel.NegotiationValue; appendix.ProtocolExists = appendixModel.ProtocolExists; appendix.OrderNumber = appendixModel.OrderNumber; appendix.OrderDate = appendixModel.OrderDate; appendix.Comment = appendixModel.Comment; appendix.SiteId = appendixModel.SiteId; _appendixService.UpdateAppendix(appendix); } return new JsonResult { Data = "success" }; } /// /// Simple JSON result for deleting a specific appendix /// /// Appendix id. [HttpPost] public ActionResult DeleteAppendix(int id) { var appendix = _appendixService.GetAppendixById(id); if (appendix != null) _appendixService.DeleteAppendix(appendix); return new JsonResult { Data = "success" }; } #endregion #region Categories /// /// Basic category view function /// [FunctionAuthorize(true, "Appendix-Categories")] public ActionResult ViewCategories() { var categories = _appendixService.GetAllCategories(); var categoryModels = categories .Select(u => CategoryDataModel.FromCategory(u, false)) .ToList(); return View("~/Views/Appendix/Categories.cshtml", categoryModels); } /// /// Get JSON data of specific category /// /// Category id. public ActionResult GetCategory(int id = -1) { var category = _appendixService.GetCategoryById(id); if (category == null) return new JsonResult { Data = "notFound", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; var categoryModel = CategoryDataModel.FromCategory(category, false); return new JsonResult { Data = JsonConvert.SerializeObject(categoryModel), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } /// /// Callback result for category grid /// public ActionResult PartialCategories() { var categories = _appendixService.GetAllCategories(); var categoryModels = categories .Select(u => CategoryDataModel.FromCategory(u, false)) .ToList(); return PartialView("~/Views/Admin/Categories/_CategoryListPartial.cshtml", categoryModels); } /// /// Partial edit for editing of existing or for new category /// /// Id for existing category, otherweise -1. public ActionResult EditCategory(int id = -1) { var category = _appendixService.GetCategoryById(id); var categoryModel = CategoryDataModel.FromCategory(category, true); return PartialView("~/Views/Admin/Categories/_CategoryEditPartial.cshtml", categoryModel); } /// /// Partial edit result if ModelState is valid, otherwise simple JSON result for success /// /// Category model to be saved. [HttpPost, ValidateInput(false)] public ActionResult EditCategory(CategoryDataModel categoryModel) { if (!ModelState.IsValid) return PartialView("~/Views/Deviations/_CategoryEditPartial.cshtml", categoryModel); if (categoryModel.Id == -1) { var claim = categoryModel.ToCategory(); _appendixService.InsertCategory(claim); } else { var disturbance = _appendixService.GetCategoryById(categoryModel.Id); disturbance.Description = categoryModel.Description; _appendixService.UpdateCategory(disturbance); } return new JsonResult { Data = "success" }; } /// /// Simple JSON result for deleting a specific category /// /// Category id. [HttpPost] public ActionResult DeleteCategory(int id) { var category = _appendixService.GetCategoryById(id); if (category != null) _appendixService.DeleteCategory(category); return new JsonResult { Data = "success" }; } #endregion } }