renis-backend/Services/Users/UserService.cs

69 lines
2.8 KiB
C#
Raw Normal View History

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;
using Renis.Repositories;
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 09:45:00 +00:00
public async Task<string> GenerateUser()
{
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
};
await _userRepository.AddUser(user);
return user.Phone;
}
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
}