Регистрция + Вход

This commit is contained in:
Danil Markov 2023-05-16 23:35:28 +04:00
parent 752868c67f
commit 295dc0ad1d
7 changed files with 136 additions and 33 deletions

View File

@ -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<UserViewModel> GetFilteredList(UserSearchModel model)
{

View File

@ -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<T>(string requestUrl)
{
var response = _user.GetAsync(requestUrl);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (response.Result.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(result);
}
else
{
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 = _user.PostAsync(requestUrl, data);
var result = response.Result.Content.ReadAsStringAsync().Result;
if (!response.Result.IsSuccessStatusCode)
{
throw new Exception(result);
}
}
}
}

View File

@ -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<HomeController> _logger;
public HomeController(ILogger<HomeController> 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<UserViewModel>($"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()

View File

@ -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())

View File

@ -3,6 +3,5 @@
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<h1>Добро пожаловать в курсовой проект University</h1>
</div>

View File

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "Login";
ViewData["Title"] = "Вход";
}
<div class="text-center">
@ -15,7 +15,13 @@
<div class="col-8"><input type="password" name="password" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Войти" class="btn btn-primary" /></div>
<div class="col-8">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Registration">
Нет аккаунта?
</a>
</div>
<div class="col-4">
<input type="submit" value="Войти" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "Register";
ViewData["Title"] = "Регистрация";
}
<div class="text-center">
@ -7,15 +7,21 @@
</div>
<form method="post">
<div class="row">
<div class="col-4">Логин:</div>
<div class="col-8"><input type="text" name="login" /></div>
<div class="col-4">Логин (эл.почта):</div>
<div class="col-8"><input type="email" name="login" /></div>
</div>
<div class="row">
<div class="col-4">Пароль:</div>
<div class="col-8"><input type="password" name="password" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Регистрация" class="btn btn-primary" /></div>
<div class="col-8">
<a class="nav-link text-primary" asp-area="" asp-controller="Home" asp-action="Login">
Есть аккаунт?
</a>
</div>
<div class="col-4">
<input type="submit" value="Регистрация" class="btn btn-primary" />
</div>
</div>
</form>