From 29a5b800d0c166fe398350ea8bc5a69fdc152017 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 17 May 2024 14:55:09 +0400 Subject: [PATCH] fix APIClient --- Bank/BankManagersClientApp/APIClient.cs | 40 ++++++++++++-- .../BankManagersClientApp.csproj | 4 ++ .../Controllers/HomeController.cs | 55 ++++++++++++------- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git a/Bank/BankManagersClientApp/APIClient.cs b/Bank/BankManagersClientApp/APIClient.cs index be68a28..4c4a854 100644 --- a/Bank/BankManagersClientApp/APIClient.cs +++ b/Bank/BankManagersClientApp/APIClient.cs @@ -1,18 +1,48 @@ using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Newtonsoft.Json; using System.Net.Http.Headers; +using System.Text; namespace BankManagersClientApp { public static class APIClient { - private static readonly HttpClient _manager = new(); - public static ManagerViewModel? Manager { get; set; } = null; + private static readonly HttpClient _client = new(); + public static ManagerViewModel? Client { get; set; } = null; + public static void Connect(IConfiguration configuration) { - _manager.BaseAddress = new Uri(configuration["IPAddress"]); - _manager.DefaultRequestHeaders.Accept.Clear(); - _manager.DefaultRequestHeaders.Accept.Add(new + _client.BaseAddress = new Uri(configuration["IPAddress"]); + _client.DefaultRequestHeaders.Accept.Clear(); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } + + 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); + } + } + + 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/Bank/BankManagersClientApp/BankManagersClientApp.csproj b/Bank/BankManagersClientApp/BankManagersClientApp.csproj index d52e37e..0e76faa 100644 --- a/Bank/BankManagersClientApp/BankManagersClientApp.csproj +++ b/Bank/BankManagersClientApp/BankManagersClientApp.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/Bank/BankManagersClientApp/Controllers/HomeController.cs b/Bank/BankManagersClientApp/Controllers/HomeController.cs index 05de9b0..77a0a4f 100644 --- a/Bank/BankManagersClientApp/Controllers/HomeController.cs +++ b/Bank/BankManagersClientApp/Controllers/HomeController.cs @@ -1,4 +1,5 @@ -using BankManagersClientApp.Models; +using BankContracts.ViewModels; +using BankManagersClientApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; @@ -16,14 +17,30 @@ namespace BankManagersClientApp.Controllers #region//работа с аккаунтом public IActionResult Privacy() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } - return View(APIClient.Manager); + return View(APIClient.Client); } - [HttpGet] + [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 Enter() { return View(); @@ -33,7 +50,7 @@ namespace BankManagersClientApp.Controllers #region//работа со счетами public IActionResult Index() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -42,7 +59,7 @@ namespace BankManagersClientApp.Controllers public IActionResult AccountCreate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -51,7 +68,7 @@ namespace BankManagersClientApp.Controllers public IActionResult AccountUpdate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -60,7 +77,7 @@ namespace BankManagersClientApp.Controllers public IActionResult AccountDelete() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -71,7 +88,7 @@ namespace BankManagersClientApp.Controllers #region//работа с переводами public IActionResult Transfers() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -80,7 +97,7 @@ namespace BankManagersClientApp.Controllers public IActionResult TransferCreate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -89,7 +106,7 @@ namespace BankManagersClientApp.Controllers public IActionResult TransferUpdate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -98,7 +115,7 @@ namespace BankManagersClientApp.Controllers public IActionResult TransferDelete() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -109,7 +126,7 @@ namespace BankManagersClientApp.Controllers #region//работа с выдачами public IActionResult Withdrawals() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -118,7 +135,7 @@ namespace BankManagersClientApp.Controllers public IActionResult WithdrawalCreate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -127,7 +144,7 @@ namespace BankManagersClientApp.Controllers public IActionResult WithdrawalUpdate() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -136,7 +153,7 @@ namespace BankManagersClientApp.Controllers public IActionResult WithdrawalDelete() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -145,7 +162,7 @@ namespace BankManagersClientApp.Controllers public IActionResult WithdrawalRequest() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -156,7 +173,7 @@ namespace BankManagersClientApp.Controllers #region//работа с отчётами public IActionResult RequestsListReport() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } @@ -165,7 +182,7 @@ namespace BankManagersClientApp.Controllers public IActionResult TransfersWithdrawalsListReport() { - if (APIClient.Manager == null) + if (APIClient.Client == null) { return Redirect("~/Home/Enter"); }