2024-06-24 16:04:51 +04:00
|
|
|
|
using BusinessLogic.BusinessLogic;
|
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.BusinessLogicContracts;
|
|
|
|
|
using Contracts.Exceptions;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace RestAPI.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
2024-06-25 15:03:16 +04:00
|
|
|
|
public class ProductController
|
2024-06-24 16:04:51 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IProductLogic _product;
|
|
|
|
|
|
2024-06-25 15:03:16 +04:00
|
|
|
|
public ProductController(ILogger<ProductController> logger, IProductLogic productLogic)
|
2024-06-24 16:04:51 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_product = productLogic;
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
2024-06-25 20:26:34 +04:00
|
|
|
|
public List<ProductViewModel>? GetList(string? search, double? pricefrom, double? priceto)
|
2024-06-24 17:13:06 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-25 20:26:34 +04:00
|
|
|
|
if (search == null && pricefrom == null && priceto == null)
|
|
|
|
|
return _product.ReadList(null);
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
return _product.ReadList(new ProductSearchModel()
|
|
|
|
|
{
|
|
|
|
|
Name = search,
|
|
|
|
|
PriceFrom = pricefrom,
|
|
|
|
|
PriceTo = priceto
|
|
|
|
|
});
|
2024-06-24 17:13:06 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка продуктов");
|
2024-06-24 16:04:51 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 08:04:07 +04:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public ProductViewModel GetProduct(Guid id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _product.ReadElement(new ProductSearchModel() {Id = id});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения продукта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 16:04:51 +04:00
|
|
|
|
[HttpPatch]
|
|
|
|
|
public IResult Update([FromBody] ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var res = _product.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 user");
|
|
|
|
|
return Results.Problem(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public IResult Delete(Guid id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var res = _product.Delete(new() { Id = id });
|
|
|
|
|
return Results.Ok(res);
|
|
|
|
|
}
|
|
|
|
|
catch (ElementNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation(ex, "User not found");
|
|
|
|
|
return Results.NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Error delete user");
|
|
|
|
|
return Results.Problem(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|