Compare commits

..

2 Commits

3 changed files with 39 additions and 113 deletions

View File

@ -1,74 +0,0 @@
using System.Net.Http.Headers;
using System.Text;
using BankContracts.ViewModels;
using Newtonsoft.Json;
namespace OperatorApp
{
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);
}
}
}
}

View File

@ -24,6 +24,8 @@ namespace OperatorApp.Controllers
private readonly ICurrencyLogic _currencyLogic; private readonly ICurrencyLogic _currencyLogic;
private readonly AbstractMailWorker _mailWorker; private readonly AbstractMailWorker _mailWorker;
public static OperatorViewModel? Operator;
public HomeController(ILogger<HomeController> logger, IDealLogic dealLogic, IPaymentLogic paymentLogic, ITransferLogic transferLogic, IOperatorLogic operatorLogic, IReportLogic reportLogic, ICurrencyLogic currencyLogic, AbstractMailWorker mailWorker) public HomeController(ILogger<HomeController> logger, IDealLogic dealLogic, IPaymentLogic paymentLogic, ITransferLogic transferLogic, IOperatorLogic operatorLogic, IReportLogic reportLogic, ICurrencyLogic currencyLogic, AbstractMailWorker mailWorker)
{ {
_logger = logger; _logger = logger;
@ -38,28 +40,28 @@ namespace OperatorApp.Controllers
public IActionResult Index() public IActionResult Index()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
return View(_dealLogic.ReadList(new DealSearchModel { OperatorId = APIClient.Operator.Id })); return View(_dealLogic.ReadList(new DealSearchModel { OperatorId = HomeController.Operator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult Privacy() public IActionResult Privacy()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
return View(APIClient.Operator); return View(HomeController.Operator);
} }
[HttpPost] [HttpPost]
public void Privacy(string login, string password, string lastname, string firstname, string middleName) public void Privacy(string login, string password, string lastname, string firstname, string middleName)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return; return;
@ -70,7 +72,7 @@ namespace OperatorApp.Controllers
} }
_operatorLogic.Update(new OperatorBindingModel _operatorLogic.Update(new OperatorBindingModel
{ {
Id = APIClient.Operator.Id, Id = HomeController.Operator.Id,
LastName = lastname, LastName = lastname,
FirstName = firstname, FirstName = firstname,
MiddleName = middleName, MiddleName = middleName,
@ -78,11 +80,11 @@ namespace OperatorApp.Controllers
Password = password Password = password
}); });
APIClient.Operator.LastName = lastname; HomeController.Operator.LastName = lastname;
APIClient.Operator.FirstName = firstname; HomeController.Operator.FirstName = firstname;
APIClient.Operator.MiddleName = middleName; HomeController.Operator.MiddleName = middleName;
APIClient.Operator.Login = login; HomeController.Operator.Login = login;
APIClient.Operator.Password = password; HomeController.Operator.Password = password;
Response.Redirect("Index"); Response.Redirect("Index");
} }
@ -106,8 +108,8 @@ namespace OperatorApp.Controllers
Response.WriteAsync($"<script language=\"javascript\">alert('Input login and password!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('Input login and password!');window.location.replace('/Home/Enter');</script>");
return; return;
} }
APIClient.Operator = _operatorLogic.ReadElement(new OperatorSearchModel { Login = login, Password = password }); HomeController.Operator = _operatorLogic.ReadElement(new OperatorSearchModel { Login = login, Password = password });
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('Wrong login or password!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('Wrong login or password!');window.location.replace('/Home/Enter');</script>");
return; return;
@ -142,7 +144,7 @@ namespace OperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult CreateDeal() public IActionResult CreateDeal()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
@ -153,7 +155,7 @@ namespace OperatorApp.Controllers
[HttpPost] [HttpPost]
public void CreateDeal(int clientid) public void CreateDeal(int clientid)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return; return;
@ -161,34 +163,34 @@ namespace OperatorApp.Controllers
_dealLogic.Create(new DealBindingModel _dealLogic.Create(new DealBindingModel
{ {
ClientId = clientid, ClientId = clientid,
OperatorId = APIClient.Operator.Id, OperatorId = HomeController.Operator.Id,
}); });
Response.Redirect("Index"); Response.Redirect("Index");
} }
public IActionResult Payments() public IActionResult Payments()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
return View(_paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id })); return View(_paymentLogic.ReadList(new PaymentSearchModel { OperatorId = HomeController.Operator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult CreatePayment() public IActionResult CreatePayment()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
ViewBag.Deals = _dealLogic.ReadList(new DealSearchModel { OperatorId = APIClient.Operator.Id }); ViewBag.Deals = _dealLogic.ReadList(new DealSearchModel { OperatorId = HomeController.Operator.Id });
return View(); return View();
} }
[HttpPost] [HttpPost]
public void CreatePayment(List<int> deals) public void CreatePayment(List<int> deals)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return; return;
@ -199,13 +201,13 @@ namespace OperatorApp.Controllers
var deal = _dealLogic.ReadElement(new DealSearchModel { Id = id }); var deal = _dealLogic.ReadElement(new DealSearchModel { Id = id });
if (deal != null) DealPayments.Add(deal.Id, deal); if (deal != null) DealPayments.Add(deal.Id, deal);
} }
_paymentLogic.Create(new PaymentBindingModel { OperatorId = APIClient.Operator.Id, DealPayments = DealPayments, }); _paymentLogic.Create(new PaymentBindingModel { OperatorId = HomeController.Operator.Id, DealPayments = DealPayments, });
Response.Redirect("Payments"); Response.Redirect("Payments");
} }
[HttpGet] [HttpGet]
public IActionResult Payment(int id) public IActionResult Payment(int id)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
@ -215,39 +217,39 @@ namespace OperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult Transfers() public IActionResult Transfers()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
return View(_transferLogic.ReadList(new TransferSearchModel { OperatorId = APIClient.Operator.Id })); return View(_transferLogic.ReadList(new TransferSearchModel { OperatorId = HomeController.Operator.Id }));
} }
[HttpGet] [HttpGet]
public IActionResult CreateTransfer() public IActionResult CreateTransfer()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id }); ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = HomeController.Operator.Id });
return View(); return View();
} }
[HttpPost] [HttpPost]
public void CreateTransfer(string amount, int payment) public void CreateTransfer(string amount, int payment)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return; return;
} }
_transferLogic.Create(new TransferBindingModel { OperatorId = APIClient.Operator.Id, Amount = (float)Convert.ToDouble(amount), PaymentId = payment }); _transferLogic.Create(new TransferBindingModel { OperatorId = HomeController.Operator.Id, Amount = (float)Convert.ToDouble(amount), PaymentId = payment });
Response.Redirect("Transfers"); Response.Redirect("Transfers");
} }
public void DeleteTransfer(int id) public void DeleteTransfer(int id)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
} }
@ -258,12 +260,12 @@ namespace OperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult PaymentsReport() public IActionResult PaymentsReport()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
} }
ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = APIClient.Operator.Id }); ViewBag.Payments = _paymentLogic.ReadList(new PaymentSearchModel { OperatorId = HomeController.Operator.Id });
return View(); return View();
} }
@ -302,7 +304,7 @@ namespace OperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult TransfersReport() public IActionResult TransfersReport()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
@ -312,7 +314,7 @@ namespace OperatorApp.Controllers
[HttpPost] [HttpPost]
public IActionResult TransfersReport(DateTime dateFrom, DateTime dateTo, string reptype, string email, string fileName) public IActionResult TransfersReport(DateTime dateFrom, DateTime dateTo, string reptype, string email, string fileName)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
@ -332,7 +334,7 @@ namespace OperatorApp.Controllers
_mailWorker.MailSendAsync(new MailSendInfoBindingModel _mailWorker.MailSendAsync(new MailSendInfoBindingModel
{ {
Subject = "Отчёт о закупках", Subject = "Отчёт о закупках",
Text = "Для оператора " + APIClient.Operator.LastName + APIClient.Operator.FirstName, Text = "Для оператора " + HomeController.Operator.LastName + HomeController.Operator.FirstName,
MailAddress = email, MailAddress = email,
FileName = fileName, FileName = fileName,
Attachment = report Attachment = report
@ -349,7 +351,7 @@ namespace OperatorApp.Controllers
[HttpGet] [HttpGet]
public IActionResult AddCurrenciesToPayment() public IActionResult AddCurrenciesToPayment()
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
return Redirect("/Home/Enter"); return Redirect("/Home/Enter");
@ -361,7 +363,7 @@ namespace OperatorApp.Controllers
[HttpPost] [HttpPost]
public void AddCurrenciesToPayment(int payment, List<int> currencies) public void AddCurrenciesToPayment(int payment, List<int> currencies)
{ {
if (APIClient.Operator == null) if (HomeController.Operator == null)
{ {
Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>"); Response.WriteAsync($"<script language=\"javascript\">alert('You need to login!');window.location.replace('/Home/Enter');</script>");
Redirect("/Home/Enter"); Redirect("/Home/Enter");

View File

@ -54,8 +54,6 @@ catch (Exception ex)
logger?.LogError(ex, "Îøèáêà ðàáîòû ñ ïî÷òîé"); logger?.LogError(ex, "Îøèáêà ðàáîòû ñ ïî÷òîé");
} }
APIClient.Connect(builder.Configuration);
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {