Удален APIClient из проекта "Поставщик".

This commit is contained in:
ksenianeva 2023-05-20 06:01:54 +04:00
parent d666f1f74b
commit fa9ade8941
2 changed files with 31 additions and 104 deletions

View File

@ -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<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);
}
}
}
}

View File

@ -20,6 +20,7 @@ namespace BankOperatorApp.Controllers
private readonly ICurrencyPurchaseLogic _currencyPurchaseLogic; private readonly ICurrencyPurchaseLogic _currencyPurchaseLogic;
private readonly IReportLogic _reportLogic; private readonly IReportLogic _reportLogic;
private readonly AbstractMailWorker _mailWorker; private readonly AbstractMailWorker _mailWorker;
public static BankOperatorViewModel? BankOperator { get; set; } = null;
public HomeController(ILogger<HomeController> logger, IBankOperatorLogic bankOperatorLogic, public HomeController(ILogger<HomeController> logger, IBankOperatorLogic bankOperatorLogic,
@ -37,29 +38,29 @@ namespace BankOperatorApp.Controllers
public IActionResult Index() public IActionResult Index()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
return View(_currencyLogic.ReadList(new CurrencySearchModel { BankOperatorId = return View(_currencyLogic.ReadList(new CurrencySearchModel { BankOperatorId =
APIClient.BankOperator.Id })); HomeController.BankOperator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult Privacy() public IActionResult Privacy()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
return View(APIClient.BankOperator); return View(HomeController.BankOperator);
} }
[HttpPost] [HttpPost]
public void Privacy(string login, string password, string lastname, public void Privacy(string login, string password, string lastname,
string firstname, string middleName) string firstname, string middleName)
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
@ -69,7 +70,7 @@ namespace BankOperatorApp.Controllers
} }
_bankOperatorLogic.Update(new BankOperatorBindingModel _bankOperatorLogic.Update(new BankOperatorBindingModel
{ {
Id = APIClient.BankOperator.Id, Id = HomeController.BankOperator.Id,
LastName = lastname, LastName = lastname,
FirstName = firstname, FirstName = firstname,
MiddleName = middleName, MiddleName = middleName,
@ -77,11 +78,11 @@ namespace BankOperatorApp.Controllers
Password = password Password = password
}); });
APIClient.BankOperator.LastName = lastname; HomeController.BankOperator.LastName = lastname;
APIClient.BankOperator.FirstName = firstname; HomeController.BankOperator.FirstName = firstname;
APIClient.BankOperator.MiddleName = middleName; HomeController.BankOperator.MiddleName = middleName;
APIClient.BankOperator.Login = login; HomeController.BankOperator.Login = login;
APIClient.BankOperator.Password = password; HomeController.BankOperator.Password = password;
Response.Redirect("Index"); Response.Redirect("Index");
} }
@ -105,9 +106,9 @@ namespace BankOperatorApp.Controllers
{ {
throw new Exception("Введите логин и пароль"); throw new Exception("Введите логин и пароль");
} }
APIClient.BankOperator = _bankOperatorLogic.ReadElement HomeController.BankOperator = _bankOperatorLogic.ReadElement
(new BankOperatorSearchModel { Login = login, Password = password }); (new BankOperatorSearchModel { Login = login, Password = password });
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Неверный логин/пароль"); throw new Exception("Неверный логин/пароль");
} }
@ -150,25 +151,25 @@ namespace BankOperatorApp.Controllers
[HttpPost] [HttpPost]
public void CreateCurrency(string name) public void CreateCurrency(string name)
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
_currencyLogic.Create(new CurrencyBindingModel _currencyLogic.Create(new CurrencyBindingModel
{ {
Name = name, Name = name,
BankOperatorId = APIClient.BankOperator.Id, BankOperatorId = HomeController.BankOperator.Id,
}); });
Response.Redirect("Index"); Response.Redirect("Index");
} }
public IActionResult CreditPrograms() public IActionResult CreditPrograms()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
return View(_creditProgramLogic.ReadList(new CreditProgramSearchModel return View(_creditProgramLogic.ReadList(new CreditProgramSearchModel
{ BankOperatorId = APIClient.BankOperator.Id})); { BankOperatorId = HomeController.BankOperator.Id}));
} }
[HttpGet] [HttpGet]
public IActionResult CreateCreditProgram() public IActionResult CreateCreditProgram()
@ -179,7 +180,7 @@ namespace BankOperatorApp.Controllers
[HttpPost] [HttpPost]
public void CreateCreditProgram(List<int> currencies, string name, float percent) public void CreateCreditProgram(List<int> currencies, string name, float percent)
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
@ -190,7 +191,7 @@ namespace BankOperatorApp.Controllers
if (currency != null) CurrencyCreditPrograms.Add(currency.Id, currency); if (currency != null) CurrencyCreditPrograms.Add(currency.Id, currency);
} }
_creditProgramLogic.Create(new CreditProgramBindingModel { BankOperatorId = _creditProgramLogic.Create(new CreditProgramBindingModel { BankOperatorId =
APIClient.BankOperator.Id, HomeController.BankOperator.Id,
CreditProgramCurrencies = CurrencyCreditPrograms, Name = name, Percent = percent}); CreditProgramCurrencies = CurrencyCreditPrograms, Name = name, Percent = percent});
Response.Redirect("CreditPrograms"); Response.Redirect("CreditPrograms");
} }
@ -202,17 +203,17 @@ namespace BankOperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult CurrencyPurchase() public IActionResult CurrencyPurchase()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
return View(_currencyPurchaseLogic.ReadList return View(_currencyPurchaseLogic.ReadList
(new CurrencyPurchaseSearchModel { BankOperatorId = APIClient.BankOperator.Id })); (new CurrencyPurchaseSearchModel { BankOperatorId = HomeController.BankOperator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult CreateCurrencyPurchase() public IActionResult CreateCurrencyPurchase()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
@ -222,13 +223,13 @@ namespace BankOperatorApp.Controllers
[HttpPost] [HttpPost]
public void CreateCurrencyPurchase(string amount, int currencyId) public void CreateCurrencyPurchase(string amount, int currencyId)
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
throw new Exception("Вы как суда попали? Суда вход только авторизованным"); throw new Exception("Вы как суда попали? Суда вход только авторизованным");
} }
_currencyPurchaseLogic.Create(new CurrencyPurchaseBindingModel _currencyPurchaseLogic.Create(new CurrencyPurchaseBindingModel
{ BankOperatorId = APIClient.BankOperator.Id, { BankOperatorId = HomeController.BankOperator.Id,
Amount = (float)Convert.ToDouble(amount), Amount = (float)Convert.ToDouble(amount),
CurrencyId = currencyId, CurrencyId = currencyId,
@ -238,18 +239,18 @@ namespace BankOperatorApp.Controllers
public IActionResult CurrencyPurchases() public IActionResult CurrencyPurchases()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
return View(_currencyPurchaseLogic.ReadList(new CurrencyPurchaseSearchModel return View(_currencyPurchaseLogic.ReadList(new CurrencyPurchaseSearchModel
{ BankOperatorId = APIClient.BankOperator.Id })); { BankOperatorId = HomeController.BankOperator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult CurrencyReport() public IActionResult CurrencyReport()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert" + Response.WriteAsync($"<script language=\"javascript\">alert" +
$"('You need to login!');window.location.replace('/Home/Enter');" + $"('You need to login!');window.location.replace('/Home/Enter');" +
@ -295,7 +296,7 @@ namespace BankOperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult CurrencyPurchasePaymentsReport() public IActionResult CurrencyPurchasePaymentsReport()
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert" + Response.WriteAsync($"<script language=\"javascript\">alert" +
$"('You need to login!');window.location.replace('/Home/Enter');</script>"); $"('You need to login!');window.location.replace('/Home/Enter');</script>");
@ -307,7 +308,7 @@ namespace BankOperatorApp.Controllers
public IActionResult CurrencyPurchasePaymentsReport(DateTime dateFrom, public IActionResult CurrencyPurchasePaymentsReport(DateTime dateFrom,
DateTime dateTo, string reptype, string email, string fileName) DateTime dateTo, string reptype, string email, string fileName)
{ {
if (APIClient.BankOperator == null) if (HomeController.BankOperator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">" + Response.WriteAsync($"<script language=\"javascript\">" +
$"alert('You need to login!');window.location.replace('/Home/Enter');</script>"); $"alert('You need to login!');window.location.replace('/Home/Enter');</script>");
@ -329,7 +330,7 @@ namespace BankOperatorApp.Controllers
_mailWorker.MailSendAsync(new MailSendInfoBindingModel _mailWorker.MailSendAsync(new MailSendInfoBindingModel
{ {
Subject = "Отчёт по закупкам", Subject = "Отчёт по закупкам",
Text = "Для оператора " + APIClient.BankOperator.LastName + APIClient.BankOperator.FirstName, Text = "Для оператора " + HomeController.BankOperator.LastName + HomeController.BankOperator.FirstName,
MailAddress = email, MailAddress = email,
FileName = fileName, FileName = fileName,
Attachment = report Attachment = report