PIAPS_CW/RestAPI/Controllers/MainController.cs

79 lines
2.2 KiB
C#

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]
public class MainController
{
private readonly ILogger _logger;
private readonly IProductLogic _product;
public MainController(ILogger<MainController> logger, IProductLogic productLogic)
{
_logger = logger;
_product = productLogic;
}
[HttpGet]
public List<ProductViewModel>? GetProductList()
{
try
{
return _product.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка пакетов");
throw;
}
}
[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);
}
}
}
}