Files
InventoryTracker/InventoryTraker.Web/Controllers/InventoryController.cs
T
2016-08-22 11:03:00 -04:00

59 lines
1.4 KiB
C#

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<InventoryViewModel>()
.ToArray();
return BetterJson(viewModels);
}
public JsonResult Add(InventoryAddForm form)
{
if (!ModelState.IsValid)
return PackageModelStateErrors();
var inventory = Mapper.Map<Inventory>(form);
inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId);
_context.Inventories.Add(inventory);
inventory.Transactions = new List<Transaction>();
inventory.Transactions.Add(new Transaction
{
AddedQuantity = inventory.Quantity,
Memo = "Arrival",
Timestamp = DateTime.Now,
TransactionDate = inventory.AddedDate
});
_context.SaveChanges();
var model = Mapper.Map<InventoryViewModel>(inventory);
return BetterJson(model);
}
}
}