From 84a1e0d0fcf5fcc62e504801ab0d6680ed83ea97 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:13:22 +0400 Subject: [PATCH] =?UTF-8?q?APIClient=20+=20=D1=84=D0=B8=D0=BA=D1=81=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20=D1=83=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BankRestApi/Controllers/DealController.cs | 6 +- .../Controllers/OperatorController.cs | 6 +- .../Controllers/PaymentController.cs | 6 +- .../Controllers/TransferController.cs | 6 +- Bank/OperatorApp/APIClientcs.cs | 74 +++++++++++++++++++ Bank/OperatorApp/OperatorApp.csproj | 10 ++- Bank/OperatorApp/appsettings.json | 3 +- 7 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 Bank/OperatorApp/APIClientcs.cs diff --git a/Bank/BankRestApi/Controllers/DealController.cs b/Bank/BankRestApi/Controllers/DealController.cs index cd98986..8c3281d 100644 --- a/Bank/BankRestApi/Controllers/DealController.cs +++ b/Bank/BankRestApi/Controllers/DealController.cs @@ -73,10 +73,14 @@ namespace BankRestApi.Controllers } } [HttpDelete] - public void DeleteDeal(DealBindingModel model) + public void DeleteDeal(int dealId) { try { + DealBindingModel model = new DealBindingModel + { + Id = dealId, + }; _deal.Delete(model); } catch (Exception ex) diff --git a/Bank/BankRestApi/Controllers/OperatorController.cs b/Bank/BankRestApi/Controllers/OperatorController.cs index 7999bb9..941fd99 100644 --- a/Bank/BankRestApi/Controllers/OperatorController.cs +++ b/Bank/BankRestApi/Controllers/OperatorController.cs @@ -47,10 +47,14 @@ namespace BankRestApi.Controllers } } [HttpDelete] - public void DeleteOperator(OperatorBindingModel model) + public void DeleteOperator(int operatorId) { try { + OperatorBindingModel model = new OperatorBindingModel + { + Id = operatorId + }; _operator.Delete(model); } catch (Exception ex) diff --git a/Bank/BankRestApi/Controllers/PaymentController.cs b/Bank/BankRestApi/Controllers/PaymentController.cs index 3afbf5a..29d6deb 100644 --- a/Bank/BankRestApi/Controllers/PaymentController.cs +++ b/Bank/BankRestApi/Controllers/PaymentController.cs @@ -73,10 +73,14 @@ namespace BankRestApi.Controllers } } [HttpDelete] - public void DeletePayment(PaymentBindingModel model) + public void DeletePayment(int paymentId) { try { + PaymentBindingModel model = new PaymentBindingModel + { + Id = paymentId + }; _payment.Delete(model); } catch (Exception ex) diff --git a/Bank/BankRestApi/Controllers/TransferController.cs b/Bank/BankRestApi/Controllers/TransferController.cs index a538f44..2d6a06b 100644 --- a/Bank/BankRestApi/Controllers/TransferController.cs +++ b/Bank/BankRestApi/Controllers/TransferController.cs @@ -74,10 +74,14 @@ namespace BankRestApi.Controllers } } [HttpDelete] - public void DeleteTransfer(TransferBindingModel model) + public void DeleteTransfer(int transferId) { try { + TransferBindingModel model = new TransferBindingModel + { + Id = transferId + }; _transfer.Delete(model); } catch (Exception ex) diff --git a/Bank/OperatorApp/APIClientcs.cs b/Bank/OperatorApp/APIClientcs.cs new file mode 100644 index 0000000..2e78dac --- /dev/null +++ b/Bank/OperatorApp/APIClientcs.cs @@ -0,0 +1,74 @@ +using System.Net.Http.Headers; +using System.Text; +using BankContracts.ViewModels; +using Newtonsoft.Json; + +namespace OperatorApp +{ + public class APIClientcs + { + private static readonly HttpClient _client = new(); + + public static OperatorViewModel? Operator { 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/OperatorApp/OperatorApp.csproj b/Bank/OperatorApp/OperatorApp.csproj index c78c9c7..b5167a1 100644 --- a/Bank/OperatorApp/OperatorApp.csproj +++ b/Bank/OperatorApp/OperatorApp.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/Bank/OperatorApp/appsettings.json b/Bank/OperatorApp/appsettings.json index 10f68b8..06180ac 100644 --- a/Bank/OperatorApp/appsettings.json +++ b/Bank/OperatorApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "IPAddress": "http://localhost:5225" }