сделаны контроллеры для юзеров (регистрация, личные данные, выход, регистрация)

This commit is contained in:
Елена Бакальская 2024-05-29 23:07:34 +04:00
parent 74dd85bd7e
commit f89a6e3db9
2 changed files with 111 additions and 5 deletions

View File

@ -1,10 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using PolyclinicBusinessLogic.BusinessLogics;
using PolyclinicBusinessLogic.OfficePackage;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Enums;
using PolyclinicDataModels.Models;
using PolyclinicWebAppSuretor.Models;
using System.Diagnostics;
@ -51,6 +53,12 @@ namespace PolyclinicWebAppSuretor.Controllers
return View();
}
/// <summary>
/// USER
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpGet]
[HttpPost]
public IActionResult Register(RegisterModel model)
@ -70,14 +78,112 @@ namespace PolyclinicWebAppSuretor.Controllers
model.FIO = model.FIO;
return View(model);
}
/*var user = new UserViewModel {
Email
};*/
var user = new UserBindingModel
{
FIO = model.FIO,
Email = model.Email,
Password = model.Password,
Role = UserRole.Ïîðó÷èòåëü
};
_userLogic.Create(user);
return RedirectToAction("Login");
}
else
{
return View();
return View(model);
}
}
[HttpGet]
[HttpPost]
public IActionResult Login(LoginModel model)
{
var errors = new List<string>();
if (HttpContext.Request.Method == "POST")
{
var user = _userLogic.ReadElement(new UserSearchModel { Email = model.Email, Password = model.Password });
if (user == null)
{
errors.Add("Íåâåðíûå ëîãèí èëè ïàðîëü");
}
else if (user.Role != UserRole.Ïîðó÷èòåëü)
{
errors.Add("Ïîëüçîâàòåëü èìååò íåðàçðåøåííóþ ðîëü");
}
if (errors.Count > 0)
{
model = new LoginModel
{
Errors = errors
};
return View(model);
}
LoginManager.LogginedUser = user;
return RedirectToAction("", "Home");
}
else
{
model = new();
return View(model);
}
}
[HttpPost]
public IActionResult Logout()
{
LoginManager.LogginedUser = null;
return RedirectToAction("Login");
}
[HttpGet]
[HttpPost]
public IActionResult Privacy(UserPrivacyModel model)
{
var currentUser = LoginManager.LogginedUser;
if (currentUser == null)
{
return RedirectToAction("Login");
}
if (HttpContext.Request.Method == "POST")
{
var errors = new List<string>();
var checkedUser = _userLogic.ReadElement(new UserSearchModel { Email = model.Email });
if (checkedUser != null && checkedUser.Id != LoginManager.LogginedUser.Id)
{
errors.Add("Ïîëüçîâàòåëü ñ òàêèì Email óæå åñòü");
}
if (model.Password != model.ConfirmPassword)
{
errors.Add("Ïàðîëè íå ñîâïàäàþò");
}
if (errors.Count > 0)
{
model.Errors = errors;
model.Password = string.Empty;
model.ConfirmPassword = string.Empty;
return View(model);
}
var user = new UserBindingModel
{
Id = currentUser.Id,
FIO = model.FIO,
Email = model.Email,
Password = model.Password.IsNullOrEmpty() ? LoginManager.LogginedUser.Password : model.Password,
};
_userLogic.Update(user);
LoginManager.LogginedUser = _userLogic.ReadElement(new UserSearchModel { Id = model.Id });
return RedirectToAction("Privacy");
}
else
{
model = new()
{
Id = currentUser.Id,
FIO = currentUser.FIO,
Email = currentUser.Email,
Role = currentUser.Role
};
return View(model);
}
}

View File

@ -3,7 +3,7 @@ using System.ComponentModel;
namespace PolyclinicWebAppSuretor.Models
{
public class UserPrivacyModel
public class UserPrivacyModel : RegisterModel
{
public int Id { get; set; }