CourseWork_CompShop/ComputerShopProvider/ComputerShopRestApi/Controllers/OrderController.cs

209 lines
5.0 KiB
C#
Raw Permalink Normal View History

2023-05-19 18:26:38 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
2023-12-02 06:42:59 +04:00
using ComputerShopContracts.StorageContracts;
2023-05-19 18:26:38 +04:00
using ComputerShopContracts.ViewModels;
2024-07-26 02:49:24 +04:00
using ComputerShopDatabaseImplement.Implements;
using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Drawing.Charts;
2023-05-19 18:26:38 +04:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-12-02 01:39:39 +04:00
using System.Reflection;
2023-05-19 18:26:38 +04:00
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
2023-05-19 18:26:38 +04:00
public class OrderController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
2023-12-02 06:42:59 +04:00
private readonly IOrderStorage _orderStorage;
2023-05-19 18:26:38 +04:00
2024-07-26 02:49:24 +04:00
public OrderController(ILogger<MainController> logger, IOrderLogic order, ISupplyLogic supply, IOrderStorage orderStorage)
2023-05-19 18:26:38 +04:00
{
_logger = logger;
_order = order;
2024-07-26 02:49:24 +04:00
_orderStorage = orderStorage;
2023-05-19 18:26:38 +04:00
}
[HttpGet]
public List<OrderViewModel>? GetOrderList(int clientId)
2023-05-19 18:26:38 +04:00
{
2023-05-19 18:26:38 +04:00
try
{
return _order.ReadList(new OrderSearchModel { ClientId = clientId });
2023-05-19 18:26:38 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов");
2023-05-19 18:26:38 +04:00
throw;
}
}
2024-07-26 02:49:24 +04:00
[HttpGet]
public List<OrderViewModel>? GetOrder(int id)
{
try
{
return _order.ReadList(new OrderSearchModel { Id = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения заказа");
throw;
}
}
[HttpGet]
2023-05-19 18:26:38 +04:00
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _order.ReadList(new OrderSearchModel {});
2023-05-19 18:26:38 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", clientId);
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_order.CreateOrder(model);
2023-05-19 18:26:38 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
2024-07-26 02:49:24 +04:00
[HttpPost]
public void DeleteOrder(OrderBindingModel model)
{
try
{
_order.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении заказа");
throw;
}
}
2023-12-02 01:39:39 +04:00
2023-12-02 06:42:59 +04:00
[HttpPost]
public void FinishOrder(int id)
{
try
{
var order = _orderStorage.GetElement(new() { Id = id });
OrderBindingModel model = new OrderBindingModel()
{
Id = id,
ClientId = order.ClientId,
DateImplement = order.DateImplement,
Status = order.Status
};
_order.FinishOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка изменения закупки");
throw;
}
}
[HttpPost]
public void DeliveryOrder(int id)
{
try
{
var order = _orderStorage.GetElement(new() { Id = id });
OrderBindingModel model = new OrderBindingModel()
{
Id = id,
ClientId = order.ClientId,
DateImplement = order.DateImplement,
Status = order.Status
};
_order.DeliveryOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка изменения закупки");
throw;
}
}
[HttpPost]
public void TakeOrderInWork(int id)
{
try
{
var order = _orderStorage.GetElement(new() { Id = id });
2024-07-26 02:49:24 +04:00
2023-12-02 06:42:59 +04:00
OrderBindingModel model = new OrderBindingModel()
{
Id = id,
ClientId = order.ClientId,
DateImplement = order.DateImplement,
Status = order.Status
};
_order.TakeOrderInWork(model);
}
catch (Exception ex)
{
2024-07-26 02:49:24 +04:00
_logger.LogError(ex, "Ошибка изменения получения техники");
2023-12-02 06:42:59 +04:00
throw;
}
}
2023-12-02 01:39:39 +04:00
[HttpPost]
2024-07-26 02:49:24 +04:00
public void AddComponent(Tuple<OrderSearchModel, ComponentSearchModel, int> model)
2023-12-02 01:39:39 +04:00
{
try
{
2024-07-26 02:49:24 +04:00
_order.AddComponent(model.Item1, model.Item2, model.Item3);
2023-12-02 01:39:39 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления компонента в сборку.");
throw;
}
}
2024-07-26 02:49:24 +04:00
[HttpPost]
public void EditOrder(OrderBindingModel model)
{
try
{
_order.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления заказа.");
throw;
}
}
[HttpGet]
public ComponentViewModel? GetComponentsByOrder(int orderId)
{
try
{
return _orderStorage.GetComponentsByOrder(new OrderSearchModel(){ Id = orderId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения компонента по orderid={Id}", orderId);
throw;
}
}
2023-12-02 01:39:39 +04:00
}
2023-05-19 18:26:38 +04:00
}