CraftController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using GreenTree.Strohrmann.ERP.Core.Domain.Business;
  7. using GreenTree.Strohrmann.ERP.Core.Helper;
  8. using GreenTree.Strohrmann.ERP.Domain.Model;
  9. using GreenTree.Strohrmann.ERP.Services.Geolocator;
  10. using GreenTree.Strohrmann.ERP.Web.Extension;
  11. using GreenTree.Strohrmann.ERP.Web.Models.Business;
  12. using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.AspNetCore.Mvc;
  15. namespace GreenTree.Strohrmann.ERP.Web.Controllers
  16. {
  17. public class CraftController : Controller
  18. {
  19. #region DI fields
  20. // The global DbContext
  21. private readonly ERPDbContext _eRPDbContext;
  22. #endregion
  23. #region Ctor
  24. /// <summary>
  25. /// Initializes a new instance of the CraftController class
  26. /// </summary>
  27. /// <param name="eRPDbContext">Global DbContext.</param>
  28. public CraftController(ERPDbContext eRPDbContext)
  29. {
  30. _eRPDbContext = eRPDbContext;
  31. }
  32. #endregion
  33. #region Actions
  34. // GET: CraftController
  35. public ActionResult Index()
  36. {
  37. var crafts = _eRPDbContext.Crafts
  38. .ToList()
  39. .Select(c => new CraftModel(c));
  40. return View(crafts);
  41. }
  42. // GET: CraftController/Details/5
  43. public ActionResult Details(int id)
  44. {
  45. var craft = _eRPDbContext.Crafts
  46. .FirstOrDefault(c => c.Id == id);
  47. var craftModel = new CraftModel(craft);
  48. return View(craftModel);
  49. }
  50. // GET: CraftController/Create
  51. public ActionResult Create()
  52. {
  53. ViewData.AddSelectList("AvailableEmployees",
  54. _eRPDbContext.Employees,
  55. e => e.Id, e => String.Format("{0} {1}", e.Firstname, e.Lastname));
  56. return View();
  57. }
  58. // POST: CraftController/Create
  59. [HttpPost]
  60. [ValidateAntiForgeryToken]
  61. public ActionResult Create(CraftModel model)
  62. {
  63. if (!ModelState.IsValid)
  64. {
  65. ViewData.AddSelectList("AvailableEmployees",
  66. _eRPDbContext.Employees,
  67. e => e.Id, e => String.Format("{0} {1}", e.Firstname, e.Lastname));
  68. return View(model);
  69. }
  70. //var craft = new Craft
  71. //{
  72. // CreatedBy = User.Identity.Name,
  73. // CreatedOn = DateTime.Now
  74. //};
  75. //_eRPDbContext.Crafts.Add(craft);
  76. //_eRPDbContext.SaveChanges();
  77. return RedirectToAction(nameof(Index));
  78. }
  79. // GET: CraftController/AddCraftEmployeePartial
  80. public ActionResult AddCraftEmployeePartial(CraftEmployeeModel craftEmployeeModel)
  81. {
  82. return PartialView("~/Views/Craft/_CraftEmployeePartial.cshtml", craftEmployeeModel);
  83. }
  84. // GET: CraftController/Edit/5
  85. public ActionResult Edit(int id)
  86. {
  87. ViewData.AddSelectList("AvailableEmployees",
  88. _eRPDbContext.Employees,
  89. e => e.Id, e => String.Format("{0} {1}", e.Firstname, e.Lastname));
  90. var craft = _eRPDbContext.Crafts
  91. .FirstOrDefault(c => c.Id == id);
  92. var craftModel = new CraftModel(craft);
  93. return View(craftModel);
  94. }
  95. // POST: CraftController/Edit/5
  96. [HttpPost]
  97. [ValidateAntiForgeryToken]
  98. public ActionResult Edit(int id, CraftModel model)
  99. {
  100. if (!ModelState.IsValid)
  101. {
  102. ViewData.AddSelectList("AvailableEmployees",
  103. _eRPDbContext.Employees,
  104. e => e.Id, e => String.Format("{0} {1}", e.Firstname, e.Lastname));
  105. return View(model);
  106. }
  107. var craft = _eRPDbContext.Crafts
  108. .FirstOrDefault(u => u.Id == id);
  109. craft.ChangedBy = User.Identity.Name;
  110. craft.ChangedOn = DateTime.Now;
  111. _eRPDbContext.SaveChanges();
  112. return RedirectToAction(nameof(Index));
  113. }
  114. // POST: CraftController/Delete/5
  115. [HttpPost]
  116. [ValidateAntiForgeryToken]
  117. public ActionResult Delete(int id, IFormCollection collection)
  118. {
  119. var craft = _eRPDbContext.Crafts
  120. .FirstOrDefault(c => c.Id == id);
  121. _eRPDbContext.Crafts.Remove(craft);
  122. _eRPDbContext.SaveChanges();
  123. return RedirectToAction(nameof(Index));
  124. }
  125. #endregion
  126. }
  127. }