CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs

243 lines
7.0 KiB
C#
Raw Normal View History

2023-05-16 20:05:59 +04:00

using BankYouBankruptClientApp.Models;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.ViewModels;
2023-05-17 15:18:35 +04:00
using BankYouBankruptDataModels.Enums;
2023-05-16 20:05:59 +04:00
using BankYouBankruptСlientApp;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
2023-05-16 20:05:59 +04:00
using System.Xml.Linq;
namespace BankYouBankruptClientApp.Controllers
{
2023-05-16 20:05:59 +04:00
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
2023-05-17 01:00:04 +04:00
[HttpGet]
public IActionResult Profile()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2023-05-16 20:05:59 +04:00
return View(APIClient.GetRequest<ClientViewModel>($"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();
}
2023-05-17 01:00:04 +04:00
2023-05-16 20:05:59 +04:00
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Client = APIClient.GetRequest<ClientViewModel>($"api/Client/Login?login={login}&password={password}");
if (APIClient.Client == null)
{
throw new Exception("Неверный логин/пароль");
}
2023-05-17 01:00:04 +04:00
Response.Redirect("Profile");
2023-05-16 20:05:59 +04:00
}
2023-05-17 01:00:04 +04:00
2023-05-16 20:05:59 +04:00
[HttpGet]
public IActionResult Register()
{
return View();
}
2023-05-17 01:00:04 +04:00
2023-05-16 20:05:59 +04:00
[HttpPost]
public void Register(string login, string password, string name, string surname, string patronymic, string telephone)
{
2023-05-16 20:05:59 +04:00
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;
}
2023-05-17 01:00:04 +04:00
2023-05-17 14:27:46 +04:00
#region Карты
[HttpGet]
public IActionResult CardsList()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<CardViewModel>>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}"));
}
2023-05-17 01:00:04 +04:00
[HttpGet]
public IActionResult CreateCard() {
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2023-05-17 15:18:35 +04:00
ViewBag.Accounts = APIClient.GetRequest<List<AccountViewModel>>($"api/Account/SearchAccountsOfCLient?clientId={APIClient.Client.Id}");
2023-05-17 01:00:04 +04:00
return View();
}
[HttpPost]
2023-05-17 15:18:35 +04:00
public IActionResult CreateCard(string accountId, string number, string cvc, string period) {
2023-05-17 01:00:04 +04:00
if (APIClient.Client == null)
{
throw new Exception("Не авторизованы");
}
APIClient.PostRequest("api/Card/CreateCard", new CardBindingModel {
ClientID = APIClient.Client.Id,
2023-05-17 15:18:35 +04:00
AccountId = int.Parse(accountId),
2023-05-17 01:00:04 +04:00
Number = number,
CVC = cvc,
Period = DateTime.Parse(period)
});
2023-05-17 14:27:46 +04:00
return Redirect("~/Home/CardsList");
}
#endregion
2023-05-17 15:18:35 +04:00
#region Снятие средств
2023-05-17 14:27:46 +04:00
[HttpGet]
public IActionResult DebitingList()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<DebitingViewModel>>($"api/Client/getUsersDebitings?userId={APIClient.Client.Id}"));
2023-05-17 01:00:04 +04:00
}
2023-05-17 15:18:35 +04:00
[HttpGet]
public IActionResult CreateDebiting()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Cards = APIClient.GetRequest<List<CardViewModel>>($"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<List<CreditingViewModel>>($"api/Client/getUsersCreditings?userId={APIClient.Client.Id}"));
}
2023-05-17 15:24:20 +04:00
[HttpGet]
public IActionResult CreateCrediting()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
2023-05-17 16:46:18 +04:00
ViewBag.Cards = APIClient.GetRequest<List<CardViewModel>>($"api/Card/GetAllCards").Select(x => new ClientCardViewModel
{
Id = x.Id,
FullName = x.ClientSurname + " " +x.Number.ToString()
}).ToList();
2023-05-17 15:24:20 +04:00
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");
}
2023-05-17 15:18:35 +04:00
#endregion
2023-05-17 01:00:04 +04:00
}
}