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 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 AbstractMailWorker _mailWorker; public ClientController(ILogger logger, IClientLogic logic, IPaymeantLogic payLogic, IReportClientLogic reportlogic, AbstractMailWorker mailWorker) { _logger = logger; _logic = logic; _payLogic = payLogic; _reportLogic = reportlogic; _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 { _payLogic.CreatePay(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания оплаты"); } } [HttpGet] public List? GetPaymeants(int _clientID) { try { return _payLogic?.ReadList(new PaymeantSearchModel { ClientID = _clientID }); } catch (Exception ex) { _logger.LogError(ex, $"Ошибка получения списка оплат клиента id = {_clientID}"); throw; } } [HttpGet] public List? 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; } } } }