From 39cfa69fa0107210712c3ed63323c4793f2bee10 Mon Sep 17 00:00:00 2001
From: ksenianeva <95441235+ksenianeva@users.noreply.github.com>
Date: Fri, 7 Apr 2023 23:40:48 +0400
Subject: [PATCH] =?UTF-8?q?HomeController+APIClient=20=D0=B4=D0=BB=D1=8F?=
 =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20BankOperator.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Bank/BankOperatorApp/APIClient.cs             |  74 +++++++++
 Bank/BankOperatorApp/BankOperatorApp.csproj   |   8 +
 .../Controllers/HomeController.cs             | 140 +++++++++++++++++-
 3 files changed, 219 insertions(+), 3 deletions(-)
 create mode 100644 Bank/BankOperatorApp/APIClient.cs

diff --git a/Bank/BankOperatorApp/APIClient.cs b/Bank/BankOperatorApp/APIClient.cs
new file mode 100644
index 0000000..4df62ce
--- /dev/null
+++ b/Bank/BankOperatorApp/APIClient.cs
@@ -0,0 +1,74 @@
+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 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<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);
+            }
+        }
+
+        public static void PatchRequest<T>(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<T>(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/BankOperatorApp.csproj b/Bank/BankOperatorApp/BankOperatorApp.csproj
index c78c9c7..7ce9418 100644
--- a/Bank/BankOperatorApp/BankOperatorApp.csproj
+++ b/Bank/BankOperatorApp/BankOperatorApp.csproj
@@ -6,4 +6,12 @@
     <ImplicitUsings>enable</ImplicitUsings>
   </PropertyGroup>
 
+  <ItemGroup>
+    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\BankContracts\BankContracts.csproj" />
+  </ItemGroup>
+
 </Project>
diff --git a/Bank/BankOperatorApp/Controllers/HomeController.cs b/Bank/BankOperatorApp/Controllers/HomeController.cs
index 700ab5e..704d5eb 100644
--- a/Bank/BankOperatorApp/Controllers/HomeController.cs
+++ b/Bank/BankOperatorApp/Controllers/HomeController.cs
@@ -1,5 +1,7 @@
-using BankOperatorApp.Models;
+using BankContracts.BindingModels;
+using BankContracts.ViewModels;
 using Microsoft.AspNetCore.Mvc;
+using BankOperatorApp.Models;
 using System.Diagnostics;
 
 namespace BankOperatorApp.Controllers
@@ -15,12 +17,50 @@ namespace BankOperatorApp.Controllers
 
         public IActionResult Index()
         {
-            return View();
+            if (APIClient.Operator == null)
+            {
+                return Redirect("~/Home/Enter");
+            }
+            return View(APIClient.GetRequest<List<DealViewModel>>($"api/deal/getdeals?operatorId={APIClient.Operator.Id}"));
         }
 
+        [HttpGet]
         public IActionResult Privacy()
         {
-            return View();
+            if (APIClient.Operator == null)
+            {
+                return Redirect("~/Home/Enter");
+            }
+            return View(APIClient.Operator);
+        }
+
+        [HttpPost]
+        public void Privacy(string login, string password, string lastname, string firstname, string middleName)
+        {
+            if (APIClient.Operator == null)
+            {
+                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
+            }
+            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(lastname) || string.IsNullOrEmpty(firstname) || string.IsNullOrEmpty(middleName))
+            {
+                throw new Exception("Введите логин, пароль и ФИО");
+            }
+            APIClient.PostRequest("api/operator/updateoperator", new OperatorBindingModel
+            {
+                Id = APIClient.Operator.Id,
+                LastName = lastname,
+                FirstName = firstname,
+                MiddleName = middleName,
+                Login = login,
+                Password = password
+            });
+
+            APIClient.Operator.LastName = lastname;
+            APIClient.Operator.FirstName = firstname;
+            APIClient.Operator.MiddleName = middleName;
+            APIClient.Operator.Login = login;
+            APIClient.Operator.Password = password;
+            Response.Redirect("Index");
         }
 
         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
@@ -28,5 +68,99 @@ namespace BankOperatorApp.Controllers
         {
             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.Operator = APIClient.GetRequest<OperatorViewModel>($"api/operator/login?login={login}&password={password}");
+            if (APIClient.Operator == null)
+            {
+                throw new Exception("Неверный логин/пароль");
+            }
+            Response.Redirect("Index");
+        }
+
+        [HttpGet]
+        public IActionResult Register()
+        {
+            return View();
+        }
+
+        [HttpPost]
+        public void Register(string login, string password, string lastname, string firstname, string middleName)
+        {
+            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(lastname) || string.IsNullOrEmpty(firstname) || string.IsNullOrEmpty(middleName))
+            {
+                throw new Exception("Введите логин, пароль и ФИО");
+            }
+            APIClient.PostRequest("api/operator/createoperator", new OperatorBindingModel
+            {
+                LastName = lastname,
+                FirstName = firstname,
+                MiddleName = middleName,
+                Login = login,
+                Password = password
+            });
+            Response.Redirect("Enter");
+            return;
+        }
+        [HttpGet]
+        public IActionResult CreateDeal()
+        {
+            return View();
+        }
+
+        [HttpPost]
+        public void CreateDeal(int clientid)
+        {
+            if (APIClient.Operator == null)
+            {
+                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
+            }
+            APIClient.PostRequest("api/deal/createdeal", new DealBindingModel
+            {
+                ClientId = clientid,
+                OperatorId = APIClient.Operator.Id,
+            });
+            Response.Redirect("Index");
+        }
+        public IActionResult Payments()
+        {
+            if (APIClient.Operator == null)
+            {
+                return Redirect("~/Home/Enter");
+            }
+            return View(APIClient.GetRequest<List<PaymentViewModel>>($"api/payment/getpayments?operatorId={APIClient.Operator.Id}"));
+        }
+        [HttpGet]
+        public IActionResult CreatePayment()
+        {
+            ViewBag.Deals = APIClient.GetRequest<List<DealViewModel>>("api/deal/getdealslist");
+            return View();
+        }
+        [HttpPost]
+        public void CreatePayment(int clientid)
+        {
+            if (APIClient.Operator == null)
+            {
+                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
+            }
+            APIClient.PostRequest("api/deal/createdeal", new PaymentBindingModel
+            {
+
+                OperatorId = APIClient.Operator.Id,
+            });
+            Response.Redirect("Index");
+        }
     }
 }
\ No newline at end of file