| | | 1 | | using KT.Domain; |
| | | 2 | | using KT.Domain.Ports; |
| | | 3 | | using KT.ModularMonolith.Shared.Commons.Utils; |
| | | 4 | | using KT.Modules.Security.Presentation.Contracts; |
| | | 5 | | using KT.Modules.Security.Presentation.Dto; |
| | | 6 | | |
| | | 7 | | namespace KT.Application |
| | | 8 | | { |
| | 3 | 9 | | internal class RegisterService(IUserRepository userRepository) : IRegisterService |
| | | 10 | | { |
| | | 11 | | public async Task RegisterAsync(RegisterRequestDto registerRequest) |
| | 3 | 12 | | { |
| | 3 | 13 | | var loweredUsername = registerRequest.Username.ToLower(); |
| | 3 | 14 | | var loweredEmail = registerRequest.Email.ToLower(); |
| | 3 | 15 | | if (await userRepository.CheckExistUsername(loweredUsername)) |
| | 1 | 16 | | throw new InvalidOperationException("Username already registered."); |
| | 2 | 17 | | if (await userRepository.CheckExistEmail(loweredEmail)) |
| | 1 | 18 | | throw new InvalidOperationException("Email already registered."); |
| | 1 | 19 | | string hashId = HashUtils.GetSha256HexId(loweredUsername, loweredEmail); |
| | 1 | 20 | | string passwordHash = BCrypt.Net.BCrypt.HashPassword(registerRequest.Password); |
| | 1 | 21 | | var user = new User( |
| | 1 | 22 | | hashId, |
| | 1 | 23 | | registerRequest.Username, |
| | 1 | 24 | | registerRequest.RealName, |
| | 1 | 25 | | registerRequest.Email, |
| | 1 | 26 | | passwordHash |
| | 1 | 27 | | ); |
| | 1 | 28 | | await userRepository.SaveAsync(user); |
| | 1 | 29 | | } |
| | | 30 | | } |
| | | 31 | | } |