From 8ad04498d1df03291867335a5c18602abcf434b5 Mon Sep 17 00:00:00 2001 From: shadowik Date: Tue, 16 May 2023 20:05:59 +0400 Subject: [PATCH] ClientApp init --- .../BankYouBankruptClientApp/APIClient.cs | 55 +++++++++++ .../BankYouBankruptClientApp.csproj | 8 ++ .../Controllers/HomeController.cs | 91 +++++++++++++++++-- .../BankYouBankruptClientApp/Program.cs | 4 + .../Views/Home/Enter.cshtml | 27 ++++++ .../Views/Home/Index.cshtml | 26 +++++- .../Views/Home/Privacy.cshtml | 6 -- .../Views/Home/Register.cshtml | 51 +++++++++++ .../Views/Shared/_Layout.cshtml | 32 +++++-- .../Views/Shared/_Layout.cshtml.css | 41 +++++---- .../BankYouBankruptClientApp/appsettings.json | 3 +- .../BankYouBancruptDatabase.cs | 4 +- .../Controllers/ClientController.cs | 14 +++ 13 files changed, 309 insertions(+), 53 deletions(-) create mode 100644 BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs create mode 100644 BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Enter.cshtml delete mode 100644 BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Privacy.cshtml create mode 100644 BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Register.cshtml diff --git a/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs b/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs new file mode 100644 index 0000000..71b9c51 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptClientApp/APIClient.cs @@ -0,0 +1,55 @@ +using BankYouBankruptContracts.ViewModels; +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; + +namespace BankYouBankruptСlientApp +{ + public class APIClient + { + private static readonly HttpClient _client = new(); + + public static ClientViewModel? Client { get; set; } = null; + + public static void Connect(IConfiguration configuration) + { + _client.BaseAddress = new Uri(configuration["IPAddress"]); + _client.DefaultRequestHeaders.Accept.Clear(); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + //Get-запрос + public static T? GetRequest(string requestUrl) + { + var response = _client.GetAsync(requestUrl); + var result = response.Result.Content.ReadAsStringAsync().Result; + + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + throw new Exception(result); + } + + return JsonConvert.DeserializeObject(""); + } + + //Post-запрос + public static void PostRequest(string requestUrl, T model) + { + var json = JsonConvert.SerializeObject(model); + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requestUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + + if (!response.Result.IsSuccessStatusCode) + { + throw new Exception(result); + } + } + } +} diff --git a/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj b/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj index c78c9c7..5bf8ee5 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj +++ b/BankYouBankrupt/BankYouBankruptClientApp/BankYouBankruptClientApp.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs b/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs index 997dbed..6c15b05 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs +++ b/BankYouBankrupt/BankYouBankruptClientApp/Controllers/HomeController.cs @@ -1,11 +1,16 @@ -using BankYouBankruptClientApp.Models; + +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 - { + public class HomeController : Controller + { private readonly ILogger _logger; public HomeController(ILogger logger) @@ -13,20 +18,86 @@ namespace BankYouBankruptClientApp.Controllers _logger = logger; } + //вытаскивает через API клиента Get-запросом список его собственных заказов + [HttpGet] public IActionResult Index() { - return View(); + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + + return View(APIClient.GetRequest($"api/Client/GetClient?clientId={APIClient.Client.Id}")); } - public IActionResult Privacy() - { - return View(); - } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + 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("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; + } + } } \ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Program.cs b/BankYouBankrupt/BankYouBankruptClientApp/Program.cs index 0727468..d6dc1cf 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Program.cs +++ b/BankYouBankrupt/BankYouBankruptClientApp/Program.cs @@ -1,3 +1,5 @@ +using BankYouBankruptlientApp; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -5,6 +7,8 @@ builder.Services.AddControllersWithViews(); var app = builder.Build(); +APIClient.Connect(builder.Configuration); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Enter.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Enter.cshtml new file mode 100644 index 0000000..8e277d4 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Enter.cshtml @@ -0,0 +1,27 @@ +@{ + ViewData["Title"] = "Enter"; +} + +
+

Вход в приложение

+
+
+
+
Логин:
+
+ +
+
+
Пароль:
+
+ +
+
+
+
+
+ +
+
+ \ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Index.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Index.cshtml index d2d19bd..7ab417c 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Index.cshtml +++ b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Index.cshtml @@ -1,8 +1,26 @@ -@{ - ViewData["Title"] = "Home Page"; +@using BankYouBankruptContracts.ViewModels + +@model ClientViewModel + +@{ + ViewData["Title"] = "Home Page"; }
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

Страница пользователя

+ +
+ @{ + if (Model == null) + { +

Сначала авторизируйтесь

+ return; + } + +

Здравствуйтe, @Model.Name @Model.Patronymic

+

+ Открыть счёт +

+ } +
\ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Privacy.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Privacy.cshtml deleted file mode 100644 index af4fb19..0000000 --- a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Privacy.cshtml +++ /dev/null @@ -1,6 +0,0 @@ -@{ - ViewData["Title"] = "Privacy Policy"; -} -

@ViewData["Title"]

- -

Use this page to detail your site's privacy policy.

diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Register.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Register.cshtml new file mode 100644 index 0000000..35db21b --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptClientApp/Views/Home/Register.cshtml @@ -0,0 +1,51 @@ +@{ + ViewData["Title"] = "Register"; +} + +
+

Регистрация

+
+
+
+
Логин:
+
+ +
+
+
+
Пароль:
+
+ +
+
+
+
Имя:
+
+ +
+
+
+
Фамилия:
+
+ +
+
+
+
Отчество:
+
+ +
+
+
+
Телефон:
+
+ +
+
+
+
+
+ +
+
+
\ No newline at end of file diff --git a/BankYouBankrupt/BankYouBankruptClientApp/Views/Shared/_Layout.cshtml b/BankYouBankrupt/BankYouBankruptClientApp/Views/Shared/_Layout.cshtml index 2f79c11..dda2b99 100644 --- a/BankYouBankrupt/BankYouBankruptClientApp/Views/Shared/_Layout.cshtml +++ b/BankYouBankrupt/BankYouBankruptClientApp/Views/Shared/_Layout.cshtml @@ -5,25 +5,37 @@ @ViewData["Title"] - BankYouBankruptClientApp - - + + +