fix user logic
This commit is contained in:
parent
4f472bce41
commit
054ed4b461
@ -1,4 +1,5 @@
|
||||
using Contracts.BindingModels;
|
||||
using BusinessLogic.Tools;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.Converters;
|
||||
using Contracts.Exceptions;
|
||||
@ -28,7 +29,10 @@ namespace BusinessLogic.BusinessLogic
|
||||
public UserViewModel Create(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
// Проверяем пароль
|
||||
_validatePassword(model.Password);
|
||||
// Хешируем пароль
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password);
|
||||
var user = _userStorage.Insert(model);
|
||||
if (user is null)
|
||||
{
|
||||
@ -46,7 +50,7 @@ namespace BusinessLogic.BusinessLogic
|
||||
var user = _userStorage.Delete(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new Exception("Update operation failed.");
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
|
||||
return UserConverter.ToView(user);
|
||||
@ -85,6 +89,11 @@ namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
if (model.Password is not null)
|
||||
{
|
||||
_validatePassword(model.Password);
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password);
|
||||
}
|
||||
var user = _userStorage.Update(model);
|
||||
if (user is null)
|
||||
{
|
||||
@ -92,5 +101,32 @@ namespace BusinessLogic.BusinessLogic
|
||||
}
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public UserViewModel Login(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
var user = _userStorage.GetElement(new() { Email = model.Email });
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
// Проверяем пароль
|
||||
_validatePassword(model.Password);
|
||||
if (PasswordHasher.Verify(model.Password, user.PasswordHash))
|
||||
{
|
||||
throw new AccountException("The passwords don't match.");
|
||||
}
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public void _validatePassword(string? password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new AccountException("The password is null.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user