| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Threading.Tasks;
- using GreenTree.Strohrmann.ERP.Core.Domain.Business;
- using GreenTree.Strohrmann.ERP.Core.Helper;
- using GreenTree.Strohrmann.ERP.Domain.Model;
- using GreenTree.Strohrmann.ERP.Services.Geolocator;
- using GreenTree.Strohrmann.ERP.Web.Extension;
- using GreenTree.Strohrmann.ERP.Web.Models.Business;
- using GreenTree.Strohrmann.ERP.Web.Models.Rights.User;
- using GreenTree.Strohrmann.ERP.Web.Models.Shared;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- namespace GreenTree.Strohrmann.ERP.Web.Controllers
- {
- public class CustomerController : Controller
- {
- #region DI fields
- // The global DbContext
- private readonly ERPDbContext _eRPDbContext;
- #endregion
- #region Ctor
- /// <summary>
- /// Initializes a new instance of the CustomerController class
- /// </summary>
- /// <param name="eRPDbContext">Global DbContext.</param>
- public CustomerController(ERPDbContext eRPDbContext)
- {
- _eRPDbContext = eRPDbContext;
- }
- #endregion
- #region Actions
- // GET: CustomerController
- public ActionResult Index()
- {
- var customers = _eRPDbContext.Customers
- .ToList()
- .Select(c => new CustomerModel(c));
- return View(customers);
- }
- // GET: CustomerController/Details/5
- public ActionResult Details(int id)
- {
- var customer = _eRPDbContext.Customers
- .FirstOrDefault(c => c.Id == id);
- var customerModel = new CustomerModel(customer);
- return View(customerModel);
- }
- // GET: CustomerController/Create
- public ActionResult Create()
- {
- ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
- return View();
- }
- // POST: CustomerController/Create
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create(CustomerModel model)
- {
- if (!ModelState.IsValid)
- {
- ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
- return View(model);
- }
- var customer = new Customer
- {
- Title = _eRPDbContext.Titles.Find(model.Title.Id),
- Firstname = model.Firstname,
- Lastname = model.Lastname,
- CompanyName = model.CompanyName,
- Address = model.Address,
- Town = model.Town,
- ZipCode = model.ZipCode,
- Country = model.Country,
- IsBusiness = model.IsBusiness,
- CreatedBy = User.Identity.Name,
- CreatedOn = DateTime.Now
- };
- _eRPDbContext.Customers.Add(customer);
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- // GET: CustomerController/Edit/5
- public ActionResult Edit(int id)
- {
- ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
- var customer = _eRPDbContext.Customers
- .FirstOrDefault(c => c.Id == id);
- var customerModel = new CustomerModel(customer);
- return View(customerModel);
- }
- // POST: CustomerController/Edit/5
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit(int id, CustomerModel model)
- {
- if (!ModelState.IsValid)
- {
- ViewData.AddSelectList("AvailableTitles", _eRPDbContext.Titles, m => m.Id, m => m.Name, 1);
- return View(model);
- }
- var customer = _eRPDbContext.Customers
- .FirstOrDefault(u => u.Id == id);
- customer.Title = _eRPDbContext.Titles.Find(model.Title.Id);
- customer.Firstname = model.Firstname;
- customer.Lastname = model.Lastname;
- customer.CompanyName = model.CompanyName;
- customer.Address = model.Address;
- customer.Town = model.Town;
- customer.ZipCode = model.ZipCode;
- customer.Country = model.Country;
- customer.IsBusiness = model.IsBusiness;
- customer.ChangedBy = User.Identity.Name;
- customer.ChangedOn = DateTime.Now;
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- // POST: CustomerController/Delete/5
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Delete(int id, IFormCollection collection)
- {
- var customer = _eRPDbContext.Customers
- .FirstOrDefault(c => c.Id == id);
- _eRPDbContext.Customers.Remove(customer);
- _eRPDbContext.SaveChanges();
- return RedirectToAction(nameof(Index));
- }
- #endregion
- #region Partials
- // POST: CustomerController/Search/*term*
- [HttpPost]
- public ActionResult Search(SearchModel search)
- {
- if (search == null)
- return new JsonResult(null);
- var words = search.SearchTerm.Split(' ');
- var result = _eRPDbContext.Customers
- .Where(c => words.Contains(c.Firstname) ||
- words.Contains(c.Lastname) ||
- words.Contains(c.CompanyName))
- .ToArray()
- .Select(c => new CustomerModel(c));
- return new JsonResult(result);
- }
- #endregion
- }
- }
|