using BankYouBankruptClientApp.Models; using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.ViewModels; using BankYouBankruptContracts.ViewModels.Client.Default; using BankYouBankruptContracts.ViewModels.Client.Reports; using BankYouBankruptContracts.ViewModels.Client.Diagram; using BankYouBankruptDataModels.Enums; using BankYouBankruptСlientApp; using Microsoft.AspNetCore.Mvc; using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using System.Collections; using System.Diagnostics; using System.Reflection; using System.Xml.Linq; using static System.Net.Mime.MediaTypeNames; namespace BankYouBankruptClientApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } #region Профиль, вход и регистрация [HttpGet] public IActionResult Profile() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest($"api/Client/GetClient?clientId={APIClient.Client.Id}")); } [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.Client = APIClient.GetRequest($"api/Client/Login?login={login}&password={password}"); if (APIClient.Client == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Profile"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string login, string password, string name, string surname, string patronymic, string telephone) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(patronymic) || string.IsNullOrEmpty(telephone)) { throw new Exception("Введите логин, пароль, ФИО и телефон"); } APIClient.PostRequest("api/Client/Register", new ClientBindingModel { Name = name, Surname = surname, Patronymic = patronymic, Email = login, Password = password, Telephone = telephone }); Response.Redirect("Enter"); return; } #endregion #region Карты [HttpGet] public IActionResult CardsList() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}")); } [HttpGet] public IActionResult CreateCard() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Accounts = APIClient.GetRequest>($"api/Account/SearchAccountsOfCLient?clientId={APIClient.Client.Id}"); return View(); } [HttpPost] public IActionResult CreateCard(string accountId, string number, string cvc, string period) { if (APIClient.Client == null) { throw new Exception("Не авторизованы"); } APIClient.PostRequest("api/Card/CreateCard", new CardBindingModel { ClientID = APIClient.Client.Id, AccountId = int.Parse(accountId), Number = number, CVC = cvc, Period = DateTime.Parse(period) }); return Redirect("~/Home/CardsList"); } #endregion #region Снятие средств [HttpGet] public IActionResult DebitingList() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/Client/getUsersDebitings?userId={APIClient.Client.Id}")); } [HttpGet] public IActionResult CreateDebiting() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Cards = APIClient.GetRequest>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}"); return View(); } [HttpPost] public IActionResult CreateDebiting(string cardId, int sum) { if (APIClient.Client == null) { throw new Exception("Не авторизованы"); } APIClient.PostRequest("api/Card/CreateDebitingRequest", new DebitingBindingModel() { CardId = int.Parse(cardId), Sum = sum, DateOpen = DateTime.Now, Status = StatusEnum.Открыта }); return Redirect("~/Home/DebitingList"); } #endregion #region Пополнение средств [HttpGet] public IActionResult CreditingList() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(APIClient.GetRequest>($"api/Client/getUsersCreditings?userId={APIClient.Client.Id}")); } [HttpGet] public IActionResult CreateCrediting() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Cards = APIClient.GetRequest>($"api/Card/GetAllCards").Select(x => new ClientSelectViewModel { Id = x.Id, FullName = x.ClientSurname + " " + x.Number.ToString() }).ToList(); return View(); } [HttpPost] public IActionResult CreateCrediting(string cardId, int sum) { if (APIClient.Client == null) { throw new Exception("Не авторизованы"); } APIClient.PostRequest("api/Card/CreateCreditingOperation", new CreditingBindingModel() { CardId = int.Parse(cardId), Sum = sum, DateOpen = DateTime.Now, Status = StatusEnum.Открыта }); return Redirect("~/Home/CreditingList"); } #endregion #region Получение отчёта PDF [HttpGet] public IActionResult CreateReport() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.DataOfClientReport = null; return View(); } [HttpPost] public IActionResult CreateReport(DateTime dateFrom, DateTime dateTo) { if (APIClient.Client == null) { throw new Exception("Не авторизованы"); } APIClient.PostRequest("api/Report/CreateClientReport", new ReportSupportBindingModel() { DateFrom = dateFrom, DateTo = dateTo }); ViewBag.DataOfClientReport = APIClient.GetRequest($"api/Report/GetDataOfClientReport"); return View(); } #endregion #region Получение отчета по картам [HttpGet] public IActionResult ReportWithCards() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } return View(new ReportClientCardsViewModel() { Cards = APIClient.GetRequest>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}").Select(x => new CheckboxViewModel() { Id = x.Id, LabelName = x.Number, IsChecked = false }).ToList() }); } [HttpPost] public IActionResult ReportWithCards(List cards) { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } List cardList = cards.Where(x => x.IsChecked).Select(x => x.Id).ToList(); List creditings = APIClient.GetRequest>($"api/Client/getUsersCreditings?userId={APIClient.Client.Id}") .Where(x => cardList.Contains(x.CardId)).Select(x => new ReportViewModel() { Id = x.Id, CardId = x.CardId, DateOpen = x.DateOpen, DateClose = x.DateClose, CardNumber = x.CardNumber, Status = x.Status, Sum = x.Sum, TypeOperation = TypeOperationEnum.Пополнение }).ToList(); List debitings = APIClient.GetRequest>($"api/Client/getUsersDebitings?userId={APIClient.Client.Id}") .Where(x => cardList.Contains(x.CardId)).Select(x => new ReportViewModel() { Id = x.Id, CardId = x.CardId, DateOpen = x.DateOpen, DateClose = x.DateClose, CardNumber = x.CardNumber, Status = x.Status, Sum = x.Sum, TypeOperation = TypeOperationEnum.Снятие }).ToList(); List result = creditings.Concat(debitings).OrderBy(x => x.DateOpen).ToList(); return View(new ReportClientCardsViewModel() { Cards = cards, Operations = result, }); } #endregion [HttpGet] public IActionResult Diagram() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Cards = APIClient.GetRequest>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}"); return View(); } [HttpPost] public IActionResult Diagram(int cardId) { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } ViewBag.Cards = APIClient.GetRequest>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}"); return View(new ClientDiagramViewModel() { DiagramName = "Hello World", Elements = APIClient.GetRequest>($"api/Card/getCardMonthResult?cardId={cardId}") }); } } }