From d65b916a94328e606f943376b3d473cb903bf96f Mon Sep 17 00:00:00 2001 From: Jon Hilton Date: Fri, 19 Jun 2020 10:11:52 +0100 Subject: [PATCH] Added features to delete and update quantity of items --- Server/Endpoints/Cart/CartController.cs | 15 ++++++++++ Server/Endpoints/Cart/Details.cs | 1 + Server/Endpoints/Cart/LineItem/Delete.cs | 33 ++++++++++++++++++++++ Server/Endpoints/Cart/LineItem/Update.cs | 35 ++++++++++++++++++++++++ Shared/Cart/LineItem/Delete.cs | 12 ++++++++ Shared/Cart/LineItem/Update.cs | 13 +++++++++ 6 files changed, 109 insertions(+) create mode 100644 Server/Endpoints/Cart/LineItem/Delete.cs create mode 100644 Server/Endpoints/Cart/LineItem/Update.cs create mode 100644 Shared/Cart/LineItem/Delete.cs create mode 100644 Shared/Cart/LineItem/Update.cs diff --git a/Server/Endpoints/Cart/CartController.cs b/Server/Endpoints/Cart/CartController.cs index 84918b7..676aa98 100644 --- a/Server/Endpoints/Cart/CartController.cs +++ b/Server/Endpoints/Cart/CartController.cs @@ -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 Delete(Delete.Command command) + { + await _mediator.Send(command); + return Ok(); + } + + [HttpPut("lines")] + public async Task Update([FromBody]Update.Command command) + { + await _mediator.Send(command); + return Ok(); + } } } \ No newline at end of file diff --git a/Server/Endpoints/Cart/Details.cs b/Server/Endpoints/Cart/Details.cs index ea0ebb6..8a033ca 100644 --- a/Server/Endpoints/Cart/Details.cs +++ b/Server/Endpoints/Cart/Details.cs @@ -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); diff --git a/Server/Endpoints/Cart/LineItem/Delete.cs b/Server/Endpoints/Cart/LineItem/Delete.cs new file mode 100644 index 0000000..ad9809b --- /dev/null +++ b/Server/Endpoints/Cart/LineItem/Delete.cs @@ -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 + { + 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); + } + } +} \ No newline at end of file diff --git a/Server/Endpoints/Cart/LineItem/Update.cs b/Server/Endpoints/Cart/LineItem/Update.cs new file mode 100644 index 0000000..1493c92 --- /dev/null +++ b/Server/Endpoints/Cart/LineItem/Update.cs @@ -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 + { + 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); + } + } +} \ No newline at end of file diff --git a/Shared/Cart/LineItem/Delete.cs b/Shared/Cart/LineItem/Delete.cs new file mode 100644 index 0000000..bb90cd6 --- /dev/null +++ b/Shared/Cart/LineItem/Delete.cs @@ -0,0 +1,12 @@ +using MediatR; + +namespace ShoppingCartStarter.Shared.Cart.LineItem +{ + public class Delete + { + public class Command : IRequest + { + public int Id { get; set; } + } + } +} \ No newline at end of file diff --git a/Shared/Cart/LineItem/Update.cs b/Shared/Cart/LineItem/Update.cs new file mode 100644 index 0000000..b3a4abb --- /dev/null +++ b/Shared/Cart/LineItem/Update.cs @@ -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; } + } + } +} \ No newline at end of file