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(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/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 @@ enable + + + + + + + + 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>($"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($"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>($"api/payment/getpayments?operatorId={APIClient.Operator.Id}")); + } + [HttpGet] + public IActionResult CreatePayment() + { + ViewBag.Deals = APIClient.GetRequest>("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