AppendixController.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using GreenTree.Nachtragsmanagement.Services.Appendix;
  2. using GreenTree.Nachtragsmanagement.Services.Deviation;
  3. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  4. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  12. {
  13. public class AppendixController : Controller
  14. {
  15. private readonly IDeviationService _deviationSerivce;
  16. private readonly IAppendixService _appendixService;
  17. public AppendixController(
  18. IDeviationService deviationService,
  19. IAppendixService appendixService)
  20. {
  21. _deviationSerivce = deviationService;
  22. _appendixService = appendixService;
  23. ViewData["AllDeviations"] = _deviationSerivce.GetAllDeviations();
  24. ViewData["AllCategories"] = _appendixService.GetAllCategories();
  25. }
  26. #region Appendices
  27. /// <summary>
  28. /// Basic appendix view function
  29. /// </summary>
  30. [FunctionAuthorize(true, "Appendix-Appendices")]
  31. public ActionResult ViewAppendices()
  32. {
  33. var appendices = _appendixService.GetAllAppendices();
  34. var appendixModels = appendices
  35. .Select(u => AppendixDataModel.FromAppendix(u, false))
  36. .ToList();
  37. return View("~/Views/Appendices/View.cshtml", appendixModels);
  38. }
  39. /// <summary>
  40. /// Get JSON data of specific appendix
  41. /// </summary>
  42. /// <param name="id">Appendix id.</param>
  43. public ActionResult GetAppendix(int id = -1)
  44. {
  45. var appendix = _appendixService.GetAppendixById(id);
  46. if (appendix == null)
  47. return new JsonResult
  48. {
  49. Data = "notFound",
  50. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  51. };
  52. var appendixModel = AppendixDataModel.FromAppendix(appendix, false);
  53. return new JsonResult
  54. {
  55. Data = JsonConvert.SerializeObject(appendixModel),
  56. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  57. };
  58. }
  59. /// <summary>
  60. /// Callback result for appendix grid
  61. /// </summary>
  62. public ActionResult PartialAppendices()
  63. {
  64. var appendices = _appendixService.GetAllAppendices();
  65. var appendixModels = appendices
  66. .Select(u => AppendixDataModel.FromAppendix(u, false))
  67. .ToList();
  68. return PartialView("~/Views/Appendices/_AppendixGridPartial.cshtml", appendixModels);
  69. }
  70. /// <summary>
  71. /// Partial edit for editing of existing or for new appendix
  72. /// </summary>
  73. /// <param name="id">Id for existing appendix, otherweise -1.</param>
  74. public ActionResult EditAppendix(int id = -1)
  75. {
  76. var appendix = _appendixService.GetAppendixById(id);
  77. var appendixModel = AppendixDataModel.FromAppendix(appendix, true);
  78. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  79. }
  80. /// <summary>
  81. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  82. /// </summary>
  83. /// <param name="appendixModel">Appendix model to be saved.</param>
  84. [HttpPost, ValidateInput(false)]
  85. public ActionResult EditAppendix(AppendixDataModel appendixModel)
  86. {
  87. if (!ModelState.IsValid)
  88. return PartialView("~/Views/Appendices/_AppendixEditPartial.cshtml", appendixModel);
  89. if (appendixModel.Id == -1)
  90. {
  91. var appendix = appendixModel.ToAppendix();
  92. _appendixService.InsertAppendix(appendix);
  93. }
  94. else
  95. {
  96. var appendix = _appendixService.GetAppendixById(appendixModel.Id);
  97. appendix.CustomNumber = appendixModel.CustomNumber;
  98. appendix.Lot = appendixModel.Lot;
  99. appendix.Probability = appendixModel.Probability;
  100. appendix.OfferingNumber = appendixModel.OfferingNumber;
  101. appendix.OfferingDate = appendixModel.OfferingDate;
  102. appendix.NegotiationDate = appendixModel.NegotiationDate;
  103. appendix.NegotiationValue = appendixModel.NegotiationValue;
  104. appendix.ProtocolExists = appendixModel.ProtocolExists;
  105. appendix.OrderNumber = appendixModel.OrderNumber;
  106. appendix.OrderDate = appendixModel.OrderDate;
  107. appendix.Comment = appendixModel.Comment;
  108. appendix.SiteId = appendixModel.SiteId;
  109. _appendixService.UpdateAppendix(appendix);
  110. }
  111. return new JsonResult
  112. {
  113. Data = "success"
  114. };
  115. }
  116. /// <summary>
  117. /// Simple JSON result for deleting a specific appendix
  118. /// </summary>
  119. /// <param name="id">Appendix id.</param>
  120. [HttpPost]
  121. public ActionResult DeleteAppendix(int id)
  122. {
  123. var appendix = _appendixService.GetAppendixById(id);
  124. if (appendix != null)
  125. _appendixService.DeleteAppendix(appendix);
  126. return new JsonResult
  127. {
  128. Data = "success"
  129. };
  130. }
  131. #endregion
  132. #region Categories
  133. /// <summary>
  134. /// Basic category view function
  135. /// </summary>
  136. [FunctionAuthorize(true, "Appendix-Categories")]
  137. public ActionResult ViewCategories()
  138. {
  139. var categories = _appendixService.GetAllCategories();
  140. var categoryModels = categories
  141. .Select(u => CategoryDataModel.FromCategory(u, false))
  142. .ToList();
  143. return View("~/Views/Appendix/Categories.cshtml", categoryModels);
  144. }
  145. /// <summary>
  146. /// Get JSON data of specific category
  147. /// </summary>
  148. /// <param name="id">Category id.</param>
  149. public ActionResult GetCategory(int id = -1)
  150. {
  151. var category = _appendixService.GetCategoryById(id);
  152. if (category == null)
  153. return new JsonResult
  154. {
  155. Data = "notFound",
  156. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  157. };
  158. var categoryModel = CategoryDataModel.FromCategory(category, false);
  159. return new JsonResult
  160. {
  161. Data = JsonConvert.SerializeObject(categoryModel),
  162. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  163. };
  164. }
  165. /// <summary>
  166. /// Callback result for category grid
  167. /// </summary>
  168. public ActionResult PartialCategories()
  169. {
  170. var categories = _appendixService.GetAllCategories();
  171. var categoryModels = categories
  172. .Select(u => CategoryDataModel.FromCategory(u, false))
  173. .ToList();
  174. return PartialView("~/Views/Admin/Categories/_CategoryListPartial.cshtml", categoryModels);
  175. }
  176. /// <summary>
  177. /// Partial edit for editing of existing or for new category
  178. /// </summary>
  179. /// <param name="id">Id for existing category, otherweise -1.</param>
  180. public ActionResult EditCategory(int id = -1)
  181. {
  182. var category = _appendixService.GetCategoryById(id);
  183. var categoryModel = CategoryDataModel.FromCategory(category, true);
  184. return PartialView("~/Views/Admin/Categories/_CategoryEditPartial.cshtml", categoryModel);
  185. }
  186. /// <summary>
  187. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  188. /// </summary>
  189. /// <param name="categoryModel">Category model to be saved.</param>
  190. [HttpPost, ValidateInput(false)]
  191. public ActionResult EditCategory(CategoryDataModel categoryModel)
  192. {
  193. if (!ModelState.IsValid)
  194. return PartialView("~/Views/Deviations/_CategoryEditPartial.cshtml", categoryModel);
  195. if (categoryModel.Id == -1)
  196. {
  197. var claim = categoryModel.ToCategory();
  198. _appendixService.InsertCategory(claim);
  199. }
  200. else
  201. {
  202. var disturbance = _appendixService.GetCategoryById(categoryModel.Id);
  203. disturbance.Description = categoryModel.Description;
  204. _appendixService.UpdateCategory(disturbance);
  205. }
  206. return new JsonResult
  207. {
  208. Data = "success"
  209. };
  210. }
  211. /// <summary>
  212. /// Simple JSON result for deleting a specific category
  213. /// </summary>
  214. /// <param name="id">Category id.</param>
  215. [HttpPost]
  216. public ActionResult DeleteCategory(int id)
  217. {
  218. var category = _appendixService.GetCategoryById(id);
  219. if (category != null)
  220. _appendixService.DeleteCategory(category);
  221. return new JsonResult
  222. {
  223. Data = "success"
  224. };
  225. }
  226. #endregion
  227. }
  228. }