dev #12
@ -21,147 +21,150 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace BusinessLogic.BusinessLogic
|
namespace BusinessLogic.BusinessLogic
|
||||||
{
|
{
|
||||||
public class UserLogic : IUserLogic
|
public class UserLogic : IUserLogic
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IUserStorage _userStorage;
|
private readonly IUserStorage _userStorage;
|
||||||
|
|
||||||
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_userStorage = userStorage;
|
_userStorage = userStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Create(UserBindingModel model)
|
public string Create(UserBindingModel model)
|
||||||
{
|
{
|
||||||
// Проверяем модель
|
// Проверяем модель
|
||||||
_validate(model);
|
_validate(model);
|
||||||
var usr = _userStorage.GetElement(new() { Email = model.Email });
|
var usr = _userStorage.GetElement(new() { Email = model.Email });
|
||||||
if (usr is not null)
|
if (usr is not null)
|
||||||
{
|
{
|
||||||
throw new AccountException("An account with that email already exists.");
|
throw new AccountException("An account with that email already exists.");
|
||||||
}
|
}
|
||||||
// Хешируем пароль
|
// Хешируем пароль
|
||||||
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
||||||
var user = _userStorage.Insert(model);
|
model.Birthday = model.Birthday.ToUniversalTime();
|
||||||
if (user is null)
|
|
||||||
{
|
|
||||||
throw new Exception("Insert operation failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
MailSender.Send(new MailRegistration(user));
|
var user = _userStorage.Insert(model);
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
throw new Exception("Insert operation failed.");
|
||||||
|
}
|
||||||
|
|
||||||
return JwtProvider.Generate(user);
|
MailSender.Send(new MailRegistration(user));
|
||||||
}
|
|
||||||
|
|
||||||
public UserViewModel Delete(UserSearchModel model)
|
return JwtProvider.Generate(user);
|
||||||
{
|
}
|
||||||
ArgumentNullException.ThrowIfNull(model);
|
|
||||||
|
|
||||||
_logger.LogInformation("Delete user. Id: {0}", model.Id);
|
public UserViewModel Delete(UserSearchModel model)
|
||||||
var user = _userStorage.Delete(model);
|
{
|
||||||
if (user is null)
|
ArgumentNullException.ThrowIfNull(model);
|
||||||
{
|
|
||||||
throw new ElementNotFoundException();
|
|
||||||
}
|
|
||||||
MailSender.Send(new MailDeleteUser(user));
|
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
_logger.LogInformation("Delete user. Id: {0}", model.Id);
|
||||||
}
|
var user = _userStorage.Delete(model);
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException();
|
||||||
|
}
|
||||||
|
MailSender.Send(new MailDeleteUser(user));
|
||||||
|
|
||||||
public IEnumerable<UserViewModel> ReadElements(UserSearchModel? model)
|
return UserConverter.ToView(user);
|
||||||
{
|
}
|
||||||
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
|
||||||
var list = _userStorage.GetList(model);
|
|
||||||
if (list is null || list.Count() == 0)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("ReadList return null list");
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
|
|
||||||
|
|
||||||
return list.Select(UserConverter.ToView);
|
public IEnumerable<UserViewModel> ReadElements(UserSearchModel? model)
|
||||||
}
|
{
|
||||||
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
||||||
|
var list = _userStorage.GetList(model);
|
||||||
|
if (list is null || list.Count() == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadList return null list");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
|
||||||
|
|
||||||
public UserViewModel ReadElement(UserSearchModel model)
|
return list.Select(UserConverter.ToView);
|
||||||
{
|
}
|
||||||
ArgumentNullException.ThrowIfNull(model);
|
|
||||||
|
|
||||||
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
|
public UserViewModel ReadElement(UserSearchModel model)
|
||||||
var user = _userStorage.GetElement(model);
|
{
|
||||||
if (user is null)
|
ArgumentNullException.ThrowIfNull(model);
|
||||||
{
|
|
||||||
throw new ElementNotFoundException();
|
|
||||||
}
|
|
||||||
_logger.LogInformation("ReadElement find. Id: {0}", user.Id);
|
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
|
||||||
}
|
var user = _userStorage.GetElement(model);
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
throw new ElementNotFoundException();
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement find. Id: {0}", user.Id);
|
||||||
|
|
||||||
public UserViewModel Update(UserBindingModel model)
|
return UserConverter.ToView(user);
|
||||||
{
|
}
|
||||||
_validate(model);
|
|
||||||
|
|
||||||
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
public UserViewModel Update(UserBindingModel model)
|
||||||
var user = _userStorage.Update(model);
|
{
|
||||||
if (user is null)
|
_validate(model);
|
||||||
{
|
|
||||||
throw new Exception("Update operation failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
MailSender.Send(new MailUpdateUserData(user));
|
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
||||||
|
model.Birthday = model.Birthday.ToUniversalTime();
|
||||||
|
var user = _userStorage.Update(model);
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
throw new Exception("Update operation failed.");
|
||||||
|
}
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
MailSender.Send(new MailUpdateUserData(user));
|
||||||
}
|
|
||||||
|
|
||||||
public string Login(string email, string password)
|
return UserConverter.ToView(user);
|
||||||
{
|
}
|
||||||
_isValidEmail(email);
|
|
||||||
var user = _userStorage.GetElement(new() { Email = email });
|
|
||||||
|
|
||||||
if (user is null)
|
public string Login(string email, string password)
|
||||||
{
|
{
|
||||||
throw new ElementNotFoundException();
|
_isValidEmail(email);
|
||||||
}
|
var user = _userStorage.GetElement(new() { Email = email });
|
||||||
// Проверяем пароль
|
|
||||||
_isValidPassword(password);
|
|
||||||
if (!PasswordHasher.Verify(password, user.PasswordHash))
|
|
||||||
{
|
|
||||||
throw new AccountException("The passwords don't match.");
|
|
||||||
}
|
|
||||||
return JwtProvider.Generate(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void _validate(UserBindingModel model)
|
if (user is null)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(model);
|
throw new ElementNotFoundException();
|
||||||
_isValidPassword(model.Password);
|
}
|
||||||
_isValidEmail(model.Email);
|
// Проверяем пароль
|
||||||
}
|
_isValidPassword(password);
|
||||||
|
if (!PasswordHasher.Verify(password, user.PasswordHash))
|
||||||
|
{
|
||||||
|
throw new AccountException("The passwords don't match.");
|
||||||
|
}
|
||||||
|
return JwtProvider.Generate(user);
|
||||||
|
}
|
||||||
|
|
||||||
private void _isValidPassword(string? password)
|
private void _validate(UserBindingModel model)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(password))
|
ArgumentNullException.ThrowIfNull(model);
|
||||||
{
|
_isValidPassword(model.Password);
|
||||||
throw new AccountException("The password is null.");
|
_isValidEmail(model.Email);
|
||||||
}
|
}
|
||||||
var hasMin8Max15Chars = new Regex(@".{8,15}");
|
|
||||||
if (!hasMin8Max15Chars.IsMatch(password))
|
|
||||||
{
|
|
||||||
throw new AccountException("The password must not be less than 8 or more than 15 characters long.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void _isValidEmail(string? email)
|
private void _isValidPassword(string? password)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(email))
|
if (string.IsNullOrWhiteSpace(password))
|
||||||
{
|
{
|
||||||
throw new AccountException("The email is null.");
|
throw new AccountException("The password is null.");
|
||||||
}
|
}
|
||||||
if (!MailAddress.TryCreate(email, out _))
|
var hasMin8Max15Chars = new Regex(@".{8,15}");
|
||||||
{
|
if (!hasMin8Max15Chars.IsMatch(password))
|
||||||
throw new AccountException("The email is not valid.");
|
{
|
||||||
}
|
throw new AccountException("The password must not be less than 8 or more than 15 characters long.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void _isValidEmail(string? email)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(email))
|
||||||
|
{
|
||||||
|
throw new AccountException("The email is null.");
|
||||||
|
}
|
||||||
|
if (!MailAddress.TryCreate(email, out _))
|
||||||
|
{
|
||||||
|
throw new AccountException("The email is not valid.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user