Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Cart page #98

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/Congo.Contracts/Clients/ICongoUserClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Congo.Contracts.Responses.Cart;
using Congo.Contracts.Responses.Orders;
using Congo.Contracts.Responses.Products;
using Refit;
Expand All @@ -14,5 +15,11 @@ public interface ICongoUserClient

[Post("/api/products/{id}/purchase")]
Task<ApiResponse<OrderConfirmationResponse>> PurchaseProduct(Guid id);

[Get("/api/Cart/{id}")]
Task<ApiResponse<CartResponse>> GetCart(Guid id);

[Post("/add-to-cart")]
Task<ApiResponse<Guid>> AddToCart(Guid cartId, Guid productId, int quantity = 1);
}
}
1 change: 1 addition & 0 deletions src/Congo.RazorPages/Extensions/ServicesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class ServicesExtensions
public static void AddAppServices(this IServiceCollection services)
{
services.AddScoped<IProductsService, ProductsService>();
services.AddScoped<ICartService, CartService>();
}

public static void AddHttpClients(this IServiceCollection services, IConfiguration config)
Expand Down
28 changes: 28 additions & 0 deletions src/Congo.RazorPages/Pages/Cart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@page
@model CartModel
@{
ViewData["Title"] = "Cart";
}

<h1>Cart</h1>
<div class="container-fluid">
<div class="row">
@if (Model.Cart == null || Model.Cart.CartItems.Count == 0)
{
<p>Cart is empty!</p>
} else
{
@foreach (var cartItem in Model.Cart.CartItems)
{
<div class="card col-sm-12 col-md-4 col-lg-3 m-1">
<img class="align-text-top pt-3" src="@(cartItem.Product.ImageUrl)" alt="@cartItem.Product.Name">
<div class="card-body">
<h4>@cartItem.Product.Name</h4>
<p>Quantity: @(cartItem.Amount)</p>
<p>Total Price: @(cartItem.TotalPrice)</p>
</div>
</div>
}
}
</div>
</div>
25 changes: 25 additions & 0 deletions src/Congo.RazorPages/Pages/Cart.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using Congo.Contracts.Responses.Cart;
using Congo.RazorPages.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Congo.RazorPages.Pages
{
public class CartModel : PageModel
{
public CartResponse Cart { get; set; }
private readonly ICartService _cartService;

public CartModel(ICartService cartService) => _cartService = cartService;

public async Task<IActionResult> OnGetAsync(Guid cartId)
{
Cart = await _cartService.GetCart(cartId);
return Page();
}


}
}
3 changes: 3 additions & 0 deletions src/Congo.RazorPages/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Products">Products</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Cart" asp-route-cartId="@Context.Request.Cookies["cartId"]">Cart</a>
</li>
</ul>
</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions src/Congo.RazorPages/Services/CartService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using Congo.Contracts.Clients;
using Congo.Contracts.Responses.Cart;

namespace Congo.RazorPages.Services
{
public class CartService : ICartService
{
private readonly ICongoUserClient _client;

public CartService(ICongoUserClient client) => _client = client;

public async Task<CartResponse> GetCart(Guid cartId)
{
var result = (await _client.GetCart(cartId));
return result.Content;
}

public async Task<Guid> AddToCart(Guid cartId, Guid productId, int quantity)
{
return (await _client.AddToCart(cartId, productId, quantity)).Content;
}
}
}
12 changes: 12 additions & 0 deletions src/Congo.RazorPages/Services/ICartService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;
using Congo.Contracts.Responses.Cart;

namespace Congo.RazorPages.Services
{
public interface ICartService
{
Task<Guid> AddToCart(Guid cartId, Guid productId, int quantity);
Task<CartResponse> GetCart(Guid cartId);
}
}
1 change: 1 addition & 0 deletions src/Congo.WebApi/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Congo.Contracts.Responses.Cart;
using Congo.WebApi.Data.CartAccess;
using Mapster;

using MediatR;
using Microsoft.AspNetCore.Mvc;
using static Congo.WebApi.Data.CartAccess.CartCommands;
Expand Down