From e765131f4a52b3a2a0ebc83b48f77c6aad2ac57c Mon Sep 17 00:00:00 2001 From: Salikh Date: Mon, 17 Jun 2024 16:38:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=84=D0=B8=D0=B3=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B2=20?= =?UTF-8?q?=D1=81=D0=B0=D0=BC=D0=BE=D0=BC=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorPlant/MotorPlantRestApi/APIConfig.cs | 12 ---- .../Controllers/ShopController.cs | 47 ++------------ MotorPlant/MotorPlantRestApi/Program.cs | 1 - MotorPlant/MotorPlantShopApp/APIClient.cs | 16 ++--- .../Controllers/HomeController.cs | 63 +++++++++++++------ MotorPlant/MotorPlantShopApp/appsettings.json | 3 +- 6 files changed, 61 insertions(+), 81 deletions(-) delete mode 100644 MotorPlant/MotorPlantRestApi/APIConfig.cs diff --git a/MotorPlant/MotorPlantRestApi/APIConfig.cs b/MotorPlant/MotorPlantRestApi/APIConfig.cs deleted file mode 100644 index 7832a49..0000000 --- a/MotorPlant/MotorPlantRestApi/APIConfig.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace MotorPlantRestApi -{ - public class APIConfig - { - public static string? ShopPassword; - - public static void LoadData(IConfiguration configuration) - { - ShopPassword = configuration["ShopAPIPassword"]; - } - } -} diff --git a/MotorPlant/MotorPlantRestApi/Controllers/ShopController.cs b/MotorPlant/MotorPlantRestApi/Controllers/ShopController.cs index 53bbf7c..a9e9e83 100644 --- a/MotorPlant/MotorPlantRestApi/Controllers/ShopController.cs +++ b/MotorPlant/MotorPlantRestApi/Controllers/ShopController.cs @@ -20,18 +20,8 @@ namespace MotorPlantRestApi.Controllers } [HttpGet] - public bool Authentication(string password) + public List? GetShopList() { - return CheckPassword(password); - } - - [HttpGet] - public List? 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; - } } } diff --git a/MotorPlant/MotorPlantRestApi/Program.cs b/MotorPlant/MotorPlantRestApi/Program.cs index a55c34b..d29865c 100644 --- a/MotorPlant/MotorPlantRestApi/Program.cs +++ b/MotorPlant/MotorPlantRestApi/Program.cs @@ -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()) diff --git a/MotorPlant/MotorPlantShopApp/APIClient.cs b/MotorPlant/MotorPlantShopApp/APIClient.cs index 65619ce..0122af2 100644 --- a/MotorPlant/MotorPlantShopApp/APIClient.cs +++ b/MotorPlant/MotorPlantShopApp/APIClient.cs @@ -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(string requestUrl) { var response = _client.GetAsync(requestUrl); @@ -28,21 +30,17 @@ namespace MotorPlantShopApp throw new Exception(result); } } - public static void PostRequest(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); + } } } diff --git a/MotorPlant/MotorPlantShopApp/Controllers/HomeController.cs b/MotorPlant/MotorPlantShopApp/Controllers/HomeController.cs index d0912c9..99efe76 100644 --- a/MotorPlant/MotorPlantShopApp/Controllers/HomeController.cs +++ b/MotorPlant/MotorPlantShopApp/Controllers/HomeController.cs @@ -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>($"api/shop/getshoplist?password={APIClient.Password}")); + return View(APIClient.GetRequest>($"api/shop/getshoplist")); } [HttpGet] @@ -33,20 +33,23 @@ namespace MotorPlantShopApp.Controllers [HttpPost] public void Enter(string password) { - bool resout = APIClient.GetRequest($"/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($"api/shop/getshop?shopId={Id}&password={APIClient.Password}")); + return View("Shop", APIClient.GetRequest($"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>("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>($"api/shop/getshoplist?password={APIClient.Password}"); + ViewBag.Shops = APIClient.GetRequest>($"api/shop/getshoplist"); ViewBag.Engines = APIClient.GetRequest>($"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, diff --git a/MotorPlant/MotorPlantShopApp/appsettings.json b/MotorPlant/MotorPlantShopApp/appsettings.json index e5d4650..cf04e9a 100644 --- a/MotorPlant/MotorPlantShopApp/appsettings.json +++ b/MotorPlant/MotorPlantShopApp/appsettings.json @@ -6,5 +6,6 @@ } }, "AllowedHosts": "*", - "IPAddress": "http://localhost:5215/" + "IPAddress": "http://localhost:5215/", + "Password": "salih" }