From fa9ade894192ec7100c862b5614eeb34d8531e96 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Sat, 20 May 2023 06:01:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20APIClien?= =?UTF-8?q?t=20=D0=B8=D0=B7=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= =?UTF-8?q?=20"=D0=9F=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D1=89=D0=B8=D0=BA".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bank/BankOperatorApp/APIClient.cs | 74 ------------------- .../Controllers/HomeController.cs | 61 +++++++-------- 2 files changed, 31 insertions(+), 104 deletions(-) delete mode 100644 Bank/BankOperatorApp/APIClient.cs diff --git a/Bank/BankOperatorApp/APIClient.cs b/Bank/BankOperatorApp/APIClient.cs deleted file mode 100644 index ed05568..0000000 --- a/Bank/BankOperatorApp/APIClient.cs +++ /dev/null @@ -1,74 +0,0 @@ -using BankContracts.ViewModels; -using System.Net.Http.Headers; -using System.Text; -using Newtonsoft.Json; - -namespace BankOperatorApp -{ - public class APIClient - { - private static readonly HttpClient _client = new(); - - public static BankOperatorViewModel? BankOperator { 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")); - } - - 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); - } - } - - public static void PatchRequest(string requestUrl, T model) - { - var json = JsonConvert.SerializeObject(model); - var data = new StringContent(json, Encoding.UTF8, "application/json"); - - var response = _client.PatchAsync(requestUrl, data); - - var result = response.Result.Content.ReadAsStringAsync().Result; - if (!response.Result.IsSuccessStatusCode) - { - throw new Exception(result); - } - } - - public static void DeleteRequest(string requestUrl) - { - var response = _client.DeleteAsync(requestUrl); - - var result = response.Result.Content.ReadAsStringAsync().Result; - if (!response.Result.IsSuccessStatusCode) - { - throw new Exception(result); - } - } - } -} diff --git a/Bank/BankOperatorApp/Controllers/HomeController.cs b/Bank/BankOperatorApp/Controllers/HomeController.cs index 99e7adf..b0cb35f 100644 --- a/Bank/BankOperatorApp/Controllers/HomeController.cs +++ b/Bank/BankOperatorApp/Controllers/HomeController.cs @@ -20,6 +20,7 @@ namespace BankOperatorApp.Controllers private readonly ICurrencyPurchaseLogic _currencyPurchaseLogic; private readonly IReportLogic _reportLogic; private readonly AbstractMailWorker _mailWorker; + public static BankOperatorViewModel? BankOperator { get; set; } = null; public HomeController(ILogger logger, IBankOperatorLogic bankOperatorLogic, @@ -37,29 +38,29 @@ namespace BankOperatorApp.Controllers public IActionResult Index() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { return Redirect("~/Home/Enter"); } return View(_currencyLogic.ReadList(new CurrencySearchModel { BankOperatorId = - APIClient.BankOperator.Id })); + HomeController.BankOperator.Id })); } [HttpGet] public IActionResult Privacy() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { return Redirect("~/Home/Enter"); } - return View(APIClient.BankOperator); + return View(HomeController.BankOperator); } [HttpPost] public void Privacy(string login, string password, string lastname, string firstname, string middleName) { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } @@ -69,7 +70,7 @@ namespace BankOperatorApp.Controllers } _bankOperatorLogic.Update(new BankOperatorBindingModel { - Id = APIClient.BankOperator.Id, + Id = HomeController.BankOperator.Id, LastName = lastname, FirstName = firstname, MiddleName = middleName, @@ -77,11 +78,11 @@ namespace BankOperatorApp.Controllers Password = password }); - APIClient.BankOperator.LastName = lastname; - APIClient.BankOperator.FirstName = firstname; - APIClient.BankOperator.MiddleName = middleName; - APIClient.BankOperator.Login = login; - APIClient.BankOperator.Password = password; + HomeController.BankOperator.LastName = lastname; + HomeController.BankOperator.FirstName = firstname; + HomeController.BankOperator.MiddleName = middleName; + HomeController.BankOperator.Login = login; + HomeController.BankOperator.Password = password; Response.Redirect("Index"); } @@ -105,9 +106,9 @@ namespace BankOperatorApp.Controllers { throw new Exception("Введите логин и пароль"); } - APIClient.BankOperator = _bankOperatorLogic.ReadElement + HomeController.BankOperator = _bankOperatorLogic.ReadElement (new BankOperatorSearchModel { Login = login, Password = password }); - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Неверный логин/пароль"); } @@ -150,25 +151,25 @@ namespace BankOperatorApp.Controllers [HttpPost] public void CreateCurrency(string name) { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } _currencyLogic.Create(new CurrencyBindingModel { Name = name, - BankOperatorId = APIClient.BankOperator.Id, + BankOperatorId = HomeController.BankOperator.Id, }); Response.Redirect("Index"); } public IActionResult CreditPrograms() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { return Redirect("~/Home/Enter"); } return View(_creditProgramLogic.ReadList(new CreditProgramSearchModel - { BankOperatorId = APIClient.BankOperator.Id})); + { BankOperatorId = HomeController.BankOperator.Id})); } [HttpGet] public IActionResult CreateCreditProgram() @@ -179,7 +180,7 @@ namespace BankOperatorApp.Controllers [HttpPost] public void CreateCreditProgram(List currencies, string name, float percent) { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } @@ -190,7 +191,7 @@ namespace BankOperatorApp.Controllers if (currency != null) CurrencyCreditPrograms.Add(currency.Id, currency); } _creditProgramLogic.Create(new CreditProgramBindingModel { BankOperatorId = - APIClient.BankOperator.Id, + HomeController.BankOperator.Id, CreditProgramCurrencies = CurrencyCreditPrograms, Name = name, Percent = percent}); Response.Redirect("CreditPrograms"); } @@ -202,17 +203,17 @@ namespace BankOperatorApp.Controllers [HttpGet] public IActionResult CurrencyPurchase() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } return View(_currencyPurchaseLogic.ReadList - (new CurrencyPurchaseSearchModel { BankOperatorId = APIClient.BankOperator.Id })); + (new CurrencyPurchaseSearchModel { BankOperatorId = HomeController.BankOperator.Id })); } [HttpGet] public IActionResult CreateCurrencyPurchase() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } @@ -222,13 +223,13 @@ namespace BankOperatorApp.Controllers [HttpPost] public void CreateCurrencyPurchase(string amount, int currencyId) { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } _currencyPurchaseLogic.Create(new CurrencyPurchaseBindingModel - { BankOperatorId = APIClient.BankOperator.Id, + { BankOperatorId = HomeController.BankOperator.Id, Amount = (float)Convert.ToDouble(amount), CurrencyId = currencyId, @@ -238,18 +239,18 @@ namespace BankOperatorApp.Controllers public IActionResult CurrencyPurchases() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { return Redirect("~/Home/Enter"); } return View(_currencyPurchaseLogic.ReadList(new CurrencyPurchaseSearchModel - { BankOperatorId = APIClient.BankOperator.Id })); + { BankOperatorId = HomeController.BankOperator.Id })); } [HttpGet] public IActionResult CurrencyReport() { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { Response.WriteAsync($""); @@ -307,7 +308,7 @@ namespace BankOperatorApp.Controllers public IActionResult CurrencyPurchasePaymentsReport(DateTime dateFrom, DateTime dateTo, string reptype, string email, string fileName) { - if (APIClient.BankOperator == null) + if (HomeController.BankOperator == null) { Response.WriteAsync($""); @@ -329,7 +330,7 @@ namespace BankOperatorApp.Controllers _mailWorker.MailSendAsync(new MailSendInfoBindingModel { Subject = "Отчёт по закупкам", - Text = "Для оператора " + APIClient.BankOperator.LastName + APIClient.BankOperator.FirstName, + Text = "Для оператора " + HomeController.BankOperator.LastName + HomeController.BankOperator.FirstName, MailAddress = email, FileName = fileName, Attachment = report