CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs
2023-05-17 01:00:04 +04:00

140 lines
3.9 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 BankYouBankruptClientApp.Models;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptСlientApp;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Xml.Linq;
namespace BankYouBankruptClientApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Index()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<CardViewModel>>($"api/Card/GetUsersCardsList?id={APIClient.Client.Id}"));
}
[HttpGet]
public IActionResult Profile()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
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();
}
[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("Неверный логин/пароль");
}
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;
}
[HttpGet]
public IActionResult CreateCard() {
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public IActionResult CreateCard(string account, 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(account),
Number = number,
CVC = cvc,
Period = DateTime.Parse(period)
});
return Redirect("~/Home/Index");
}
}
}