using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using AutoMapper; using AutoMapper.QueryableExtensions; using InventoryTraker.Web.Core; using InventoryTraker.Web.Data; using InventoryTraker.Web.Models; namespace InventoryTraker.Web.Controllers { public class InventoryController : ControllerBase { private readonly AppDbContext _context; public InventoryController(AppDbContext context) { _context = context; } public ActionResult Index() { return View(); } public JsonResult All() { var viewModels = _context.Inventories .OrderBy(x => x.InventoryType.Name) .ProjectTo() .ToArray(); return BetterJson(viewModels); } public JsonResult Add(InventoryAddForm form) { if (!ModelState.IsValid) return PackageModelStateErrors(); var inventory = Mapper.Map(form); inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId); _context.Inventories.Add(inventory); inventory.Transactions = new List(); inventory.Transactions.Add(new Transaction { AddedQuantity = inventory.Quantity, Memo = "Arrival", Timestamp = DateTime.Now, TransactionDate = inventory.AddedDate }); _context.SaveChanges(); var model = Mapper.Map(inventory); return BetterJson(model); } } }