base logic #5

Merged
mfnefd merged 25 commits from registration into main 2024-06-10 12:27:55 +04:00
Showing only changes of commit 054ed4b461 - Show all commits

View File

@ -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.");
}
}
}
}