PIbd-21 Yaruskin S.A. LabWork05_Hard #29

Closed
Salikh wants to merge 23 commits from lab5-hard into laba5
6 changed files with 61 additions and 81 deletions
Showing only changes of commit e765131f4a - Show all commits

View File

@ -1,12 +0,0 @@
namespace MotorPlantRestApi
{
public class APIConfig
{
public static string? ShopPassword;
public static void LoadData(IConfiguration configuration)
{
ShopPassword = configuration["ShopAPIPassword"];
}
}
}

View File

@ -20,18 +20,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpGet]
public bool Authentication(string password)
public List<ShopViewModel>? GetShopList()
{
return CheckPassword(password);
}
[HttpGet]
public List<ShopViewModel>? GetShopList(string password)
{
if (!CheckPassword(password))
{
return null;
}
try
{
return _shopLogic.ReadList(null);
@ -44,12 +34,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpGet]
public ShopEngineViewModel? GetShop(int shopId, string password)
public ShopEngineViewModel? GetShop(int shopId)
{
if (!CheckPassword(password))
{
return null;
}
try
{
var shop = _shopLogic.ReadElement(new ShopSearchModel { Id = shopId });
@ -77,12 +63,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpPost]
public void CreateShop(ShopBindingModel model, string password)
public void CreateShop(ShopBindingModel model)
{
if (!CheckPassword(password))
{
return;
}
try
{
_shopLogic.Create(model);
@ -95,12 +77,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpPost]
public void UpdateShop(ShopBindingModel model, string password)
public void UpdateShop(ShopBindingModel model)
{
if (!CheckPassword(password))
{
return;
}
try
{
_shopLogic.Update(model);
@ -113,12 +91,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpDelete]
public void DeleteShop(int shopId, string password)
public void DeleteShop(int shopId)
{
if (!CheckPassword(password))
{
return;
}
try
{
_shopLogic.Delete(new ShopBindingModel { Id = shopId });
@ -131,12 +105,8 @@ namespace MotorPlantRestApi.Controllers
}
[HttpPost]
public void MakeSypply(SupplyBindingModel model, string password)
public void MakeSypply(SupplyBindingModel model)
{
if (!CheckPassword(password))
{
return;
}
try
{
_shopLogic.MakeSupply(model);
@ -147,10 +117,5 @@ namespace MotorPlantRestApi.Controllers
throw;
}
}
private bool CheckPassword(string password)
{
return APIConfig.ShopPassword == password;
}
}
}

View File

@ -35,7 +35,6 @@ builder.Services.AddSwaggerGen(c =>
});
var app = builder.Build();
APIConfig.LoadData(builder.Configuration);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

View File

@ -7,14 +7,16 @@ namespace MotorPlantShopApp
public class APIClient
{
private static readonly HttpClient _client = new();
public static string? Password { get; set; }
private static string password = string.Empty;
public static bool Access { get; private set; } = false;
public static void Connect(IConfiguration configuration)
{
password = configuration["Password"];
_client.BaseAddress = new Uri(configuration["IPAddress"]);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
}
public static T? GetRequest<T>(string requestUrl)
{
var response = _client.GetAsync(requestUrl);
@ -28,21 +30,17 @@ namespace MotorPlantShopApp
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 DeleteRequest(string requestUrl)
{
var response = _client.DeleteAsync(requestUrl);
@ -52,5 +50,9 @@ namespace MotorPlantShopApp
throw new Exception(result);
}
}
public static bool CheckPassword(string _password)
{
return Access = (_password == password);
}
}
}

View File

@ -17,11 +17,11 @@ namespace MotorPlantShopApp.Controllers
public IActionResult Index()
{
if (APIClient.Password == null)
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist?password={APIClient.Password}"));
return View(APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist"));
}
[HttpGet]
@ -33,20 +33,23 @@ namespace MotorPlantShopApp.Controllers
[HttpPost]
public void Enter(string password)
{
bool resout = APIClient.GetRequest<bool>($"/api/shop/authentication?password={password}");
if (!resout)
{
Response.Redirect("../Home/Enter");
return;
}
APIClient.Password = password;
Response.Redirect("Index");
if (string.IsNullOrEmpty(password))
{
throw new Exception("Введите пароль");
}
APIClient.CheckPassword(password);
if (APIClient.Access == false)
{
throw new Exception("Неправильный пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Create()
{
if (APIClient.Password == null)
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
@ -56,6 +59,10 @@ namespace MotorPlantShopApp.Controllers
[HttpPost]
public void Create(int id, string shopname, string adress, DateTime openingdate, int maxcount)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress))
{
throw new Exception("Название или адрес не может быть пустым");
@ -65,7 +72,7 @@ namespace MotorPlantShopApp.Controllers
throw new Exception("Дата открытия не может быть пустой");
}
APIClient.PostRequest($"api/shop/createshop?password={APIClient.Password}", new ShopBindingModel
APIClient.PostRequest($"api/shop/createshop", new ShopBindingModel
{
Id = id,
ShopName = shopname,
@ -79,16 +86,20 @@ namespace MotorPlantShopApp.Controllers
[HttpGet]
public IActionResult Update(int Id)
{
if (APIClient.Password == null)
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
return View("Shop", APIClient.GetRequest<ShopEngineViewModel>($"api/shop/getshop?shopId={Id}&password={APIClient.Password}"));
return View("Shop", APIClient.GetRequest<ShopEngineViewModel>($"api/shop/getshop?shopId={Id}"));
}
[HttpPost]
public void Update(int id, string shopname, string adress, DateTime dateopen, int maxcount)
{
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress))
{
throw new Exception("Название или адрес не может быть пустым");
@ -97,7 +108,7 @@ namespace MotorPlantShopApp.Controllers
{
throw new Exception("Дата открытия не может быть пустой");
}
APIClient.PostRequest($"api/shop/updateshop?password={APIClient.Password}", new ShopBindingModel
APIClient.PostRequest($"api/shop/updateshop", new ShopBindingModel
{
Id = id,
ShopName = shopname,
@ -108,22 +119,36 @@ namespace MotorPlantShopApp.Controllers
Response.Redirect("../Index");
}
public IActionResult Delete()
{
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
return View();
}
[HttpPost]
public void Delete(int Id)
{
APIClient.DeleteRequest($"api/shop/deleteshop?shopId={Id}&password={APIClient.Password}");
if (APIClient.Access == false)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
APIClient.DeleteRequest($"api/shop/deleteshop");
Response.Redirect("../Index");
}
[HttpGet]
public IActionResult Supply()
{
if (APIClient.Password == null)
if (APIClient.Access == false)
{
return Redirect("~/Home/Enter");
}
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist?password={APIClient.Password}");
ViewBag.Shops = APIClient.GetRequest<List<ShopViewModel>>($"api/shop/getshoplist");
ViewBag.Engines = APIClient.GetRequest<List<EngineViewModel>>($"api/main/getenginelist");
return View();
}
@ -131,7 +156,7 @@ namespace MotorPlantShopApp.Controllers
[HttpPost]
public void Supply(int shop, int engine, int count)
{
APIClient.PostRequest($"api/shop/makesypply?password={APIClient.Password}", new SupplyBindingModel
APIClient.PostRequest($"api/shop/makesypply", new SupplyBindingModel
{
ShopId = shop,
EngineId = engine,

View File

@ -6,5 +6,6 @@
}
},
"AllowedHosts": "*",
"IPAddress": "http://localhost:5215/"
"IPAddress": "http://localhost:5215/",
"Password": "salih"
}