PIAPS_CW/RestAPI/Controllers/CartItemController.cs
Аришина) d402b05d5f 1
2024-11-24 19:51:40 +04:00

138 lines
4.3 KiB
C#

using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Exceptions;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers
{
[Route("[controller]/[action]")]
[ApiController]
public class CartItemController : Controller
{
private readonly ICartItemLogic _cartItemLogic;
private readonly IProductLogic _productLogic;
private readonly ILogger _logger;
public CartItemController(ILogger<CartItemController> logger, ICartItemLogic cartItemLogic, IProductLogic productLogic)
{
_logger = logger;
_cartItemLogic = cartItemLogic;
_productLogic = productLogic;
}
[HttpGet]
public List<CartItemViewModel> GetFullList(Guid userId)
{
try
{
return _cartItemLogic.ReadElements(new CartItemSearchModel(){ UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка");
throw;
}
}
[HttpGet]
public List<CartItemViewModel> GetFilteredList(Guid userId, bool? isclosed)
{
try
{
return _cartItemLogic.ReadElements(new CartItemSearchModel() { UserId = userId, isClosed = isclosed });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка");
throw;
}
}
[HttpGet]
public CartItemViewModel GetElement(Guid id)
{
try
{
return _cartItemLogic.ReadElement(new CartItemSearchModel() { Id = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения элемента корзины");
throw;
}
}
[HttpGet]
public ProductViewModel GetProduct(Guid cartItemId)
{
try
{
var item = _cartItemLogic.ReadElement(new CartItemSearchModel() { Id = cartItemId });
return _productLogic.ReadElement(new ProductSearchModel() { Id = item.ProductId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта");
throw;
}
}
[HttpPost]
public IResult Create([FromBody] CartItemBindingModel model)
{
try
{
var res = _cartItemLogic.Create(model);
return Results.Ok(res);
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong data");
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error create item");
return Results.Problem(ex.Message);
}
}
[HttpPatch]
public IResult Update([FromBody] CartItemBindingModel model)
{
try
{
var res = _cartItemLogic.Update(model);
return Results.Ok(res);
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong update data");
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error update cart item");
return Results.Problem(ex.Message);
}
}
[HttpDelete]
public IResult Delete(Guid id)
{
try
{
var res = _cartItemLogic.Delete(new() { Id = id });
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
{
_logger.LogInformation(ex, "Item not found");
return Results.NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error delete item");
return Results.Problem(ex.Message);
}
}
}
}