AppendixController.cs 9.1 KB

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