From c1b3843e2a3635885edea315b37cd88a5b42a920 Mon Sep 17 00:00:00 2001 From: ereshkigal Date: Sat, 7 Sep 2024 15:24:05 +0300 Subject: [PATCH] user service operaional --- Services/Users/UserService.cs | 52 ++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) 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}"; } - - } }