2024-07-26 02:46:29 +04:00
|
|
|
|
using BusinessLogic.BusinessLogic;
|
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.BusinessLogicContracts;
|
|
|
|
|
using Contracts.Exceptions;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.ViewModels;
|
2024-11-24 19:51:40 +04:00
|
|
|
|
using DatabaseImplement.Models;
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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]
|
2024-11-24 19:51:40 +04:00
|
|
|
|
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]
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|