101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
using BusinessLogic.BusinessLogic;
|
|
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicContracts;
|
|
using Contracts.Exceptions;
|
|
using Contracts.SearchModels;
|
|
using Contracts.ViewModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace RestAPI.Controllers
|
|
{
|
|
[Route("[controller]/[action]")]
|
|
[ApiController]
|
|
public class SaleController : Controller
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly ISaleLogic _saleLogic;
|
|
private readonly IProductLogic _productLogic;
|
|
|
|
public SaleController(ILogger<SaleController> logger, ISaleLogic saleLogic, IProductLogic productLogic)
|
|
{
|
|
_logger = logger;
|
|
_saleLogic = saleLogic;
|
|
_productLogic = productLogic;
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<SaleViewModel>? GetList(string? category)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(category))
|
|
return _saleLogic.ReadElements(null);
|
|
|
|
else
|
|
return _saleLogic.ReadElements(new SaleSearchModel()
|
|
{
|
|
Category = category
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка акций");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public SaleViewModel GetSale(Guid id)
|
|
{
|
|
try
|
|
{
|
|
return _saleLogic.ReadElement(new SaleSearchModel() { Id = id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения акции");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public SaleViewModel GetSaleByProduct(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var product = _productLogic.ReadElement(new ProductSearchModel() { Id = id });
|
|
if (product.SaleId == null)
|
|
{
|
|
return null;
|
|
}
|
|
return _saleLogic.ReadElement(new SaleSearchModel() { Id = product.SaleId });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения акции");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IResult CreateSale([FromBody] SaleBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
var res = _saleLogic.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 sale");
|
|
return Results.Problem(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|