Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Congo.Contracts/Requests/Cart/AddToCartRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Congo.WebApi.Requests.Cart
{
public class AddToCartRequest
{
public Guid ProductId { get; set; }
public int Quantity { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Congo.RazorPages/Congo.RazorPages.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<ProjectReference Include="..\Congo.Contracts\Congo.Contracts.csproj" />
<ProjectReference Include="..\Congo.WebApi\Congo.WebApi.csproj" />
</ItemGroup>

</Project>
49 changes: 33 additions & 16 deletions src/Congo.RazorPages/Pages/Products.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,38 @@

<h1>Products</h1>
<div class="container-fluid">
<form method="post">
<div class="row">
@foreach (var product in Model.Products)
{
<div class="card col-sm-12 col-md-4 col-lg-3 m-1">
<img class="card-img-top pt-3" src="@product.ImageUrl" alt="@product.Name">
<div class="card-body">
<h4>@product.Name</h4>
<p>$@product.Price</p>
</div>
<button class="btn btn-primary mx-2 my-4" asp-page-handler="purchase" asp-route-id="@product.Id">
Purchase
</button>
<div class="row">
@foreach (var product in Model.Products)
{
<div class="card col-sm-12 col-md-4 col-lg-3 m-1">
<img class="card-img-top pt-3" src="@product.ImageUrl" alt="@product.Name">
<div class="card-body">
<h4>@product.Name</h4>
<p>$@product.Price</p>
</div>
}
</div>
</form>

<form method="post" asp-page-handler="AddToCart" data-ajax="true" data-ajax-method="post" data-ajax-success="AddedToCart">
<input type="hidden" name="productId" value="@product.Id" />
<div class="form-group">
<div class="product-quantity">
<label class="control-label">Quantity</label>
<input class="form-control" type="number" name="quantity" size=5 min="1" value="1" />
</div>
<div class="add-to-cart">
<button type="submit" class="btn btn-primary mx-2 my-4">
Add to Cart
</button>
</div>
</div>
</form>
</div>
}
</div>
</div>

<script>
function AddedToCart()
{
// show a message or something saying that we've added the product
}
</script>
7 changes: 4 additions & 3 deletions src/Congo.RazorPages/Pages/Products.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public async Task<IActionResult> OnGetAsync()
return Page();
}

public async Task<IActionResult> OnPostPurchaseAsync(Guid id)
public async Task<IActionResult> OnPostAddToCartAsync(Guid productId, int quantity)
{
var order = await _productsService.PurchaseAsync(id);
return RedirectToPage("/OrderSuccessful", new { order.OrderId });
// call _cartService.AddToCart(new AddToCartRequest(...))
await Task.Delay(0);
return new OkObjectResult(true);
}
}
}
8 changes: 8 additions & 0 deletions src/Congo.RazorPages/Pages/Products.cshtml.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
input, label {
display: block;
}

.product-quantity {
float: left;
margin-right: 20px;
}
2 changes: 2 additions & 0 deletions src/Congo.RazorPages/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<title>@ViewData["Title"] - Congo.RazorPages</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<link rel="stylesheet" href="~/Congo.RazorPages.styles.css" />
</head>
<body>
<header>
Expand Down Expand Up @@ -44,6 +45,7 @@
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.unobtrusive-ajax/3.2.5/jquery.unobtrusive-ajax.min.js"></script>

@await RenderSectionAsync("Scripts", required: false)
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/Congo.WebApi.Tests/Controllers/CartControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public async void CartController_ReturnsCartOfTypeCartResponse()

// Assert
cartResponse.Result.Should().BeAssignableTo<OkObjectResult>();
}
}
}
}
29 changes: 23 additions & 6 deletions src/Congo.WebApi/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Threading.Tasks;
using Congo.Contracts.Responses.Cart;
using Congo.WebApi.Data.CartAccess;
using Congo.WebApi.Data.Models;
using Congo.WebApi.Requests.Cart;
using Mapster;
using MediatR;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -24,15 +26,30 @@ public async Task<ActionResult<CartResponse>> GetCartById(Guid id)
return Ok(cart.Adapt<CartResponse>());
}

[HttpPost("/add-to-cart")]
[HttpPost("add-to-cart")]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(Guid), 200)]
public async Task<ActionResult> AddToCart(Guid? cartId, Guid productId, int quantity)
[ProducesResponseType(typeof(CartResponse), 200)]
[ProducesResponseType(400)]
public async Task<ActionResult<CartResponse>> AddToCart(AddToCartRequest request)
{
var cart = await _mediator.Send(new AddToCartCommand(cartId, productId, quantity));
if (cart == Guid.Empty)
var cart = await _mediator.Send(new AddToCartCommand(CurrentCartId, request.ProductId, request.Quantity));

if (cart == null)
return BadRequest();
return Ok(cart);
else if (!cart.IsNewCart)
return NoContent();

return Ok(cart.Adapt<CartResponse>());
}

private Guid? CurrentCartId
{
get
{
if (Guid.TryParse(HttpContext.Request.Cookies["cart_id"], out var cartId))
return cartId;
return null;
}
}
}
}
15 changes: 11 additions & 4 deletions src/Congo.WebApi/Data/CartAccess/AddToCartHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Congo.Contracts.Responses.Cart;
using Congo.WebApi.Data.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using static Congo.WebApi.Data.CartAccess.CartCommands;

namespace Congo.WebApi.Data.CartAccess
{
public class AddToCartHandler : IRequestHandler<AddToCartCommand, Guid>
public class AddToCartHandler : IRequestHandler<AddToCartCommand, Cart>
{
private readonly CongoContext _dbContext;

Expand All @@ -18,17 +20,22 @@ public AddToCartHandler(CongoContext dbContext)
_dbContext = dbContext;
}

public async Task<Guid> Handle(AddToCartCommand request, CancellationToken cancellationToken)
public async Task<Cart> Handle(AddToCartCommand request, CancellationToken cancellationToken)
{
var cart = await _dbContext.Carts
.Include(c => c.CartItems)
.FirstOrDefaultAsync(C => C.Id == request.cartId);

if (cart != null)
{
cart.IsNewCart = false;
}

var product = await _dbContext.Products.FindAsync(request.productId);

if (request.cartId != null && cart == null)
{
return Guid.Empty;
return null;
}
else if (request.cartId == null)
{
Expand All @@ -43,7 +50,7 @@ public async Task<Guid> Handle(AddToCartCommand request, CancellationToken cance

await _dbContext.SaveChangesAsync();

return cart.Id;
return cart;
}

private async Task AddCartItem(AddToCartCommand request, Cart cart, Product product, CongoContext _dbContext)
Expand Down
3 changes: 2 additions & 1 deletion src/Congo.WebApi/Data/CartAccess/CartCommands.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using Congo.WebApi.Data.Models;
using MediatR;

namespace Congo.WebApi.Data.CartAccess
{
public class CartCommands
{
public record AddToCartCommand(Guid? cartId, Guid productId, int quantity) : IRequest<Guid>;
public record AddToCartCommand(Guid? cartId, Guid productId, int quantity) : IRequest<Cart>;
}
}
1 change: 1 addition & 0 deletions src/Congo.WebApi/Data/Models/Cart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class Cart
{
public Guid Id { get; init; }
public ICollection<CartItem> CartItems { get; set; } = new List<CartItem>();
public bool IsNewCart { get; set; } = true;
}
}