From df552c39e1a50503052102c2191e3d6cd4f7e578 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 26 May 2024 16:22:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B0=D1=81=D1=82=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=B2=D1=85=D0=BE=D0=B4.=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20UserStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UniversityClientApp/APIStorekeeper.cs | 48 +++++++++++++++++++ .../Controllers/HomeController.cs | 48 ++++++++++++++++--- University/UniversityClientApp/Program.cs | 15 +++--- .../UniversityClientAppStorekeeper.csproj | 5 ++ .../Views/Home/Index.cshtml | 14 +++++- .../Views/Home/Register.cshtml | 26 +++------- .../UniversityClientApp/appsettings.json | 5 +- .../Implements/UserStorage.cs | 8 ++-- .../UniversityDatabase.cs | 2 +- .../Controllers/UserController.cs | 12 ++++- .../UniversityRestApi/UniversityRestApi.http | 6 --- 11 files changed, 144 insertions(+), 45 deletions(-) create mode 100644 University/UniversityClientApp/APIStorekeeper.cs delete mode 100644 University/UniversityRestApi/UniversityRestApi.http diff --git a/University/UniversityClientApp/APIStorekeeper.cs b/University/UniversityClientApp/APIStorekeeper.cs new file mode 100644 index 0000000..69a5f82 --- /dev/null +++ b/University/UniversityClientApp/APIStorekeeper.cs @@ -0,0 +1,48 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System.Net.Http.Headers; +using Newtonsoft.Json; +using System.Text; +using UniversityContracts.ViewModels; + +namespace UniversityClientAppStorekeeper +{ + [Route("api/[controller]")] + [ApiController] + public class APIStorekeeper : ControllerBase + { + private static readonly HttpClient _client = new(); + public static UserViewModel? Client { 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(string requestUrl) + { + var response = _client.GetAsync(requestUrl); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (response.Result.IsSuccessStatusCode) + { + return JsonConvert.DeserializeObject(result); + } + else + { + 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); + } + } + } +} diff --git a/University/UniversityClientApp/Controllers/HomeController.cs b/University/UniversityClientApp/Controllers/HomeController.cs index 8886000..6b1e844 100644 --- a/University/UniversityClientApp/Controllers/HomeController.cs +++ b/University/UniversityClientApp/Controllers/HomeController.cs @@ -1,6 +1,10 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using UniversityClientApp.Models; +using UniversityClientAppStorekeeper; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; +using UniversityDataModels.Enums; namespace UniversityClientApp.Controllers { @@ -23,12 +27,49 @@ namespace UniversityClientApp.Controllers return View(); } + [HttpGet] public IActionResult Enter() { return View(); } + [HttpPost] + public void Enter(string login, string password) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + { + throw new Exception(" "); + } + APIStorekeeper.Client = APIStorekeeper.GetRequest($"api/user/loginstorekeeper?login={login}&password={password}"); + if (APIStorekeeper.Client == null) + { + throw new Exception(" /"); + } + Response.Redirect("Index"); + } + [HttpGet] + public IActionResult Register() + { + return View(); + } + [HttpPost] + public void Register(string login, string password, string email) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(email)) + { + throw new Exception(" , "); + } + APIStorekeeper.PostRequest("api/user/registerstorekeeper", new UserBindingModel + { + Login = login, + Email = email, + Password = password, + Role = UserRole. + }); + Response.Redirect("Enter"); + return; + } - public IActionResult Disciplines() + public IActionResult Disciplines() { return View(); } @@ -47,11 +88,6 @@ namespace UniversityClientApp.Controllers return View(); } - public IActionResult Register() - { - return View(); - } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/University/UniversityClientApp/Program.cs b/University/UniversityClientApp/Program.cs index 4ac679a..5ad5e46 100644 --- a/University/UniversityClientApp/Program.cs +++ b/University/UniversityClientApp/Program.cs @@ -1,16 +1,19 @@ +using UniversityClientAppStorekeeper; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddControllersWithViews(); +builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); var app = builder.Build(); +APIStorekeeper.Connect(builder.Configuration); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { - app.UseExceptionHandler("/Home/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); + app.UseExceptionHandler("/Home/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); } app.UseHttpsRedirection(); @@ -21,7 +24,7 @@ app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( - name: "default", - pattern: "{controller=Home}/{action=Index}/{id?}"); + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); diff --git a/University/UniversityClientApp/UniversityClientAppStorekeeper.csproj b/University/UniversityClientApp/UniversityClientAppStorekeeper.csproj index ec81bd6..c92e851 100644 --- a/University/UniversityClientApp/UniversityClientAppStorekeeper.csproj +++ b/University/UniversityClientApp/UniversityClientAppStorekeeper.csproj @@ -13,6 +13,11 @@ + + + + + diff --git a/University/UniversityClientApp/Views/Home/Index.cshtml b/University/UniversityClientApp/Views/Home/Index.cshtml index 7c07625..c84b9a3 100644 --- a/University/UniversityClientApp/Views/Home/Index.cshtml +++ b/University/UniversityClientApp/Views/Home/Index.cshtml @@ -3,7 +3,17 @@ }
- + @{ + if (Model == null) + { +

Log in

+ Enter + Register + return; + } + } + + diff --git a/University/UniversityClientApp/Views/Home/Register.cshtml b/University/UniversityClientApp/Views/Home/Register.cshtml index eb0880b..bec131d 100644 --- a/University/UniversityClientApp/Views/Home/Register.cshtml +++ b/University/UniversityClientApp/Views/Home/Register.cshtml @@ -2,36 +2,24 @@ ViewData["Title"] = "Register"; }
-

Регистрация

+

Registration

-
Логин:
-
+
Login:
+
-
Пароль:
+
Password:
-
Фамилия:
-
+
Email:
+
-
-
Имя:
-
-
-
-
Отчество:
-
-
-
-
Телефон:
-
-
-
diff --git a/University/UniversityClientApp/appsettings.json b/University/UniversityClientApp/appsettings.json index 4d56694..0774fc8 100644 --- a/University/UniversityClientApp/appsettings.json +++ b/University/UniversityClientApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} + "AllowedHosts": "*", + "IPAddress": "http://localhost:5032/" +} \ No newline at end of file diff --git a/University/UniversityDatabaseImplement/Implements/UserStorage.cs b/University/UniversityDatabaseImplement/Implements/UserStorage.cs index 60ebf7b..5b90f71 100644 --- a/University/UniversityDatabaseImplement/Implements/UserStorage.cs +++ b/University/UniversityDatabaseImplement/Implements/UserStorage.cs @@ -30,16 +30,18 @@ namespace UniversityDatabaseImplement.Implements } public UserViewModel? GetElement(UserSearchModel model) { - if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password)) { return null; } using var context = new UniversityDatabase(); - //Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли) + //Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли) + // UPD По роли не ищется if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue) { - return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)?.GetViewModel; + + return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password)?.GetViewModel; } //Получение по логину (пользователей с таким логином будет 1 или 0) if (!string.IsNullOrEmpty(model.Login)) diff --git a/University/UniversityDatabaseImplement/UniversityDatabase.cs b/University/UniversityDatabaseImplement/UniversityDatabase.cs index 3eebb86..07213d0 100644 --- a/University/UniversityDatabaseImplement/UniversityDatabase.cs +++ b/University/UniversityDatabaseImplement/UniversityDatabase.cs @@ -11,7 +11,7 @@ namespace UniversityDatabaseImplement if (optionsBuilder.IsConfigured == false) { //Возможно понадобится писать вместо (localdb) название пк, вот пк Егора: DESKTOP-N8BRIPR; other-name: LAPTOP-DYCTATOR; other-name: DyCTaTOR - optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-DYCTATOR\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-N8BRIPR\SQLEXPRESS;Initial Catalog=UniversityDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/University/UniversityRestApi/Controllers/UserController.cs b/University/UniversityRestApi/Controllers/UserController.cs index 32cdd80..5bc8a2d 100644 --- a/University/UniversityRestApi/Controllers/UserController.cs +++ b/University/UniversityRestApi/Controllers/UserController.cs @@ -45,7 +45,17 @@ namespace UniversityRestApi.Controllers { try { - return _logic.ReadElement(new UserSearchModel + + // + var x = _logic.ReadElement(new UserSearchModel + { + Login = login, + Password = password, + Role = UserRole.Кладовщик + }); + // + + return _logic.ReadElement(new UserSearchModel { Login = login, Password = password, diff --git a/University/UniversityRestApi/UniversityRestApi.http b/University/UniversityRestApi/UniversityRestApi.http deleted file mode 100644 index e9abc7b..0000000 --- a/University/UniversityRestApi/UniversityRestApi.http +++ /dev/null @@ -1,6 +0,0 @@ -@UniversityRestApi_HostAddress = http://localhost:5032 - -GET {{UniversityRestApi_HostAddress}}/weatherforecast/ -Accept: application/json - -###