diff --git a/UniversityDataBaseImplemet/Implements/UserStorage.cs b/UniversityDataBaseImplemet/Implements/UserStorage.cs index 0c310b1..2ef0592 100644 --- a/UniversityDataBaseImplemet/Implements/UserStorage.cs +++ b/UniversityDataBaseImplemet/Implements/UserStorage.cs @@ -26,16 +26,21 @@ namespace UniversityDataBaseImplemet.Implements public UserViewModel? GetElement(UserSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } - using var context = new Database(); - return context.User - .Include(record => record.Role) - .FirstOrDefault(record => record.Id.Equals(model.Id)) - ?.GetViewModel; - } + using var context = new Database(); + if (model.Id.HasValue) + { + return context.User + .FirstOrDefault(x => (x.Id == model.Id)) + ?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue) + { + return context.User + .FirstOrDefault(x => (x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)) + ?.GetViewModel; + } + return null; + } public List GetFilteredList(UserSearchModel model) { diff --git a/UniversityProvider/APIClient.cs b/UniversityProvider/APIClient.cs new file mode 100644 index 0000000..9ef18e1 --- /dev/null +++ b/UniversityProvider/APIClient.cs @@ -0,0 +1,47 @@ +using Newtonsoft.Json; +using System.Net.Http.Headers; +using System.Text; +using UniversityContracts.ViewModels; + +namespace UniversityProvider +{ + public static class APIClient + { + private static readonly HttpClient _user = new(); + + public static UserViewModel? User { get; set; } = null; + + public static void Connect(IConfiguration configuration) + { + _user.BaseAddress = new Uri(configuration["IPAddress"]); + _user.DefaultRequestHeaders.Accept.Clear(); + _user.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + public static T? GetRequest(string requestUrl) + { + var response = _user.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 = _user.PostAsync(requestUrl, data); + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) + { + throw new Exception(result); + } + } + } +} diff --git a/UniversityProvider/Controllers/HomeController.cs b/UniversityProvider/Controllers/HomeController.cs index 5baac32..bc9c706 100644 --- a/UniversityProvider/Controllers/HomeController.cs +++ b/UniversityProvider/Controllers/HomeController.cs @@ -1,27 +1,66 @@ using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; +using UniversityModels.Enums; using UniversityProvider.Models; namespace UniversityProvider.Controllers { public class HomeController : Controller { - private readonly ILogger _logger; - - public HomeController(ILogger logger) - { - _logger = logger; - } + public HomeController(){} public IActionResult Index() { - return View(); - } + if (APIClient.User == null) + { + return Redirect("~/Home/Login"); + } + return View(); + } - public IActionResult Privacy() - { - return View(); - } + public IActionResult Login() + { + return View(); + } + + [HttpPost] + public void Login(string login, string password) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIClient.User = APIClient.GetRequest($"api/user/login?login={login}&password={password}&role={Role.Provider}"); + if (APIClient.User == null) + { + throw new Exception("Неверный логин/пароль"); + } + Response.Redirect("Index"); + } + + public IActionResult Registration() + { + return View(); + } + + [HttpPost] + public void Registration(string login, string password) + { + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) + { + throw new Exception("Введите логин и пароль"); + } + APIClient.PostRequest("api/user/register", new UserBindingModel + { + Login = login, + Password = password, + Role = Role.Provider + }); + Response.Redirect("Login"); + return; + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() diff --git a/UniversityProvider/Program.cs b/UniversityProvider/Program.cs index fb687e2..c577997 100644 --- a/UniversityProvider/Program.cs +++ b/UniversityProvider/Program.cs @@ -1,4 +1,5 @@ using System.IO.Pipes; +using UniversityProvider; var builder = WebApplication.CreateBuilder(args); @@ -7,7 +8,7 @@ builder.Services.AddControllersWithViews(); var app = builder.Build(); - +APIClient.Connect(builder.Configuration); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) diff --git a/UniversityProvider/Views/Home/Index.cshtml b/UniversityProvider/Views/Home/Index.cshtml index d2d19bd..cad901e 100644 --- a/UniversityProvider/Views/Home/Index.cshtml +++ b/UniversityProvider/Views/Home/Index.cshtml @@ -3,6 +3,5 @@ }
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

Добро пожаловать в курсовой проект University

diff --git a/UniversityProvider/Views/Home/Login.cshtml b/UniversityProvider/Views/Home/Login.cshtml index 3e5c9a2..b95c97f 100644 --- a/UniversityProvider/Views/Home/Login.cshtml +++ b/UniversityProvider/Views/Home/Login.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["Title"] = "Login"; + ViewData["Title"] = "Вход"; }
@@ -15,7 +15,13 @@
\ No newline at end of file diff --git a/UniversityProvider/Views/Home/Registration.cshtml b/UniversityProvider/Views/Home/Registration.cshtml index 2b000d6..509479b 100644 --- a/UniversityProvider/Views/Home/Registration.cshtml +++ b/UniversityProvider/Views/Home/Registration.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["Title"] = "Register"; + ViewData["Title"] = "Регистрация"; }
@@ -7,15 +7,21 @@
-
Логин:
-
+
Логин (эл.почта):
+
Пароль:
\ No newline at end of file