fix datetime error
This commit is contained in:
parent
ff3d4b3824
commit
b1d13a591d
@ -21,147 +21,150 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class UserLogic : IUserLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserStorage _userStorage;
|
||||
public class UserLogic : IUserLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserStorage _userStorage;
|
||||
|
||||
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_userStorage = userStorage;
|
||||
}
|
||||
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_userStorage = userStorage;
|
||||
}
|
||||
|
||||
public string Create(UserBindingModel model)
|
||||
{
|
||||
// Проверяем модель
|
||||
_validate(model);
|
||||
var usr = _userStorage.GetElement(new() { Email = model.Email });
|
||||
if (usr is not null)
|
||||
{
|
||||
throw new AccountException("An account with that email already exists.");
|
||||
}
|
||||
// Хешируем пароль
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
||||
var user = _userStorage.Insert(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new Exception("Insert operation failed.");
|
||||
}
|
||||
public string Create(UserBindingModel model)
|
||||
{
|
||||
// Проверяем модель
|
||||
_validate(model);
|
||||
var usr = _userStorage.GetElement(new() { Email = model.Email });
|
||||
if (usr is not null)
|
||||
{
|
||||
throw new AccountException("An account with that email already exists.");
|
||||
}
|
||||
// Хешируем пароль
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
||||
model.Birthday = model.Birthday.ToUniversalTime();
|
||||
|
||||
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)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
return JwtProvider.Generate(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 UserViewModel Delete(UserSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
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)
|
||||
{
|
||||
_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 UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
return list.Select(UserConverter.ToView);
|
||||
}
|
||||
|
||||
_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 ReadElement(UserSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
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)
|
||||
{
|
||||
_validate(model);
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password!);
|
||||
var user = _userStorage.Update(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new Exception("Update operation failed.");
|
||||
}
|
||||
public UserViewModel Update(UserBindingModel model)
|
||||
{
|
||||
_validate(model);
|
||||
|
||||
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)
|
||||
{
|
||||
_isValidEmail(email);
|
||||
var user = _userStorage.GetElement(new() { Email = email });
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
// Проверяем пароль
|
||||
_isValidPassword(password);
|
||||
if (!PasswordHasher.Verify(password, user.PasswordHash))
|
||||
{
|
||||
throw new AccountException("The passwords don't match.");
|
||||
}
|
||||
return JwtProvider.Generate(user);
|
||||
}
|
||||
public string Login(string email, string password)
|
||||
{
|
||||
_isValidEmail(email);
|
||||
var user = _userStorage.GetElement(new() { Email = email });
|
||||
|
||||
private void _validate(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
_isValidPassword(model.Password);
|
||||
_isValidEmail(model.Email);
|
||||
}
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
// Проверяем пароль
|
||||
_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)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new AccountException("The password is null.");
|
||||
}
|
||||
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 _validate(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
_isValidPassword(model.Password);
|
||||
_isValidEmail(model.Email);
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
private void _isValidPassword(string? password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new AccountException("The password is null.");
|
||||
}
|
||||
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)
|
||||
{
|
||||
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…
Reference in New Issue
Block a user