using System; using System.Collections.Generic; 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; public UserService( ILogger logger, IUserRepository userRepository) { _logger = logger; _userRepository = userRepository; } 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 { string firstName = russianFirstNames[random.Next(0, russianFirstNames.Length)]; string lastName = russianLastNames[random.Next(0, russianLastNames.Length)]; string patronymic = russianPatronymics[random.Next(0, russianPatronymics.Length)]; string name = $"{lastName} {firstName} {patronymic}"; string phone = GenerateRussianPhoneNumber(); string password = new string(Enumerable.Repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10).Select(c => c[random.Next(c.Length)]).ToArray()); var user = new User { Name = name, Phone = GenerateRussianPhoneNumber(), Password = password }; if(await _userRepository.AddUser(user)) { return await _userRepository.GetUserByPhone(phone); } throw new UserException("User already exists"); } catch (Exception ex) { _logger.LogError(ex.Message); if(ex is UserException) { throw; } throw new UserException(ex.Message); } } private static Random random = new Random(); private static string[] russianFirstNames = new string[] { "Иван", "Петр", "Алексей", "Дмитрий", "Николай", "Сергей", "Александр", "Михаил", "Андрей", "Владимир" }; private static string[] russianLastNames = new string[] { "Иванов", "Петров", "Сидоров", "Кузнецов", "Попов", "Смирнов", "Николаев", "Зайцев", "Павлов", "Михайлов" }; private static string[] russianPatronymics = new string[] { "Иванович", "Петрович", "Алексеевич", "Дмитриевич", "Николаевич", "Сергеевич", "Александрович", "Михайлович", "Андреевич", "Владимирович" }; private static string[] russianPhonePrefixes = new string[] { "+7", "+375", "+380", "+7" }; private static string GenerateRussianPhoneNumber() { string prefix = russianPhonePrefixes[random.Next(0, russianPhonePrefixes.Length)]; string number = new string(Enumerable.Repeat("0123456789", 10).Select(c => c[random.Next(c.Length)]).ToArray()); return $"{prefix}{number}"; } } }