Update Quantity working; applying responsive styling changes next. Checking in at working state.
This commit is contained in:
parent
df593b704d
commit
a3a1071482
|
@ -128,5 +128,29 @@ namespace ShopOnline.Api.Controllers
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPatch("{id:int}")]
|
||||||
|
public async Task<ActionResult<CartItemDto>> UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cartItem = await this.shoppingCartRepository.UpdateQuantity(id, cartItemQuantityUpdateDto);
|
||||||
|
if(cartItem == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var product = await productRepository.GetItem(cartItem.ProductId);
|
||||||
|
|
||||||
|
var cartItemDto = cartItem.ConvertToDto(product);
|
||||||
|
|
||||||
|
return Ok(cartItemDto);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,9 +87,18 @@ namespace ShopOnline.Api.Repositories
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<CartItem> UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto)
|
public async Task<CartItem> UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var item = await this.shopOnlineDbContext.CartItems.FindAsync(id);
|
||||||
|
|
||||||
|
if(item != null)
|
||||||
|
{
|
||||||
|
item.Quantity = cartItemQuantityUpdateDto.Quantity;
|
||||||
|
await this.shopOnlineDbContext.SaveChangesAsync();
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,12 @@ else {
|
||||||
<div class="col-md-8">
|
<div class="col-md-8">
|
||||||
<h5>@item.ProductName</h5>
|
<h5>@item.ProductName</h5>
|
||||||
<div class="mb-4">@item.ProductDescription</div>
|
<div class="mb-4">@item.ProductDescription</div>
|
||||||
<span>Price: <b>@item.Price.ToString("C")</b></span>
|
<span>Price: <b>@item.Price.ToString("C")</b>
|
||||||
|
<input type="number" @bind="@item.Quantity"/>
|
||||||
|
<button class="btn btn-info btn-sm"
|
||||||
|
@onclick="(()=> UpdateQuantityCartItem_Click(item.Id, item.Quantity))"
|
||||||
|
>Update Quantity</button>
|
||||||
|
</span>
|
||||||
<div>
|
<div>
|
||||||
<button @onclick = "(() => DeleteCartItem_Click(item.Id))"
|
<button @onclick = "(() => DeleteCartItem_Click(item.Id))"
|
||||||
class="btn btn-danger sm">Remove</button>
|
class="btn btn-danger sm">Remove</button>
|
||||||
|
@ -31,7 +36,7 @@ else {
|
||||||
<div class="col-md-3">
|
<div class="col-md-3">
|
||||||
<h5>Cart Summary</h5>
|
<h5>Cart Summary</h5>
|
||||||
<div class="mt-2">
|
<div class="mt-2">
|
||||||
<div>Total - </div>
|
<div>Total - (@TotalQuantity items) <b>@TotalPrice</b></div>
|
||||||
<a href="#" class="btn btn-success">
|
<a href="#" class="btn btn-success">
|
||||||
<span class="oi oi-credit-card"></span> Proceed to Checkout
|
<span class="oi oi-credit-card"></span> Proceed to Checkout
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -13,11 +13,15 @@ namespace ShopOnline.Web.Pages
|
||||||
|
|
||||||
public string ErrorMessage { get; set; }
|
public string ErrorMessage { get; set; }
|
||||||
|
|
||||||
|
protected string TotalPrice { get; set; }
|
||||||
|
protected int TotalQuantity { get; set; }
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId);
|
ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId);
|
||||||
|
CalculateCartSummaryTotals();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -30,9 +34,71 @@ namespace ShopOnline.Web.Pages
|
||||||
var cartItemDto = await ShoppingCartService.DeleteItem(id);
|
var cartItemDto = await ShoppingCartService.DeleteItem(id);
|
||||||
|
|
||||||
RemoveCartItem(id);
|
RemoveCartItem(id);
|
||||||
|
CalculateCartSummaryTotals();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async Task UpdateQuantityCartItem_Click(int id, int quantity)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if(quantity > 0)
|
||||||
|
{
|
||||||
|
var updateItemDto = new CartItemQuantityUpdateDto
|
||||||
|
{
|
||||||
|
CartItemId = id,
|
||||||
|
Quantity = quantity
|
||||||
|
};
|
||||||
|
|
||||||
|
var returnedUpdateItemDto = await this.ShoppingCartService.UpdateQuantity(updateItemDto);
|
||||||
|
|
||||||
|
UpdateItemTotalPrice(returnedUpdateItemDto);
|
||||||
|
CalculateCartSummaryTotals();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var item = this.ShoppingCartItems.FirstOrDefault(i => i.Id == id);
|
||||||
|
|
||||||
|
if(item != null)
|
||||||
|
{
|
||||||
|
item.Quantity = 1;
|
||||||
|
item.TotalPrice = item.Price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateItemTotalPrice(CartItemDto cartItemDto)
|
||||||
|
{
|
||||||
|
var item = GetCartItem(cartItemDto.Id);
|
||||||
|
|
||||||
|
if(item != null)
|
||||||
|
{
|
||||||
|
item.TotalPrice = cartItemDto.Price * cartItemDto.Quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CalculateCartSummaryTotals()
|
||||||
|
{
|
||||||
|
SetTotalPrice();
|
||||||
|
SetTotalQuantity();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetTotalPrice()
|
||||||
|
{
|
||||||
|
TotalPrice = this.ShoppingCartItems.Sum(p => p.TotalPrice).ToString("C");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetTotalQuantity()
|
||||||
|
{
|
||||||
|
TotalQuantity = this.ShoppingCartItems.Sum(p => p.Quantity);
|
||||||
|
}
|
||||||
|
|
||||||
private CartItemDto GetCartItem(int id)
|
private CartItemDto GetCartItem(int id)
|
||||||
{
|
{
|
||||||
return ShoppingCartItems.FirstOrDefault(i => i.Id == id);
|
return ShoppingCartItems.FirstOrDefault(i => i.Id == id);
|
||||||
|
@ -44,5 +110,7 @@ namespace ShopOnline.Web.Pages
|
||||||
|
|
||||||
ShoppingCartItems.Remove(CartItemDto);
|
ShoppingCartItems.Remove(CartItemDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,6 @@ namespace ShopOnline.Web.Services.Contracts
|
||||||
Task<List<CartItemDto>> GetItems(int userId);
|
Task<List<CartItemDto>> GetItems(int userId);
|
||||||
Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto);
|
Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto);
|
||||||
Task<CartItemDto> DeleteItem(int id);
|
Task<CartItemDto> DeleteItem(int id);
|
||||||
|
Task<CartItemDto> UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
using ShopOnline.Models.Dtos;
|
using Newtonsoft.Json;
|
||||||
|
using ShopOnline.Models.Dtos;
|
||||||
using ShopOnline.Web.Services.Contracts;
|
using ShopOnline.Web.Services.Contracts;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace ShopOnline.Web.Services
|
namespace ShopOnline.Web.Services
|
||||||
{
|
{
|
||||||
|
@ -82,6 +85,26 @@ namespace ShopOnline.Web.Services
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<CartItemDto> UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonRequest = JsonConvert.SerializeObject(cartItemQuantityUpdateDto);
|
||||||
|
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json");
|
||||||
|
|
||||||
|
var response = await httpClient.PatchAsync($"api/ShoppingCart/{cartItemQuantityUpdateDto.CartItemId}", content);
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return await response.Content.ReadFromJsonAsync<CartItemDto>();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.8" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.8" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue