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.Models.Business; using GreenTree.Strohrmann.ERP.Web.Models.Rights.User; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace GreenTree.Strohrmann.ERP.Web.Controllers { public class SupplierController : Controller { #region DI fields // The global DbContext private readonly ERPDbContext _eRPDbContext; #endregion #region Ctor /// /// Initializes a new instance of the SupplierController class /// /// Global DbContext. public SupplierController(ERPDbContext eRPDbContext) { _eRPDbContext = eRPDbContext; } #endregion #region Actions // GET: SupplierController public ActionResult Index() { var suppliers = _eRPDbContext.Suppliers .ToList() .Select(c => new SupplierModel(c)); return View(suppliers); } // GET: SupplierController/Details/5 public ActionResult Details(int id) { var supplier = _eRPDbContext.Suppliers .FirstOrDefault(c => c.Id == id); var supplierModel = new SupplierModel(supplier); return View(supplierModel); } // GET: SupplierController/Create public ActionResult Create() { return View(); } // POST: SupplierController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(SupplierModel model) { if (!ModelState.IsValid) return View(model); var supplier = new Supplier { Name = model.Name, Description = model.Description, Address = model.Address, Town = model.Town, ZipCode = model.ZipCode, Country = model.Country, PhoneFirst = model.PhoneFirst, PhoneSecond = model.PhoneSecond, MailFirst = model.MailFirst, MailSecond = model.MailSecond, Comment = model.Comment, CreatedBy = User.Identity.Name, CreatedOn = DateTime.Now }; _eRPDbContext.Suppliers.Add(supplier); _eRPDbContext.SaveChanges(); return RedirectToAction(nameof(Index)); } // GET: SupplierController/Edit/5 public ActionResult Edit(int id) { var supplier = _eRPDbContext.Suppliers .FirstOrDefault(c => c.Id == id); var supplierModel = new SupplierModel(supplier); return View(supplierModel); } // POST: SupplierController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, SupplierModel model) { if (!ModelState.IsValid) return View(model); var supplier = _eRPDbContext.Suppliers .FirstOrDefault(u => u.Id == id); supplier.Name = model.Name; supplier.Description = model.Description; supplier.Address = model.Address; supplier.Town = model.Town; supplier.ZipCode = model.ZipCode; supplier.Country = model.Country; supplier.PhoneFirst = model.PhoneFirst; supplier.PhoneSecond = model.PhoneSecond; supplier.MailFirst = model.MailFirst; supplier.MailSecond = model.MailSecond; supplier.Comment = model.Comment; supplier.ChangedBy = User.Identity.Name; supplier.ChangedOn = DateTime.Now; _eRPDbContext.SaveChanges(); return RedirectToAction(nameof(Index)); } // POST: SupplierController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, IFormCollection collection) { var supplier = _eRPDbContext.Suppliers .FirstOrDefault(c => c.Id == id); _eRPDbContext.Suppliers.Remove(supplier); _eRPDbContext.SaveChanges(); return RedirectToAction(nameof(Index)); } #endregion } }