CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs
Илья Федотов adaa621ff6 беда
2024-07-24 13:02:15 +04:00

171 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DocumentFormat.OpenXml.Drawing.Diagrams;
using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopBusinessLogic.BusinessLogic;
using ElectronicsShopBusinessLogic.MailWorker;
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Client;
using MigraDoc.Rendering;
namespace ElectronicsShopRestAPI.Controllers {
[Route("api/[controller]/[action]")]
[ApiController]
public class ClientController : Controller {
private readonly ILogger _logger;
private readonly IClientLogic _logic;
private readonly IPaymeantLogic _payLogic;
private readonly IReportClientLogic _reportLogic;
private readonly IOrderLogic _orderLogic;
private readonly AbstractMailWorker _mailWorker;
public ClientController(ILogger<ClientController> logger, IClientLogic logic, IPaymeantLogic payLogic, IReportClientLogic reportlogic,
IOrderLogic orderLogic, AbstractMailWorker mailWorker) {
_logger = logger;
_logic = logic;
_payLogic = payLogic;
_reportLogic = reportlogic;
_orderLogic = orderLogic;
_mailWorker = mailWorker;
}
[HttpGet]
public ClientViewModel? Login(string email, string password) {
try {
return _logic.ReadElemet(new ClientSearchModel {
Email = email,
Passwrod = password
});
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpPost]
public void Register(ClientBindingModel model) {
try {
_logic.Create(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(ClientBindingModel model) {
try {
_logic.Update(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpPost]
public void CreatePaymeant (PaymeantBindingModel model) {
try {
var products = _orderLogic.ReadElement(new OrderSearchModel { ID = model.OrderID })?.ProductList;
if (products == null) {
throw new Exception("Ошибка получения товаров");
}
model.PayProductList = products;
_payLogic.CreatePay(model);
if (model.PayOption == 0) {
_orderLogic.Delete(new OrderBindingModel { ID = model.OrderID});
}
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания оплаты");
}
}
[HttpGet]
public List<PaymeantViewModel>? GetPaymeants(int _clientID) {
try {
return _payLogic?.ReadList(new PaymeantSearchModel { ClientID = _clientID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения списка оплат клиента id = {_clientID}");
throw;
}
}
[HttpGet]
public List<ReportPaymeantsViewModel>? GetReport(string _start,string _end) {
try {
var dataSource = _reportLogic.GetPaymeants(new ReportBindingModel {
DateFrom = DateTime.Parse(_start),
DateTo = DateTime.Parse(_end)
});
return dataSource;
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных");
throw;
}
}
[HttpGet]
public byte[]? CreateXlsxReport (int _clientID) {
try {
var document = _reportLogic.SavePaymeantToExcelFile (_clientID);
return document;
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка создания файла");
throw;
}
}
[HttpGet]
public byte[]? CreateDocxReport (int _clientID) {
try {
var document = _reportLogic.SavePaymeantToWordFile (_clientID);
return document;
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка создания файла");
throw;
}
}
[HttpPost]
public void SendReportMail (ReportBindingModel model) {
try {
var doc = _reportLogic.SaveProductToPdfFile(model);
MemoryStream stream = new MemoryStream();
doc.Save(stream, true);
byte[] data = stream.ToArray();
_mailWorker.MailSendAsync(new() {
MailAddress = model.ClientEmail,
Subject = "Отчет",
Text = $"Отчет оплаченных товаров с {model.DateFrom} по {model.DateTo}",
document = data
});
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка создания файла");
throw;
}
}
}
}