PIbd-22_Chernyshev_Shabunov.../ComputerShopRestApi/Controllers/OrderController.cs

102 lines
2.1 KiB
C#

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class OrderController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _logic;
public OrderController(IOrderLogic logic, ILogger<OrderController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public OrderViewModel? GetOrder(int id)
{
try
{
return _logic.ReadElement(new OrderSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения заказа");
throw;
}
}
/// <summary>
/// Получение заказов по id пользователя (полный список, кот. будет выводиться)
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
public List<OrderViewModel>? GetOrders(int? userId)
{
try
{
return _logic.ReadList(new OrderSearchModel { UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", userId);
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
[HttpPost]
public void UpdateOrder(OrderBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления заказа");
throw;
}
}
[HttpDelete]
public void DeleteOrder(OrderBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
}
}