CustomerController.cs 4.5 KB

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