diff --git a/Services/Users/UserService.cs b/Services/Users/UserService.cs index 532cf6e..006d810 100644 --- a/Services/Users/UserService.cs +++ b/Services/Users/UserService.cs @@ -4,13 +4,47 @@ using System.Globalization; using System.Linq; using System.Threading.Tasks; using Faker; +using Renis.Database.Models; using Renis.Repositories; +using Renis.Services.Jwt; +using renis_backend.DTO; +using renis_backend.Exceptions.User; namespace renis_backend.Services.Users { public class UserService : IUserService { private readonly ILogger _logger; private readonly IUserRepository _userRepository; + private readonly IJwtService _jwtService; + public UserService( ILogger logger, IUserRepository userRepository, IJwtService jwtService) + { + _logger = logger; + _userRepository = userRepository; + _jwtService = jwtService; + } + + + public async Task GetUserAsync(string phone) + { + try + { + var user = await _userRepository.GetUserByPhone(phone); + if(user == null) + { + throw new UserException("User not found"); + } + return user; + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + if(ex is UserException) + { + throw; + } + throw new UserException(ex.Message); + } + } public async Task GenerateUser() { try @@ -30,8 +64,20 @@ namespace renis_backend.Services.Users Phone = GenerateRussianPhoneNumber(), Password = password }; - await _userRepository.AddUser(user); - return user.Phone; + if(await _userRepository.AddUser(user)) + { + return _jwtService.GenerateAccessToken(user); + } + throw new UserException("User already exists"); + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + if(ex is UserException) + { + throw; + } + throw new UserException(ex.Message); } } @@ -62,7 +108,5 @@ namespace renis_backend.Services.Users string number = new string(Enumerable.Repeat("0123456789", 10).Select(c => c[random.Next(c.Length)]).ToArray()); return $"{prefix}{number}"; } - - } }