Удален 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 IReportLogic _reportLogic;
private readonly AbstractMailWorker _mailWorker;
public static BankOperatorViewModel? BankOperator { get; set; } = null;
public HomeController(ILogger<HomeController> 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<int> 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($"<script language=\"javascript\">alert" +
$"('You need to login!');window.location.replace('/Home/Enter');" +
@ -295,7 +296,7 @@ namespace BankOperatorApp.Controllers
[HttpGet]
public IActionResult CurrencyPurchasePaymentsReport()
{
if (APIClient.BankOperator == null)
if (HomeController.BankOperator == null)
{
Response.WriteAsync($"<script language=\"javascript\">alert" +
$"('You need to login!');window.location.replace('/Home/Enter');</script>");
@ -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($"<script language=\"javascript\">" +
$"alert('You need to login!');window.location.replace('/Home/Enter');</script>");
@ -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