InventoryType from XML
Transaction updates
This commit is contained in:
@@ -8,7 +8,7 @@ namespace InventoryTraker.Web.Controllers
|
||||
{
|
||||
public BetterJsonResult<T> BetterJson<T>(T model)
|
||||
{
|
||||
return new BetterJsonResult<T>() {Data = model};
|
||||
return new BetterJsonResult<T> {Data = model};
|
||||
}
|
||||
|
||||
protected JsonResult PackageModelStateErrors()
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using AutoMapper;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using InventoryTraker.Web.ActionResults;
|
||||
using InventoryTraker.Web.Core;
|
||||
using InventoryTraker.Web.Data;
|
||||
using InventoryTraker.Web.Models;
|
||||
@@ -26,14 +27,22 @@ namespace InventoryTraker.Web.Controllers
|
||||
|
||||
public JsonResult All()
|
||||
{
|
||||
var viewModels = _context.Inventories
|
||||
.OrderBy(x => x.InventoryType.Name)
|
||||
var viewModels =
|
||||
AllInventory()
|
||||
.ProjectTo<InventoryViewModel>()
|
||||
.ToArray();
|
||||
|
||||
return BetterJson(viewModels);
|
||||
}
|
||||
|
||||
private IQueryable<Inventory> AllInventory()
|
||||
{
|
||||
return _context
|
||||
.Inventories
|
||||
.Where(x => x.Quantity > 0)
|
||||
.OrderBy(x => x.InventoryType.Name);
|
||||
}
|
||||
|
||||
public JsonResult Add(InventoryAddForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
@@ -42,18 +51,56 @@ namespace InventoryTraker.Web.Controllers
|
||||
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
|
||||
inventory.Transactions = new List<Transaction>
|
||||
{
|
||||
AddedQuantity = inventory.Quantity,
|
||||
Memo = "Arrival",
|
||||
Timestamp = DateTime.Now,
|
||||
TransactionDate = inventory.AddedDate
|
||||
});
|
||||
new Transaction
|
||||
{
|
||||
AddedQuantity = inventory.Quantity,
|
||||
CurrentQuantity = inventory.Quantity,
|
||||
Memo = "Arrival",
|
||||
Timestamp = DateTime.Now,
|
||||
TransactionDate = inventory.AddedDate
|
||||
}
|
||||
};
|
||||
_context.SaveChanges();
|
||||
|
||||
var model = Mapper.Map<InventoryViewModel>(inventory);
|
||||
return BetterJson(model);
|
||||
}
|
||||
|
||||
public JsonResult Distribute(InventoryDistributeForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return PackageModelStateErrors();
|
||||
|
||||
foreach (var quantityForm in form.InventoryQuantities)
|
||||
{
|
||||
var inventory = _context.Inventories.Find(quantityForm.InventoryId);
|
||||
|
||||
// check if it's really there
|
||||
if (inventory == null)
|
||||
return BetterJsonResult.Error($"Inventory {quantityForm.InventoryId} not found");
|
||||
|
||||
if (inventory.Quantity < quantityForm.Quantity)
|
||||
return BetterJsonResult.Error(
|
||||
$"Inventory {inventory.InventoryType.Name} has only {inventory.Quantity}, trying to remove {quantityForm.Quantity}");
|
||||
inventory.Quantity -= quantityForm.Quantity;
|
||||
|
||||
inventory.Transactions.Add(new Transaction
|
||||
{
|
||||
RemovedQuantity = quantityForm.Quantity,
|
||||
CurrentQuantity = inventory.Quantity,
|
||||
Memo = "Distributed to " + form.Destination,
|
||||
Timestamp = DateTime.Now,
|
||||
TransactionDate = form.DistributedDate
|
||||
});
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return BetterJson(AllInventory()
|
||||
.ProjectTo<InventoryViewModel>()
|
||||
.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using AutoMapper;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using InventoryTraker.Web.ActionResults;
|
||||
using InventoryTraker.Web.Core;
|
||||
using InventoryTraker.Web.Data;
|
||||
using InventoryTraker.Web.Models;
|
||||
|
||||
namespace InventoryTraker.Web.Controllers
|
||||
{
|
||||
public class TransactionController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public TransactionController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public ActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public JsonResult All()
|
||||
{
|
||||
var viewModels =
|
||||
_context.Transactions
|
||||
.ProjectTo<TransactionViewModel>()
|
||||
.ToArray();
|
||||
|
||||
return BetterJson(viewModels);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user