CustomerController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 CustomerController : 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 CustomerController class
  25. /// </summary>
  26. /// <param name="eRPDbContext">Global DbContext.</param>
  27. public CustomerController(ERPDbContext eRPDbContext)
  28. {
  29. _eRPDbContext = eRPDbContext;
  30. }
  31. #endregion
  32. #region Actions
  33. // GET: CustomerController
  34. public ActionResult Index()
  35. {
  36. var customers = _eRPDbContext.Customers
  37. .ToList()
  38. .Select(c => new CustomerModel(c));
  39. return View(customers);
  40. }
  41. // GET: CustomerController/Details/5
  42. public ActionResult Details(int id)
  43. {
  44. var customer = _eRPDbContext.Customers
  45. .FirstOrDefault(c => c.Id == id);
  46. var customerModel = new CustomerModel(customer);
  47. return View(customerModel);
  48. }
  49. // GET: CustomerController/Create
  50. public ActionResult Create()
  51. {
  52. return View();
  53. }
  54. // POST: CustomerController/Create
  55. [HttpPost]
  56. [ValidateAntiForgeryToken]
  57. public ActionResult Create(CustomerModel model)
  58. {
  59. if (!ModelState.IsValid)
  60. return View(model);
  61. var customer = new Customer
  62. {
  63. Firstname = model.Firstname,
  64. Lastname = model.Lastname,
  65. CompanyName = model.CompanyName,
  66. Address = model.Address,
  67. Town = model.Town,
  68. ZipCode = model.ZipCode,
  69. Country = model.Country,
  70. IsBusiness = model.IsBusiness,
  71. CreatedBy = User.Identity.Name,
  72. CreatedOn = DateTime.Now
  73. };
  74. _eRPDbContext.Customers.Add(customer);
  75. _eRPDbContext.SaveChanges();
  76. return RedirectToAction(nameof(Index));
  77. }
  78. // GET: CustomerController/Edit/5
  79. public ActionResult Edit(int id)
  80. {
  81. var customer = _eRPDbContext.Customers
  82. .FirstOrDefault(c => c.Id == id);
  83. var customerModel = new CustomerModel(customer);
  84. return View(customerModel);
  85. }
  86. // POST: CustomerController/Edit/5
  87. [HttpPost]
  88. [ValidateAntiForgeryToken]
  89. public ActionResult Edit(int id, CustomerModel model)
  90. {
  91. if (!ModelState.IsValid)
  92. return View(model);
  93. var customer = _eRPDbContext.Customers
  94. .FirstOrDefault(u => u.Id == id);
  95. customer.Firstname = model.Firstname;
  96. customer.Lastname = model.Lastname;
  97. customer.CompanyName = model.CompanyName;
  98. customer.Address = model.Address;
  99. customer.Town = model.Town;
  100. customer.ZipCode = model.ZipCode;
  101. customer.Country = model.Country;
  102. customer.IsBusiness = model.IsBusiness;
  103. customer.ChangedBy = User.Identity.Name;
  104. customer.ChangedOn = DateTime.Now;
  105. _eRPDbContext.SaveChanges();
  106. return RedirectToAction(nameof(Index));
  107. }
  108. // POST: CustomerController/Delete/5
  109. [HttpPost]
  110. [ValidateAntiForgeryToken]
  111. public ActionResult Delete(int id, IFormCollection collection)
  112. {
  113. var customer = _eRPDbContext.Customers
  114. .FirstOrDefault(c => c.Id == id);
  115. _eRPDbContext.Customers.Remove(customer);
  116. _eRPDbContext.SaveChanges();
  117. return RedirectToAction(nameof(Index));
  118. }
  119. #endregion
  120. }
  121. }