исправил, теперь конфиг файл в самом приложении

This commit is contained in:
Salikh 2024-06-17 16:38:47 +04:00
parent 53f52a91f8
commit e765131f4a
6 changed files with 61 additions and 81 deletions

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

View File

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

View File

@ -7,14 +7,16 @@ namespace MotorPlantShopApp
public class APIClient public class APIClient
{ {
private static readonly HttpClient _client = new(); 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) public static void Connect(IConfiguration configuration)
{ {
password = configuration["Password"];
_client.BaseAddress = new Uri(configuration["IPAddress"]); _client.BaseAddress = new Uri(configuration["IPAddress"]);
_client.DefaultRequestHeaders.Accept.Clear(); _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) public static T? GetRequest<T>(string requestUrl)
{ {
var response = _client.GetAsync(requestUrl); var response = _client.GetAsync(requestUrl);
@ -28,21 +30,17 @@ namespace MotorPlantShopApp
throw new Exception(result); throw new Exception(result);
} }
} }
public static void PostRequest<T>(string requestUrl, T model) public static void PostRequest<T>(string requestUrl, T model)
{ {
var json = JsonConvert.SerializeObject(model); var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json"); var data = new StringContent(json, Encoding.UTF8, "application/json");
var response = _client.PostAsync(requestUrl, data); var response = _client.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result; var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode) if (!response.Result.IsSuccessStatusCode)
{ {
throw new Exception(result); throw new Exception(result);
} }
} }
public static void DeleteRequest(string requestUrl) public static void DeleteRequest(string requestUrl)
{ {
var response = _client.DeleteAsync(requestUrl); var response = _client.DeleteAsync(requestUrl);
@ -52,5 +50,9 @@ namespace MotorPlantShopApp
throw new Exception(result); 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() public IActionResult Index()
{ {
if (APIClient.Password == null) if (APIClient.Access == false)
{ {
return Redirect("~/Home/Enter"); 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] [HttpGet]
@ -33,20 +33,23 @@ namespace MotorPlantShopApp.Controllers
[HttpPost] [HttpPost]
public void Enter(string password) public void Enter(string password)
{ {
bool resout = APIClient.GetRequest<bool>($"/api/shop/authentication?password={password}"); if (string.IsNullOrEmpty(password))
if (!resout) {
{ throw new Exception("Введите пароль");
Response.Redirect("../Home/Enter"); }
return; APIClient.CheckPassword(password);
}
APIClient.Password = password; if (APIClient.Access == false)
Response.Redirect("Index"); {
throw new Exception("Неправильный пароль");
}
Response.Redirect("Index");
} }
[HttpGet] [HttpGet]
public IActionResult Create() public IActionResult Create()
{ {
if (APIClient.Password == null) if (APIClient.Access == false)
{ {
return Redirect("~/Home/Enter"); return Redirect("~/Home/Enter");
} }
@ -56,6 +59,10 @@ namespace MotorPlantShopApp.Controllers
[HttpPost] [HttpPost]
public void Create(int id, string shopname, string adress, DateTime openingdate, int maxcount) 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)) if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress))
{ {
throw new Exception("Название или адрес не может быть пустым"); throw new Exception("Название или адрес не может быть пустым");
@ -65,7 +72,7 @@ namespace MotorPlantShopApp.Controllers
throw new Exception("Дата открытия не может быть пустой"); throw new Exception("Дата открытия не может быть пустой");
} }
APIClient.PostRequest($"api/shop/createshop?password={APIClient.Password}", new ShopBindingModel APIClient.PostRequest($"api/shop/createshop", new ShopBindingModel
{ {
Id = id, Id = id,
ShopName = shopname, ShopName = shopname,
@ -79,16 +86,20 @@ namespace MotorPlantShopApp.Controllers
[HttpGet] [HttpGet]
public IActionResult Update(int Id) public IActionResult Update(int Id)
{ {
if (APIClient.Password == null) if (APIClient.Access == false)
{ {
return Redirect("~/Home/Enter"); 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] [HttpPost]
public void Update(int id, string shopname, string adress, DateTime dateopen, int maxcount) 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)) if (string.IsNullOrEmpty(shopname) || string.IsNullOrEmpty(adress))
{ {
throw new Exception("Название или адрес не может быть пустым"); throw new Exception("Название или адрес не может быть пустым");
@ -97,7 +108,7 @@ namespace MotorPlantShopApp.Controllers
{ {
throw new Exception("Дата открытия не может быть пустой"); throw new Exception("Дата открытия не может быть пустой");
} }
APIClient.PostRequest($"api/shop/updateshop?password={APIClient.Password}", new ShopBindingModel APIClient.PostRequest($"api/shop/updateshop", new ShopBindingModel
{ {
Id = id, Id = id,
ShopName = shopname, ShopName = shopname,
@ -108,22 +119,36 @@ namespace MotorPlantShopApp.Controllers
Response.Redirect("../Index"); 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] [HttpPost]
public void Delete(int Id) 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"); Response.Redirect("../Index");
} }
[HttpGet] [HttpGet]
public IActionResult Supply() public IActionResult Supply()
{ {
if (APIClient.Password == null) if (APIClient.Access == false)
{ {
return Redirect("~/Home/Enter"); 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"); ViewBag.Engines = APIClient.GetRequest<List<EngineViewModel>>($"api/main/getenginelist");
return View(); return View();
} }
@ -131,7 +156,7 @@ namespace MotorPlantShopApp.Controllers
[HttpPost] [HttpPost]
public void Supply(int shop, int engine, int count) 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, ShopId = shop,
EngineId = engine, EngineId = engine,

View File

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