299 lines
12 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 BankBusinessLogic.MailWorker;
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using BankDataModels.Models;
using MailKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata;
using OperatorApp.Models;
using System.Diagnostics;
namespace OperatorApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IDealLogic _dealLogic;
private readonly IPaymentLogic _paymentLogic;
private readonly ITransferLogic _transferLogic;
private readonly IOperatorLogic _operatorLogic;
private readonly IReportLogic _reportLogic;
private readonly AbstractMailWorker _mailWorker;
public HomeController(ILogger<HomeController> logger, IDealLogic dealLogic, IPaymentLogic paymentLogic, ITransferLogic transferLogic, IOperatorLogic operatorLogic, IReportLogic reportLogic, AbstractMailWorker mailWorker)
{
_logger = logger;
_dealLogic = dealLogic;
_paymentLogic = paymentLogic;
_transferLogic = transferLogic;
_operatorLogic = operatorLogic;
_reportLogic = reportLogic;
_mailWorker = mailWorker;
}
public IActionResult Index()
{
if (APIClient.Operator == null)
{
return Redirect("~/Home/Enter");
}
return View(_dealLogic.ReadList(new DealSearchModel { OperatorId = APIClient.Operator.Id }));
}
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Operator == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Operator);
}
[HttpPost]
public void Privacy(string login, string password, string lastname, string firstname, string middleName)
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(lastname) || string.IsNullOrEmpty(firstname) || string.IsNullOrEmpty(middleName))
{
throw new Exception("Введите логин, пароль и ФИО");
}
_operatorLogic.Update(new OperatorBindingModel
{
Id = APIClient.Operator.Id,
LastName = lastname,
FirstName = firstname,
MiddleName = middleName,
Login = login,
Password = password
});
APIClient.Operator.LastName = lastname;
APIClient.Operator.FirstName = firstname;
APIClient.Operator.MiddleName = middleName;
APIClient.Operator.Login = login;
APIClient.Operator.Password = password;
Response.Redirect("Index");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Operator = _operatorLogic.ReadElement(new OperatorSearchModel { Login = login, Password = password });
if (APIClient.Operator == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string password, string lastname, string firstname, string middleName)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(lastname) || string.IsNullOrEmpty(firstname) || string.IsNullOrEmpty(middleName))
{
throw new Exception("Введите логин, пароль и ФИО");
}
_operatorLogic.Create(new OperatorBindingModel
{
LastName = lastname,
FirstName = firstname,
MiddleName = middleName,
Login = login,
Password = password
});
Response.Redirect("Enter");
return;
}
[HttpGet]
public IActionResult CreateDeal()
{
return View();
}
[HttpPost]
public void CreateDeal(int clientid)
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
_dealLogic.Create(new DealBindingModel
{
ClientId = clientid,
OperatorId = APIClient.Operator.Id,
});
Response.Redirect("Index");
}
public IActionResult Payments()
{
if (APIClient.Operator == null)
{
return Redirect("~/Home/Enter");
}
return View(_paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id }));
}
[HttpGet]
public IActionResult CreatePayment()
{
ViewBag.Deals = _dealLogic.ReadList(new DealSearchModel { OperatorId = APIClient.Operator.Id });
return View();
}
[HttpPost]
public void CreatePayment(List<int> deals)
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
Dictionary<int, IDealModel> DealPayments = new();
foreach (int id in deals)
{
var deal = _dealLogic.ReadElement(new DealSearchModel { Id = id });
if (deal != null) DealPayments.Add(deal.Id, deal);
}
_paymentLogic.Create(new PaymentBindingModel { OperatorId = APIClient.Operator.Id, DealPayments = DealPayments, });
Response.Redirect("Payments");
}
[HttpGet]
public IActionResult Payment(int id)
{
return View(_paymentLogic.ReadElement(new PaymentSearchModel { Id = id }));
}
[HttpGet]
public IActionResult Transfers()
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
return View(_transferLogic.ReadList(new TransferSearchModel { OperatorId = APIClient.Operator.Id }));
}
[HttpGet]
public IActionResult CreateTransfer()
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id });
return View();
}
[HttpPost]
public void CreateTransfer(string amount, int payment)
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
_transferLogic.Create(new TransferBindingModel { OperatorId = APIClient.Operator.Id, Amount = (float)Convert.ToDouble(amount), PaymentId = payment });
Response.Redirect("Transfers");
}
public void DeleteTransfer(int id)
{
if (APIClient.Operator == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
_transferLogic.Delete(new TransferBindingModel { Id = id });
Response.Redirect("/Home/Transfers");
}
[HttpGet]
public IActionResult PaymentsReport()
{
if (APIClient.Operator == null)
{
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');</script>");
Response.Redirect("/Home/Enter");
}
ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id });
return View();
}
[HttpPost]
public IActionResult PaymentsReport(List<int> payments, bool word, bool excel)
{
List<PaymentBindingModel> paymentBindings = new List<PaymentBindingModel>();
foreach (int paymentId in payments)
{
var paymentView = _paymentLogic.ReadElement(new PaymentSearchModel { Id = paymentId });
if (paymentView != null) paymentBindings.Add(new PaymentBindingModel
{
Id = paymentView.Id,
OperatorId = paymentView.OperatorId,
PaymentDate = paymentView.PaymentDate,
CurrencyPayments = paymentView.CurrencyPayments,
DealPayments = paymentView.DealPayments
});
}
//if (word)
//{
MemoryStream list = _reportLogic.SavePaymentPurchaseToWord(new ReportBindingModel { FileName = "test" }, paymentBindings);
return File(list, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "testDoc.docx");
//}
//else
//{
// MemoryStream list = _reportLogic.SavePaymentPurchaseToExcel(new ReportBindingModel { FileName = "test" }, paymentBindings);
// return File(list, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "testExcel.xlsx");
//}
}
[HttpGet]
public IActionResult TransfersReport()
{
return View(new ReportBindingModel());
}
[HttpPost]
public void TransfersReport(DateTime dateFrom, DateTime dateTo)
{
MemoryStream report = _reportLogic.SaveTransferPurchaseToPDF(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo });
try
{
_mailWorker.MailSendAsync(new MailSendInfoBindingModel
{
Subject = "Отчёт о закупках",
Text = "От банка \"Вы банкрот\"",
MailAddress = "",
FileName = "test.pdf",
Attachment = report
});
Response.Redirect("/");
}
catch (Exception ex)
{
Response.WriteAsync($"<script language=\"javascript\">alert('{ex.Message}');</script>");
Response.Redirect("/");
}
}
}
}