CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs
2023-05-16 20:05:59 +04:00

103 lines
3.3 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;
}
//вытаскивает через API клиента Get-запросом список его собственных заказов
[HttpGet]
public IActionResult Index()
{
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("Index");
}
//просто открытие вьюхи
[HttpGet]
public IActionResult Register()
{
return View();
}
//Post-запрос по созданию нового пользователя
[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
});
//переход на вкладку "Enter", чтобы пользователь сразу смог зайти
Response.Redirect("Enter");
return;
}
}
}