Remove inventory, delete transactions

This commit is contained in:
2016-09-14 07:53:41 -04:00
parent 9cdf90b1e9
commit 052f812d6f
33 changed files with 3250 additions and 142 deletions
@@ -12,7 +12,7 @@ namespace InventoryTraker.Web.Controllers
return new BetterJsonResult<T> {Data = model};
}
protected IEnumerable<string> GetModelStateErrorList()
protected string[] GetModelStateErrorList()
{
var errorList =
from kvp in ModelState
@@ -20,13 +20,18 @@ namespace InventoryTraker.Web.Controllers
let errors = string.Join(", ", kvp.Value.Errors.Select(e => e.ErrorMessage))
let msg = kvp.Key + ": " + errors
select msg;
return errorList;
return errorList.ToArray();
}
protected JsonResult GetModelStateErrorListJson()
{
return GetErrorListJson(GetModelStateErrorList());
}
protected JsonResult GetErrorListJson(params string[] errors)
{
var betterJsonResult = new BetterJsonResult();
foreach (var err in GetModelStateErrorList())
foreach (var err in errors)
betterJsonResult.AddError(err);
return betterJsonResult;
}
@@ -4,7 +4,6 @@ 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;
@@ -57,6 +56,10 @@ namespace InventoryTraker.Web.Controllers
var inventory = Mapper.Map<Inventory>(form);
inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId);
if (inventory.InventoryType == null)
return GetErrorListJson("Inventory Type not found");
_context.Inventories.Add(inventory);
inventory.Transactions = new List<Transaction>
{
@@ -81,35 +84,105 @@ namespace InventoryTraker.Web.Controllers
if (!ModelState.IsValid)
return GetModelStateErrorListJson();
var errors = new List<string>();
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");
{
errors.Add($"'{quantityForm.InventoryId}' not found");
continue;
}
if (inventory.Quantity < quantityForm.Quantity)
return BetterJsonResult.Error(
$"Inventory {inventory.InventoryType.Name} has only {inventory.Quantity}, trying to remove {quantityForm.Quantity}");
errors.Add(
$"'{inventory.InventoryType.Name}' has only {inventory.Quantity} qty, " +
$"cannot remove {quantityForm.Quantity}");
inventory.Quantity -= quantityForm.Quantity;
var mostRecentTransaction =
inventory.Transactions.OrderByDescending(t => t.TransactionDate).First();
if (form.DistributedDate < mostRecentTransaction.TransactionDate)
errors.Add($"'{inventory.InventoryType.Name}' most recent transaction " +
$"is {mostRecentTransaction.TransactionDate.ToShortDateString()}. " +
$"Cannot add a transaction on {form.DistributedDate.ToShortDateString()}.");
inventory.Transactions.Add(new Transaction
{
TransactionType = TransactionType.Distributed,
RemovedQuantity = quantityForm.Quantity,
CurrentQuantity = inventory.Quantity,
Memo = "Distributed to " + form.Destination,
Memo =
string.IsNullOrEmpty(form.Memo)
? $"{form.Memo} | "
: ""
+ $"Distributed to {form.Destination}",
Timestamp = DateTime.Now,
TransactionDate = form.DistributedDate
});
}
if (errors.Any())
return GetErrorListJson(errors.ToArray());
_context.SaveChanges();
return BetterJson(AllInventory()
.ProjectTo<InventoryViewModel>()
.ToArray());
}
public JsonResult Remove(InventoryRemoveForm form)
{
if (!ModelState.IsValid)
return GetModelStateErrorListJson();
var errors = new List<string>();
var inventory = _context.Inventories.Find(form.InventoryId);
// check if it's really there
if (inventory == null)
{
errors.Add($"'{form.InventoryId}' not found");
}
else
{
if (inventory.Quantity < form.Quantity)
errors.Add(
$"'{inventory.InventoryType.Name}' has only {inventory.Quantity} qty, " +
$"cannot remove {form.Quantity}");
inventory.Quantity -= form.Quantity;
var mostRecentTransaction =
inventory.Transactions.OrderByDescending(t => t.TransactionDate).First();
if (form.RemovedDate < mostRecentTransaction.TransactionDate)
errors.Add($"'{inventory.InventoryType.Name}' most recent transaction " +
$"is {mostRecentTransaction.TransactionDate.ToShortDateString()}. " +
$"Cannot add a transaction on {form.RemovedDate.ToShortDateString()}.");
inventory.Transactions.Add(new Transaction
{
TransactionType = form.TransactionType,
RemovedQuantity = form.Quantity,
CurrentQuantity = inventory.Quantity,
Memo = form.Memo,
Timestamp = DateTime.Now,
TransactionDate = form.RemovedDate
});
}
if (errors.Any())
return GetErrorListJson(errors.ToArray());
_context.SaveChanges();
return BetterJson(Mapper.Map<InventoryViewModel>(inventory));
}
}
}
@@ -2,9 +2,7 @@
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;
@@ -35,7 +33,7 @@ namespace InventoryTraker.Web.Controllers
return BetterJson(viewModels);
}
public JsonResult GetTransactions(int? pageNumber, int? pageSize, int? inventoryId)
public JsonResult Get(int? pageNumber, int? pageSize, int? inventoryId)
{
IQueryable<Transaction> query =
_context.Transactions
@@ -56,5 +54,38 @@ namespace InventoryTraker.Web.Controllers
.ToArray();
return BetterJson(new { totalItems, transactions });
}
public JsonResult Delete(int transactionId)
{
var transaction = _context.Transactions.Find(transactionId);
if (transaction == null)
return GetErrorListJson("Transaction not found");
var inventory = transaction.Inventory;
var inventoryId = inventory.Id;
var inventoryTransactionsMostRecent =
inventory
.Transactions.OrderByDescending(t => t.TransactionDate)
.First() == transaction;
if (!inventoryTransactionsMostRecent)
return GetErrorListJson("Delete permitted only when transaction is the most recent");
// roll back that transaction
inventory.Quantity -= transaction.AddedQuantity;
inventory.Quantity += transaction.RemovedQuantity;
inventory.Transactions.Remove(transaction);
_context.Transactions.Remove(transaction);
if (!inventory.Transactions.Any())
_context.Inventories.Remove(inventory);
_context.SaveChanges();
return BetterJson(new { inventoryId});
}
}
}