CustomerController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 GreenTree.Strohrmann.ERP.Web.Models.Shared;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. namespace GreenTree.Strohrmann.ERP.Web.Controllers
  17. {
  18. public class CustomerController : Controller
  19. {
  20. #region DI fields
  21. // The global DbContext
  22. private readonly ERPDbContext _eRPDbContext;
  23. #endregion
  24. #region Ctor
  25. /// <summary>
  26. /// Initializes a new instance of the CustomerController class
  27. /// </summary>
  28. /// <param name="eRPDbContext">Global DbContext.</param>
  29. public CustomerController(ERPDbContext eRPDbContext)
  30. {
  31. _eRPDbContext = eRPDbContext;
  32. }
  33. #endregion
  34. #region Actions
  35. // GET: CustomerController
  36. public ActionResult Index()
  37. {
  38. var customers = _eRPDbContext.Customers
  39. .ToList()
  40. .Select(c => new CustomerModel(c));
  41. return View(customers);
  42. }
  43. // GET: CustomerController/Details/5
  44. public ActionResult Details(int id)
  45. {
  46. var customer = _eRPDbContext.Customers
  47. .FirstOrDefault(c => c.Id == id);
  48. var customerModel = new CustomerModel(customer);
  49. return View(customerModel);
  50. }
  51. // GET: CustomerController/Create
  52. public ActionResult Create()
  53. {
  54. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  55. return View();
  56. }
  57. // POST: CustomerController/Create
  58. [HttpPost]
  59. [ValidateAntiForgeryToken]
  60. public ActionResult Create(CustomerModel model)
  61. {
  62. if (!ModelState.IsValid)
  63. {
  64. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  65. return View(model);
  66. }
  67. var customer = new Customer
  68. {
  69. Title = _eRPDbContext.Titles.Find(model.Title.Id),
  70. Firstname = model.Firstname,
  71. Lastname = model.Lastname,
  72. CompanyName = model.CompanyName,
  73. Address = model.Address,
  74. Town = model.Town,
  75. ZipCode = model.ZipCode,
  76. Country = model.Country,
  77. IsBusiness = model.IsBusiness,
  78. CreatedBy = User.Identity.Name,
  79. CreatedOn = DateTime.Now
  80. };
  81. _eRPDbContext.Customers.Add(customer);
  82. _eRPDbContext.SaveChanges();
  83. return RedirectToAction(nameof(Index));
  84. }
  85. // GET: CustomerController/Edit/5
  86. public ActionResult Edit(int id)
  87. {
  88. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  89. var customer = _eRPDbContext.Customers
  90. .FirstOrDefault(c => c.Id == id);
  91. var customerModel = new CustomerModel(customer);
  92. return View(customerModel);
  93. }
  94. // POST: CustomerController/Edit/5
  95. [HttpPost]
  96. [ValidateAntiForgeryToken]
  97. public ActionResult Edit(int id, CustomerModel model)
  98. {
  99. if (!ModelState.IsValid)
  100. {
  101. ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
  102. return View(model);
  103. }
  104. var customer = _eRPDbContext.Customers
  105. .FirstOrDefault(u => u.Id == id);
  106. customer.Title = _eRPDbContext.Titles.Find(model.Title.Id);
  107. customer.Firstname = model.Firstname;
  108. customer.Lastname = model.Lastname;
  109. customer.CompanyName = model.CompanyName;
  110. customer.Address = model.Address;
  111. customer.Town = model.Town;
  112. customer.ZipCode = model.ZipCode;
  113. customer.Country = model.Country;
  114. customer.IsBusiness = model.IsBusiness;
  115. customer.ChangedBy = User.Identity.Name;
  116. customer.ChangedOn = DateTime.Now;
  117. _eRPDbContext.SaveChanges();
  118. return RedirectToAction(nameof(Index));
  119. }
  120. // POST: CustomerController/Delete/5
  121. [HttpPost]
  122. [ValidateAntiForgeryToken]
  123. public ActionResult Delete(int id, IFormCollection collection)
  124. {
  125. var customer = _eRPDbContext.Customers
  126. .FirstOrDefault(c => c.Id == id);
  127. _eRPDbContext.Customers.Remove(customer);
  128. _eRPDbContext.SaveChanges();
  129. return RedirectToAction(nameof(Index));
  130. }
  131. #endregion
  132. #region Partials
  133. // POST: CustomerController/Search/*term*
  134. [HttpPost]
  135. public ActionResult Search(SearchModel search)
  136. {
  137. if (search == null)
  138. return new JsonResult(null);
  139. var words = search.SearchTerm.Split(' ');
  140. var result = _eRPDbContext.Customers
  141. .Where(c => words.Contains(c.Firstname) ||
  142. words.Contains(c.Lastname) ||
  143. words.Contains(c.CompanyName))
  144. .ToArray()
  145. .Select(c => new CustomerModel(c));
  146. return new JsonResult(result);
  147. }
  148. #endregion
  149. }
  150. }