Breaking 05_03 in twain
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
using HPlusSports.Models;
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using HPlusSports.Models;
|
||||
using HPlusSports.Requests;
|
||||
|
||||
namespace HPlusSports.Controllers
|
||||
{
|
||||
@ -10,11 +11,6 @@ namespace HPlusSports.Controllers
|
||||
{
|
||||
private HPlusSportsDbContext _context;
|
||||
|
||||
public InventoryController()
|
||||
: this(new HPlusSportsDbContext())
|
||||
{
|
||||
}
|
||||
|
||||
public InventoryController(HPlusSportsDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
@ -35,7 +31,6 @@ namespace HPlusSports.Controllers
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Create(CreateProductRequest request)
|
||||
{
|
||||
@ -44,7 +39,8 @@ namespace HPlusSports.Controllers
|
||||
return View();
|
||||
}
|
||||
|
||||
var product = new Product {
|
||||
var product = new Product
|
||||
{
|
||||
CategoryId = request.CategoryId,
|
||||
Description = request.Description,
|
||||
MSRP = request.MSRP,
|
||||
@ -59,8 +55,7 @@ namespace HPlusSports.Controllers
|
||||
_context.Products.Add(product);
|
||||
_context.SaveChanges();
|
||||
|
||||
TempData["SuccessMessage"] =
|
||||
$"Successfully created \"{product.Name}\"";
|
||||
TempData.SuccessMessage($"Successfully created \"{product.Name}\"");
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
@ -72,43 +67,41 @@ namespace HPlusSports.Controllers
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
return ProductListError(
|
||||
$"Couldn't update product #\"{id}\": product not found!"
|
||||
);
|
||||
TempData.ErrorMessage($"Couldn't update product #\"{id}\": product not found!");
|
||||
}
|
||||
|
||||
return View(existing);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult Update(long id, Product product)
|
||||
public ActionResult Update(UpdateProductRequest request)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
var existing = _context.Products.Find(id);
|
||||
request.LastUpdatedUserId = GetUserId(this);
|
||||
|
||||
var existing = _context.Products.Find(request.Id);
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
return ProductListError(
|
||||
$"Couldn't update product #\"{id}\": product not found!"
|
||||
);
|
||||
TempData.ErrorMessage($"Couldn't update product #\"{request.Id}\": product not found!");
|
||||
return View();
|
||||
}
|
||||
|
||||
var hasPriceChanged = existing.Price != product.Price;
|
||||
var hasPriceChanged = existing.Price != request.Price;
|
||||
|
||||
existing.CategoryId = product.CategoryId;
|
||||
existing.Description = product.Description;
|
||||
existing.MSRP = product.MSRP;
|
||||
existing.Name = product.Name;
|
||||
existing.Price = product.Price;
|
||||
existing.SKU = product.SKU;
|
||||
existing.Summary = product.Summary;
|
||||
existing.CategoryId = request.CategoryId;
|
||||
existing.Description = request.Description;
|
||||
existing.MSRP = request.MSRP;
|
||||
existing.Name = request.Name;
|
||||
existing.Price = request.Price;
|
||||
existing.SKU = request.SKU;
|
||||
existing.Summary = request.Summary;
|
||||
|
||||
existing.LastUpdated = DateTime.UtcNow;
|
||||
existing.LastUpdatedUserId = GetUserId(this);
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
@ -117,13 +110,13 @@ namespace HPlusSports.Controllers
|
||||
var cartsToUpdate =
|
||||
_context.ShoppingCarts
|
||||
.Include("Items")
|
||||
.Where(cart => cart.Items.Any(x => x.SKU == product.SKU));
|
||||
.Where(cart => cart.Items.Any(x => x.SKU == request.SKU));
|
||||
|
||||
foreach (var cart in cartsToUpdate)
|
||||
{
|
||||
foreach (var cartItem in cart.Items.Where(x => x.SKU == product.SKU))
|
||||
foreach (var cartItem in cart.Items.Where(x => x.SKU == request.SKU))
|
||||
{
|
||||
cartItem.Price = product.Price;
|
||||
cartItem.Price = request.Price;
|
||||
}
|
||||
|
||||
cart.Recalculate();
|
||||
@ -132,8 +125,7 @@ namespace HPlusSports.Controllers
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
TempData["SuccessMessage"] =
|
||||
$"Successfully updated \"{product.Name}\"";
|
||||
TempData.SuccessMessage($"Successfully updated \"{request.Name}\"");
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user