Added features to delete and update quantity of items

This commit is contained in:
Jon Hilton 2020-06-19 10:11:52 +01:00
parent 4cfc6c8f38
commit d65b916a94
6 changed files with 109 additions and 0 deletions

View file

@ -2,6 +2,7 @@ using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using ShoppingCartStarter.Shared.Cart;
using ShoppingCartStarter.Shared.Cart.LineItem;
namespace ShoppingCartStarter.Server.Endpoints.Cart
{
@ -21,5 +22,19 @@ namespace ShoppingCartStarter.Server.Endpoints.Cart
var cart = await _mediator.Send(query);
return Ok(cart);
}
[HttpDelete("lines/{id}")]
public async Task<IActionResult> Delete(Delete.Command command)
{
await _mediator.Send(command);
return Ok();
}
[HttpPut("lines")]
public async Task<IActionResult> Update([FromBody]Update.Command command)
{
await _mediator.Send(command);
return Ok();
}
}
}

View file

@ -21,6 +21,7 @@ namespace ShoppingCartStarter.Server.Endpoints.Cart
{
// in reality we'd retrieve a specific cart (for the current user/session) but for demo
// purposes this just pulls back the first cart it finds
var cart = await _context.Carts
.Include(x => x.LineItems)
.FirstOrDefaultAsync(cancellationToken: cancellationToken);

View file

@ -0,0 +1,33 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ShoppingCartStarter.Server.Data;
using ShoppingCartStarter.Shared.Cart.LineItem;
namespace ShoppingCartStarter.Server.Endpoints.Cart.LineItem
{
public class DeleteHandler : AsyncRequestHandler<Delete.Command>
{
private readonly StoreContext _context;
public DeleteHandler(StoreContext context)
{
_context = context;
}
protected override async Task Handle(Delete.Command request, CancellationToken cancellationToken)
{
// in reality we'd have more than one cart and would need to locate the correct one here
// for the current user/session
var cart = await _context.Carts.Include(x => x.LineItems)
.FirstOrDefaultAsync(cancellationToken: cancellationToken);
var toDelete = cart.LineItems.Single(x => x.Id == request.Id);
cart.LineItems.Remove(toDelete);
await _context.SaveChangesAsync(cancellationToken);
}
}
}

View file

@ -0,0 +1,35 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ShoppingCartStarter.Server.Data;
using ShoppingCartStarter.Shared.Cart.LineItem;
namespace ShoppingCartStarter.Server.Endpoints.Cart.LineItem
{
public class UpdateHandler : AsyncRequestHandler<Update.Command>
{
private readonly StoreContext _context;
public UpdateHandler(StoreContext context)
{
_context = context;
}
protected override async Task Handle(Update.Command request, CancellationToken cancellationToken)
{
// find the cart (in reality we'd use the customer's id or similar to do this)
var cart = await _context.Carts
.Include(x => x.LineItems)
.FirstOrDefaultAsync(cancellationToken: cancellationToken);
// find the relevant line and adjust its quantity
var line = cart.LineItems.Single(x => x.Id == request.Id);
line.Quantity = request.Quantity;
// save changes
await _context.SaveChangesAsync(cancellationToken);
}
}
}

View file

@ -0,0 +1,12 @@
using MediatR;
namespace ShoppingCartStarter.Shared.Cart.LineItem
{
public class Delete
{
public class Command : IRequest
{
public int Id { get; set; }
}
}
}

View file

@ -0,0 +1,13 @@
using MediatR;
namespace ShoppingCartStarter.Shared.Cart.LineItem
{
public class Update
{
public class Command : IRequest
{
public int Id { get; set; }
public int Quantity { get; set; }
}
}
}