Fix db seeding, migration, repository services
This commit is contained in:
parent
872dc1e263
commit
6b87902ca7
22 changed files with 606 additions and 64 deletions
|
@ -3,12 +3,51 @@ namespace CSR.Application.Services;
|
|||
using CSR.Application.Interfaces;
|
||||
using CSR.Domain.Entities;
|
||||
|
||||
public class UserService(IUserRepository userRepository) : IUserService
|
||||
public class UserService(IUserRepository userRepository, Domain.Interfaces.IPasswordHasher passwordHasher) : IUserService
|
||||
{
|
||||
private readonly IUserRepository _userRepository = userRepository;
|
||||
private readonly Domain.Interfaces.IPasswordHasher _passwordHasher = passwordHasher;
|
||||
|
||||
public void CreateNewUser(string username, string email, string password, bool isAdmin = false)
|
||||
public async Task<IUserService.RegisterNewUserResult> RegisterNewUser
|
||||
(
|
||||
string username,
|
||||
string email,
|
||||
string password
|
||||
)
|
||||
{
|
||||
var errors = new List<string>();
|
||||
var (IsValid, Errors) = User.IsValidUsername(username);
|
||||
if (!IsValid)
|
||||
{
|
||||
errors.AddRange(Errors!);
|
||||
}
|
||||
|
||||
if (!User.IsValidEmail(email))
|
||||
{
|
||||
errors.Add("Invalid email address.");
|
||||
}
|
||||
|
||||
(IsValid, Errors) = User.IsValidPassword(password);
|
||||
if (!IsValid)
|
||||
{
|
||||
errors.AddRange(Errors!);
|
||||
}
|
||||
|
||||
var existingUser = await _userRepository.GetByUsernameAsync(username);
|
||||
if (existingUser != null)
|
||||
{
|
||||
errors.Add("Username already exists.");
|
||||
}
|
||||
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
return new IUserService.RegisterNewUserResult(null, errors);
|
||||
}
|
||||
|
||||
// create the new user
|
||||
var user = User.CreateNew(username, email, password, _passwordHasher);
|
||||
user = await _userRepository.AddAsync(user);
|
||||
|
||||
return new IUserService.RegisterNewUserResult(user, null);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue