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();
}
///
/// Basic deviation view function
///
[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);
}
///
/// Get JSON data of specific deviation
///
/// Deviation id.
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
};
}
///
/// Callback result for deviation grid
///
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);
}
///
/// Partial edit for editing of existing or for new deviation
///
/// Id for existing deviation, otherweise -1.
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);
}
///
/// Partial edit result if ModelState is valid, otherwise simple JSON result for success
///
/// Deviation model to be saved.
[HttpPost, ValidateInput(false)]
public ActionResult EditDeviation(DeviationDataModel deviationModel)
{
if (!ModelState.IsValid)
{
//deviationModel.AppendixDescription = ((IList)ViewData["AllAppendices"])
// .First(d => d.Id == deviationModel.AppendixId).CustomNumber;
//deviationModel.StatusDescription = ((IList)ViewData["AllStatuses"])
// .First(d => d.Id == deviationModel.StatusId).Description;
//deviationModel.DisturbanceDescription = ((IList)ViewData["AllDisturbances"])
// .First(d => d.Id == deviationModel.DisturbanceId).Description;
//deviationModel.KindDescription = ((IList)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"
};
}
///
/// Simple JSON result for deleting a specific deviation
///
/// Deviation id.
[HttpPost]
public ActionResult DeleteDeviation(int id)
{
var deviation = _deviationService.GetDeviationById(id);
if (deviation != null)
_deviationService.DeleteDeviation(deviation);
return new JsonResult
{
Data = "success"
};
}
}
}