cart
Some checks failed
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Failing after 20s
Some checks failed
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Failing after 20s
This commit is contained in:
50
Webshop.Api/Controllers/Customers/CartController.cs
Normal file
50
Webshop.Api/Controllers/Customers/CartController.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Customers;
|
||||
using Webshop.Application.DTOs.Shipping;
|
||||
using Webshop.Application.Services.Customers.Interfaces;
|
||||
|
||||
namespace Webshop.Api.Controllers.Customer
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/customer/[controller]")]
|
||||
[Authorize(Roles = "Customer")]
|
||||
public class CartController : ControllerBase
|
||||
{
|
||||
private readonly ICartService _cartService;
|
||||
|
||||
public CartController(ICartService cartService)
|
||||
{
|
||||
_cartService = cartService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetCart()
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var result = await _cartService.GetCartAsync(userId!);
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
[HttpPost("items")]
|
||||
public async Task<IActionResult> AddToCart([FromBody] CartItemDto item)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var result = await _cartService.AddToCartAsync(userId!, item);
|
||||
return result.Type == ServiceResultType.Success ? Ok() : BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpDelete("items/{productId}")]
|
||||
public async Task<IActionResult> RemoveItem(Guid productId)
|
||||
{
|
||||
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _cartService.RemoveFromCartAsync(userId!, productId);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user