Some checks failed
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Failing after 20s
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
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();
|
|
}
|
|
}
|
|
} |