2024-09-07 09:45:00 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2024-09-07 12:13:38 +00:00
|
|
|
using System.Globalization;
|
2024-09-07 09:45:00 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2024-09-07 12:13:38 +00:00
|
|
|
using Faker;
|
2024-09-07 12:24:05 +00:00
|
|
|
using Renis.Database.Models;
|
2024-09-07 12:13:38 +00:00
|
|
|
using Renis.Repositories;
|
2024-09-07 12:24:05 +00:00
|
|
|
using Renis.Services.Jwt;
|
|
|
|
using renis_backend.DTO;
|
|
|
|
using renis_backend.Exceptions.User;
|
2024-09-07 09:45:00 +00:00
|
|
|
namespace renis_backend.Services.Users
|
|
|
|
{
|
|
|
|
public class UserService : IUserService
|
|
|
|
{
|
2024-09-07 12:13:38 +00:00
|
|
|
private readonly ILogger<UserService> _logger;
|
|
|
|
private readonly IUserRepository _userRepository;
|
2024-09-07 14:09:18 +00:00
|
|
|
public UserService( ILogger<UserService> logger, IUserRepository userRepository)
|
2024-09-07 12:24:05 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_userRepository = userRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<User> 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);
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 14:09:18 +00:00
|
|
|
public async Task<User> GenerateUser()
|
2024-09-07 09:45:00 +00:00
|
|
|
{
|
2024-09-07 12:13:38 +00:00
|
|
|
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
|
|
|
|
};
|
2024-09-07 12:24:05 +00:00
|
|
|
if(await _userRepository.AddUser(user))
|
|
|
|
{
|
2024-09-07 14:09:18 +00:00
|
|
|
return await _userRepository.GetUserByPhone(phone);
|
2024-09-07 12:24:05 +00:00
|
|
|
}
|
|
|
|
throw new UserException("User already exists");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex.Message);
|
|
|
|
if(ex is UserException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
throw new UserException(ex.Message);
|
2024-09-07 12:13:38 +00:00
|
|
|
}
|
2024-09-07 09:45:00 +00:00
|
|
|
}
|
2024-09-07 12:13:38 +00:00
|
|
|
|
|
|
|
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}";
|
|
|
|
}
|
2024-09-07 09:45:00 +00:00
|
|
|
}
|
2024-09-07 12:13:38 +00:00
|
|
|
}
|