diff --git a/InventoryTraker.Web/Controllers/ControllerBase.cs b/InventoryTraker.Web/Controllers/ControllerBase.cs index 97316a8..082f38d 100644 --- a/InventoryTraker.Web/Controllers/ControllerBase.cs +++ b/InventoryTraker.Web/Controllers/ControllerBase.cs @@ -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 {Data = model}; } - protected JsonResult PackageModelStateErrors() + protected IEnumerable 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; } } diff --git a/InventoryTraker.Web/Controllers/InventoryController.cs b/InventoryTraker.Web/Controllers/InventoryController.cs index 0bfd247..5a59176 100644 --- a/InventoryTraker.Web/Controllers/InventoryController.cs +++ b/InventoryTraker.Web/Controllers/InventoryController.cs @@ -46,7 +46,7 @@ namespace InventoryTraker.Web.Controllers public JsonResult Add(InventoryAddForm form) { if (!ModelState.IsValid) - return PackageModelStateErrors(); + return GetModelStateErrorListJson(); var inventory = Mapper.Map(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) { diff --git a/InventoryTraker.Web/Controllers/InventoryTypeController.cs b/InventoryTraker.Web/Controllers/InventoryTypeController.cs index b13c451..fd7b048 100644 --- a/InventoryTraker.Web/Controllers/InventoryTypeController.cs +++ b/InventoryTraker.Web/Controllers/InventoryTypeController.cs @@ -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(form); + _context.InventoryTypes.Add(inventoryType); + _context.SaveChanges(); + + var model = Mapper.Map(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(inventoryType); + return BetterJson(model); + } } } \ No newline at end of file diff --git a/InventoryTraker.Web/InventoryTraker.Web.csproj b/InventoryTraker.Web/InventoryTraker.Web.csproj index 87b6f49..95bd9fa 100644 --- a/InventoryTraker.Web/InventoryTraker.Web.csproj +++ b/InventoryTraker.Web/InventoryTraker.Web.csproj @@ -224,10 +224,14 @@ + + + + - + @@ -270,6 +274,9 @@ PreserveNewest + + + @@ -319,6 +326,7 @@ + @@ -349,6 +357,7 @@ + Web.config diff --git a/InventoryTraker.Web/Models/InventoryDistributeForm.cs b/InventoryTraker.Web/Models/InventoryDistributeForm.cs index 7fd7c6e..7ff4240 100644 --- a/InventoryTraker.Web/Models/InventoryDistributeForm.cs +++ b/InventoryTraker.Web/Models/InventoryDistributeForm.cs @@ -4,15 +4,6 @@ using System.ComponentModel.DataAnnotations; namespace InventoryTraker.Web.Models { - public class InventoryQuantityForm - { - [Required] - public int InventoryId { get; set; } - - [Required, Range(1, int.MaxValue, ErrorMessage = "Quantity must be greater than 0")] - public int Quantity { get; set; } - } - public class InventoryDistributeForm { public IList InventoryQuantities { get; set; } diff --git a/InventoryTraker.Web/Models/InventoryQuantityForm.cs b/InventoryTraker.Web/Models/InventoryQuantityForm.cs new file mode 100644 index 0000000..2afa59a --- /dev/null +++ b/InventoryTraker.Web/Models/InventoryQuantityForm.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace InventoryTraker.Web.Models +{ + public class InventoryQuantityForm + { + [Required] + public int InventoryId { get; set; } + + [Required, Range(1, int.MaxValue, ErrorMessage = "Quantity must be greater than 0")] + public int Quantity { get; set; } + } +} \ No newline at end of file diff --git a/InventoryTraker.Web/Models/InventoryTypeViewModel.cs b/InventoryTraker.Web/Models/InventoryTypeViewModel.cs index da8f12b..00e8921 100644 --- a/InventoryTraker.Web/Models/InventoryTypeViewModel.cs +++ b/InventoryTraker.Web/Models/InventoryTypeViewModel.cs @@ -1,22 +1,31 @@ -using Heroic.AutoMapper; +using System.ComponentModel.DataAnnotations; +using System.Web.Mvc; +using Heroic.AutoMapper; using InventoryTraker.Web.Core; namespace InventoryTraker.Web.Models { - public class InventoryTypeViewModel : IMapFrom + public class InventoryTypeViewModel : IMapFrom, IMapTo { + [HiddenInput] public int Id { get; set; } + [Required] public string Identifier { get; set; } + [Required] public string Name { get; set; } + [Required] public int UnitsPerCase { get; set; } + [Required] public string ContainerType { get; set; } + [Required] public double WeightPerCase { get; set; } + [Required] public decimal PricePerCase { get; set; } } } \ No newline at end of file diff --git a/InventoryTraker.Web/Views/Inventory/Index.cshtml b/InventoryTraker.Web/Views/Inventory/Index.cshtml index 68e0418..a6c7651 100644 --- a/InventoryTraker.Web/Views/Inventory/Index.cshtml +++ b/InventoryTraker.Web/Views/Inventory/Index.cshtml @@ -1,17 +1,14 @@ -@model dynamic - -@{ - ViewBag.Title = "Inventory"; -} - -
+

Inventory

- \ No newline at end of file diff --git a/InventoryTraker.Web/Views/InventoryType/Index.cshtml b/InventoryTraker.Web/Views/InventoryType/Index.cshtml new file mode 100644 index 0000000..b1b5fe7 --- /dev/null +++ b/InventoryTraker.Web/Views/InventoryType/Index.cshtml @@ -0,0 +1,16 @@ +@model dynamic + +@{ + ViewBag.Title = "Inventory"; +} + +
+

+ Commodity Types +

+
+ Add +
+ + +
\ No newline at end of file diff --git a/InventoryTraker.Web/Views/Shared/_Navigation.cshtml b/InventoryTraker.Web/Views/Shared/_Navigation.cshtml index d23b3bf..c3e2ece 100644 --- a/InventoryTraker.Web/Views/Shared/_Navigation.cshtml +++ b/InventoryTraker.Web/Views/Shared/_Navigation.cshtml @@ -30,7 +30,7 @@ Inventory
  • - Transactions + Transaction History
  • diff --git a/InventoryTraker.Web/Views/Transaction/Index.cshtml b/InventoryTraker.Web/Views/Transaction/Index.cshtml index 4b8537b..7efe498 100644 --- a/InventoryTraker.Web/Views/Transaction/Index.cshtml +++ b/InventoryTraker.Web/Views/Transaction/Index.cshtml @@ -8,13 +8,10 @@

    - Transactions + Transaction History

    -
    \ No newline at end of file diff --git a/InventoryTraker.Web/js/inventory/InventoryEditDirective.js b/InventoryTraker.Web/js/inventory/InventoryEditDirective.js index 9491e84..8b8d519 100644 --- a/InventoryTraker.Web/js/inventory/InventoryEditDirective.js +++ b/InventoryTraker.Web/js/inventory/InventoryEditDirective.js @@ -31,7 +31,7 @@ $scope.$parent.$close(); }) .error(function(data) { - vm.errorMessage = 'There was a problem saving changes to the inventory: ' + data; + vm.errorMessage = 'There was a problem saving changes to the inventory: ' + data.errorMessage; }) .finally(function() { vm.saving = false; diff --git a/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml b/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml index a2a216b..1a818e2 100644 --- a/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml +++ b/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml @@ -9,7 +9,7 @@