| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using GreenTree.Nachtragsmanagement.Core.Domain.Appendix;
- using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
- using GreenTree.Nachtragsmanagement.Services.Appendix;
- using GreenTree.Nachtragsmanagement.Services.Deviation;
- using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
- using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
- 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 DeviationController : Controller
- {
- private readonly IDeviationService _deviationService;
- private readonly IAppendixService _appendixService;
- public DeviationController(
- IDeviationService deviationService,
- IAppendixService appendixService)
- {
- _deviationService = deviationService;
- ViewData["AllAppendices"] = _appendixService.GetAllAppendices();
- ViewData["AllStatuses"] = _deviationService.GetAllStatuses();
- ViewData["AllDisturbances"] = _deviationService.GetAllDisturbances();
- ViewData["AllKinds"] = _deviationService.GetAllKinds();
- }
- /// <summary>
- /// Basic deviation view function
- /// </summary>
- [FunctionAuthorize(true, "Deviation-Deviations")]
- public ActionResult ViewDeviations()
- {
- var deviations = _deviationService.GetAllDeviations();
- var deviationModels = deviations
- .Select(u => DeviationDataModel.FromDeviation(u, false))
- .ToList();
- return View("~/Views/Admin/Deviations/View.cshtml", deviationModels);
- }
- /// <summary>
- /// Get JSON data of specific deviation
- /// </summary>
- /// <param name="id">Deviation id.</param>
- public ActionResult GetDeviation(int id = -1)
- {
- var deviation = _deviationService.GetDeviationById(id);
- if (deviation == null)
- return new JsonResult
- {
- Data = "notFound",
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- var deviationModel = DeviationDataModel.FromDeviation(deviation, false);
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(deviationModel),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- /// <summary>
- /// Callback result for deviation grid
- /// </summary>
- public ActionResult PartialDeviations()
- {
- var deviations = _deviationService.GetAllDeviations();
- var deviationModels = deviations
- .Select(u => DeviationDataModel.FromDeviation(u, false))
- .ToList();
- return PartialView("~/Views/Admin/Deviations/_DeviationGridPartial.cshtml", deviationModels);
- }
- /// <summary>
- /// Partial edit for editing of existing or for new deviation
- /// </summary>
- /// <param name="id">Id for existing deviation, otherweise -1.</param>
- public ActionResult EditDeviation(int id = -1)
- {
- var deviation = _deviationService.GetDeviationById(id);
- var deviationModel = DeviationDataModel.FromDeviation(deviation, true);
- return PartialView("~/Views/Admin/Deviations/_DeviationEditPartial.cshtml", deviationModel);
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="deviationModel">Deviation model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditDeviation(DeviationDataModel deviationModel)
- {
- if (!ModelState.IsValid)
- {
- //deviationModel.AppendixDescription = ((IList<Appendix>)ViewData["AllAppendices"])
- // .First(d => d.Id == deviationModel.AppendixId).CustomNumber;
- //deviationModel.StatusDescription = ((IList<Status>)ViewData["AllStatuses"])
- // .First(d => d.Id == deviationModel.StatusId).Description;
- //deviationModel.DisturbanceDescription = ((IList<Disturbance>)ViewData["AllDisturbances"])
- // .First(d => d.Id == deviationModel.DisturbanceId).Description;
- //deviationModel.KindDescription = ((IList<Kind>)ViewData["AllKinds"])
- // .First(d => d.Id == deviationModel.KindId).Description;
- return PartialView("~/Views/Admin/Deviations/_DeviationEditPartial.cshtml", deviationModel);
- }
- if (deviationModel.Id == -1)
- {
- var deviation = deviationModel.ToDeviation();
- _deviationService.InsertDeviation(deviation);
- }
- else
- {
- var deviation = _deviationService.GetDeviationById(deviationModel.Id);
- deviation.CustomNumber = deviationModel.CustomNumber;
- deviation.ReceiptDate = deviationModel.ReceiptDate;
- deviation.Value = deviationModel.Value;
- deviation.AppendixId = deviationModel.AppendixId;
- deviation.StatusId = deviationModel.StatusId;
- deviation.DisturbanceId = deviationModel.DisturbanceId;
- deviation.KindId = deviationModel.KindId;
- deviation.Comment = deviationModel.Comment;
- _deviationService.UpdateDeviation(deviation);
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- /// <summary>
- /// Simple JSON result for deleting a specific deviation
- /// </summary>
- /// <param name="id">Deviation id.</param>
- [HttpPost]
- public ActionResult DeleteDeviation(int id)
- {
- var deviation = _deviationService.GetDeviationById(id);
- if (deviation != null)
- _deviationService.DeleteDeviation(deviation);
- return new JsonResult
- {
- Data = "success"
- };
- }
- }
- }
|