Skip to content

Commit

Permalink
Add order feature
Browse files Browse the repository at this point in the history
  • Loading branch information
minsoeaung committed Dec 1, 2023
1 parent 54316d2 commit 0da5d4a
Show file tree
Hide file tree
Showing 33 changed files with 2,933 additions and 81 deletions.
3 changes: 3 additions & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.14" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.11" />
<PackageReference Include="Nager.Country" Version="4.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
Expand Down
4 changes: 2 additions & 2 deletions API/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public CartController(ICartService cartService, IMapper mapper)
}

[HttpGet]
public async Task<ActionResult<CartResponse>> GetCartItems()
public async Task<ActionResult<CartDetails>> GetCartItems()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId is null)
return BadRequest();

var cartItems = await _cartService.GetCartItemsAsync(int.Parse(userId));
return _mapper.Map<CartResponse>(cartItems);
return _mapper.Map<CartDetails>(cartItems);
}

[HttpPost]
Expand Down
46 changes: 46 additions & 0 deletions API/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Security.Claims;
using API.Entities;
using API.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers;

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[Route("api/[controller]")]
public class OrderController : ControllerBase
{
private readonly IOrderService _orderService;
private readonly IAddressService _addressService;
private readonly ICartService _cartService;

public OrderController(IOrderService orderService, IAddressService addressService, ICartService cartService)
{
_orderService = orderService;
_addressService = addressService;
_cartService = cartService;
}

[HttpGet]
public Task<IEnumerable<Order>> GetOrders()
{
return _orderService.GetOrders(GetUserId());
}

[HttpPost]
public async Task<ActionResult> CreateOrder(int addressId)
{
await _orderService.CreateOrder(GetUserId(), addressId);
return NoContent();
}


private int GetUserId()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
int.TryParse(userId, out int validUserId);
return validUserId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace API.DTOs.Responses;

public class CartResponse
public class CartDetails
{
public double Total => CartItems.Sum(c => c.Total);
public IEnumerable<CartItemWithTotal> CartItems { get; set; } = new List<CartItemWithTotal>();
Expand Down
Loading

0 comments on commit 0da5d4a

Please sign in to comment.