Inventory Type editing
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using InventoryTraker.Web.ActionResults;
|
||||
|
||||
@@ -11,14 +12,22 @@ namespace InventoryTraker.Web.Controllers
|
||||
return new BetterJsonResult<T> {Data = model};
|
||||
}
|
||||
|
||||
protected JsonResult PackageModelStateErrors()
|
||||
protected IEnumerable<string> GetModelStateErrorList()
|
||||
{
|
||||
var errorList =
|
||||
from kvp in ModelState
|
||||
where kvp.Value.Errors.Any()
|
||||
let errors = string.Join(", ", kvp.Value.Errors.Select(e => e.ErrorMessage))
|
||||
let msg = kvp.Key + ": " + errors
|
||||
select msg;
|
||||
return errorList;
|
||||
}
|
||||
|
||||
protected JsonResult GetModelStateErrorListJson()
|
||||
{
|
||||
var betterJsonResult = new BetterJsonResult();
|
||||
foreach (var err in ModelState.Where(ms => ms.Value.Errors.Any()))
|
||||
{
|
||||
betterJsonResult.AddError(
|
||||
err.Key + ": " + string.Join(", ", err.Value.Errors.Select(e => e.ErrorMessage)));
|
||||
}
|
||||
foreach (var err in GetModelStateErrorList())
|
||||
betterJsonResult.AddError(err);
|
||||
return betterJsonResult;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace InventoryTraker.Web.Controllers
|
||||
public JsonResult Add(InventoryAddForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return PackageModelStateErrors();
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var inventory = Mapper.Map<Inventory>(form);
|
||||
inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId);
|
||||
@@ -71,7 +71,7 @@ namespace InventoryTraker.Web.Controllers
|
||||
public JsonResult Distribute(InventoryDistributeForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return PackageModelStateErrors();
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
foreach (var quantityForm in form.InventoryQuantities)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
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;
|
||||
|
||||
@@ -15,6 +19,11 @@ namespace InventoryTraker.Web.Controllers
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public ActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public JsonResult All()
|
||||
{
|
||||
var viewModels = _context.InventoryTypes
|
||||
@@ -23,5 +32,32 @@ namespace InventoryTraker.Web.Controllers
|
||||
|
||||
return BetterJson(viewModels.ToArray());
|
||||
}
|
||||
|
||||
public JsonResult Add(InventoryTypeViewModel form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var inventoryType = Mapper.Map<InventoryType>(form);
|
||||
_context.InventoryTypes.Add(inventoryType);
|
||||
_context.SaveChanges();
|
||||
|
||||
var model = Mapper.Map<InventoryTypeViewModel>(inventoryType);
|
||||
return BetterJson(model);
|
||||
}
|
||||
|
||||
public JsonResult Update(InventoryTypeViewModel form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var inventoryType = _context.InventoryTypes.Find(form.Id);
|
||||
Mapper.Map(form, inventoryType);
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
var model = Mapper.Map<InventoryTypeViewModel>(inventoryType);
|
||||
return BetterJson(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user