HomeController+APIClient для проекта BankOperator.

This commit is contained in:
ksenianeva 2023-04-07 23:40:48 +04:00
parent ddbda1f6ac
commit 39cfa69fa0
3 changed files with 219 additions and 3 deletions

View File

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

View File

@ -6,4 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BankContracts\BankContracts.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,7 @@
using BankOperatorApp.Models; using BankContracts.BindingModels;
using BankContracts.ViewModels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using BankOperatorApp.Models;
using System.Diagnostics; using System.Diagnostics;
namespace BankOperatorApp.Controllers namespace BankOperatorApp.Controllers
@ -15,12 +17,50 @@ namespace BankOperatorApp.Controllers
public IActionResult Index() 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() 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)] [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 }); 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");
}
} }
} }