fix APIClient

This commit is contained in:
Zakharov_Rostislav 2024-05-17 14:55:09 +04:00
parent 62d814e5d0
commit 29a5b800d0
3 changed files with 75 additions and 24 deletions

View File

@ -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<T>(string requestUrl)
{
var response = _client.GetAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (response.Result.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(result);
}
else
{
throw new Exception(result);
}
}
public static void PostRequest<T>(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);
}
}
}
}

View File

@ -6,6 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BankContracts\BankContracts.csproj" />
<ProjectReference Include="..\BankRestApi\BankRestApi.csproj" />

View File

@ -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<ClientViewModel>($"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");
}