CustomerController.cs 5.0 KB

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