PIbd-22_Chernyshev_Shabunov.../ComputerShopRestApi/Controllers/OrderController.cs
2024-05-29 19:01:27 +04:00

168 lines
3.8 KiB
C#

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using ComputerShopBusinessLogic.MailWorker;
using ComputerShopDatabaseImplement.Models;
using ComputerShopDataModels.Enums;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class OrderController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _logic;
private readonly IReportImplementerLogic _reportLogic;
private readonly AbstractMailWorker _mailWorker;
public OrderController(IOrderLogic logic, ILogger<OrderController> logger, IReportImplementerLogic reportLogic, AbstractMailWorker mailWorker)
{
_logger = logger;
_logic = logic;
_reportLogic = reportLogic;
_mailWorker = mailWorker;
}
[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 CreateReportToWordFile(ReportBindingModel model)
{
try
{
_reportLogic.SaveReportOrderAssembliesToWordFile(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания отчета");
throw;
}
}
[HttpPost]
public void CreateReportToExcelFile(ReportBindingModel model)
{
try
{
_reportLogic.SaveReportOrderAssembliesToExcelFile(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания отчета");
throw;
}
}
[HttpPost]
public void CreateReportToPDFFile(ReportBindingModel model)
{
try
{
_reportLogic.SaveReportOrdersByDatesToPdfFile(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания отчета");
throw;
}
}
[HttpPost]
public void SendPDFToMail(MailSendInfoBindingModel model)
{
try
{
_mailWorker.MailSendAsync(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отправки письма");
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;
}
}
[HttpPost]
public void DeleteOrder(OrderBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
}
}