SupplierController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Models.Business;
  11. using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. namespace GreenTree.Strohrmann.ERP.Web.Controllers
  15. {
  16. public class SupplierController : Controller
  17. {
  18. #region DI fields
  19. // The global DbContext
  20. private readonly ERPDbContext _eRPDbContext;
  21. #endregion
  22. #region Ctor
  23. /// <summary>
  24. /// Initializes a new instance of the SupplierController class
  25. /// </summary>
  26. /// <param name="eRPDbContext">Global DbContext.</param>
  27. public SupplierController(ERPDbContext eRPDbContext)
  28. {
  29. _eRPDbContext = eRPDbContext;
  30. }
  31. #endregion
  32. #region Actions
  33. // GET: SupplierController
  34. public ActionResult Index()
  35. {
  36. var suppliers = _eRPDbContext.Suppliers
  37. .ToList()
  38. .Select(c => new SupplierModel(c));
  39. return View(suppliers);
  40. }
  41. // GET: SupplierController/Details/5
  42. public ActionResult Details(int id)
  43. {
  44. var supplier = _eRPDbContext.Suppliers
  45. .FirstOrDefault(c => c.Id == id);
  46. var supplierModel = new SupplierModel(supplier);
  47. return View(supplierModel);
  48. }
  49. // GET: SupplierController/Create
  50. public ActionResult Create()
  51. {
  52. return View();
  53. }
  54. // POST: SupplierController/Create
  55. [HttpPost]
  56. [ValidateAntiForgeryToken]
  57. public ActionResult Create(SupplierModel model)
  58. {
  59. if (!ModelState.IsValid)
  60. return View(model);
  61. var supplier = new Supplier
  62. {
  63. Name = model.Name,
  64. Description = model.Description,
  65. Address = model.Address,
  66. Town = model.Town,
  67. ZipCode = model.ZipCode,
  68. Country = model.Country,
  69. PhoneFirst = model.PhoneFirst,
  70. PhoneSecond = model.PhoneSecond,
  71. MailFirst = model.MailFirst,
  72. MailSecond = model.MailSecond,
  73. Comment = model.Comment,
  74. CreatedBy = User.Identity.Name,
  75. CreatedOn = DateTime.Now
  76. };
  77. _eRPDbContext.Suppliers.Add(supplier);
  78. _eRPDbContext.SaveChanges();
  79. return RedirectToAction(nameof(Index));
  80. }
  81. // GET: SupplierController/Edit/5
  82. public ActionResult Edit(int id)
  83. {
  84. var supplier = _eRPDbContext.Suppliers
  85. .FirstOrDefault(c => c.Id == id);
  86. var supplierModel = new SupplierModel(supplier);
  87. return View(supplierModel);
  88. }
  89. // POST: SupplierController/Edit/5
  90. [HttpPost]
  91. [ValidateAntiForgeryToken]
  92. public ActionResult Edit(int id, SupplierModel model)
  93. {
  94. if (!ModelState.IsValid)
  95. return View(model);
  96. var supplier = _eRPDbContext.Suppliers
  97. .FirstOrDefault(u => u.Id == id);
  98. supplier.Name = model.Name;
  99. supplier.Description = model.Description;
  100. supplier.Address = model.Address;
  101. supplier.Town = model.Town;
  102. supplier.ZipCode = model.ZipCode;
  103. supplier.Country = model.Country;
  104. supplier.PhoneFirst = model.PhoneFirst;
  105. supplier.PhoneSecond = model.PhoneSecond;
  106. supplier.MailFirst = model.MailFirst;
  107. supplier.MailSecond = model.MailSecond;
  108. supplier.Comment = model.Comment;
  109. supplier.ChangedBy = User.Identity.Name;
  110. supplier.ChangedOn = DateTime.Now;
  111. _eRPDbContext.SaveChanges();
  112. return RedirectToAction(nameof(Index));
  113. }
  114. // POST: SupplierController/Delete/5
  115. [HttpPost]
  116. [ValidateAntiForgeryToken]
  117. public ActionResult Delete(int id, IFormCollection collection)
  118. {
  119. var supplier = _eRPDbContext.Suppliers
  120. .FirstOrDefault(c => c.Id == id);
  121. _eRPDbContext.Suppliers.Remove(supplier);
  122. _eRPDbContext.SaveChanges();
  123. return RedirectToAction(nameof(Index));
  124. }
  125. #endregion
  126. }
  127. }