From 97d53618f6e02eec29b721d81bc946e1bc70c414 Mon Sep 17 00:00:00 2001 From: ereshkigal Date: Sat, 7 Sep 2024 17:09:18 +0300 Subject: [PATCH] services --- Services/Cars/CarsService.cs | 55 +++++++++++++++++++++++ Services/Cars/ICarsService.cs | 12 +++++ Services/Polis/IPolisService.cs | 5 ++- Services/Polis/PolisService.cs | 51 +++++++++++++++++++++ Services/UserPolises/IUserPolisService.cs | 13 ++++++ Services/UserPolises/UserPolisService.cs | 24 ++++++++++ Services/Users/IUserService.cs | 4 +- Services/Users/UserService.cs | 8 ++-- 8 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 Services/Cars/CarsService.cs create mode 100644 Services/Cars/ICarsService.cs create mode 100644 Services/Polis/PolisService.cs create mode 100644 Services/UserPolises/IUserPolisService.cs create mode 100644 Services/UserPolises/UserPolisService.cs diff --git a/Services/Cars/CarsService.cs b/Services/Cars/CarsService.cs new file mode 100644 index 0000000..2ebcef3 --- /dev/null +++ b/Services/Cars/CarsService.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Renis.Database.Models; +using Renis.Repositories; + +namespace renis_backend.Services.Cars +{ + public class CarsService : ICarsService + { + private readonly ICarRepository _carsRepository; + private readonly ILogger _logger; + public CarsService( ICarRepository carsRepository, ILogger logger) + { + _carsRepository = carsRepository; + _logger = logger; + } + public async Task GenerateCar(Renis.Database.Models.Polis polis, User user) + { + try + { + if(await _carsRepository.AddCar(new Car() + { + PolisId = polis.Id, + RespId = user.Id, + Verified = false, + Polis = polis, + Resp = user, + Number = GenerateRussianCarNumber() + + })) + { + return await _carsRepository.GetCarByNumber(GenerateRussianCarNumber()); + } + throw new Exception("Car not created"); + } + catch(Exception ex) + { + _logger.LogError(ex.Message); + throw; + } + } + private string GenerateRussianCarNumber() + { + string letters = "АВЕКМНОРСТУХ"; + string numbers = "0123456789"; + string regionCode = new string(Enumerable.Repeat(numbers, 2).Select(c => c[new Random().Next(c.Length)]).ToArray()); + string series = new string(Enumerable.Repeat(letters, 3).Select(c => c[new Random().Next(c.Length)]).ToArray()); + string number = new string(Enumerable.Repeat(numbers, 3).Select(c => c[new Random().Next(c.Length)]).ToArray()); + return $"{series} {number} {regionCode}"; + } + + } +} \ No newline at end of file diff --git a/Services/Cars/ICarsService.cs b/Services/Cars/ICarsService.cs new file mode 100644 index 0000000..61e9c5f --- /dev/null +++ b/Services/Cars/ICarsService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace renis_backend.Services.Cars +{ + public interface ICarsService + { + public Task GenerateCar(Renis.Database.Models.Polis polis, Renis.Database.Models.User user); + } +} \ No newline at end of file diff --git a/Services/Polis/IPolisService.cs b/Services/Polis/IPolisService.cs index 8bec7f3..e86f0de 100644 --- a/Services/Polis/IPolisService.cs +++ b/Services/Polis/IPolisService.cs @@ -3,10 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Renis.Database.Models; -namespace renis_backend.Services.Polis +namespace renis_backend.Services { public interface IPolisService { - public Task> GetPolises(); + public Task GeneratePolis(); + public Task GetPolis(long id); } } \ No newline at end of file diff --git a/Services/Polis/PolisService.cs b/Services/Polis/PolisService.cs new file mode 100644 index 0000000..096faa1 --- /dev/null +++ b/Services/Polis/PolisService.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Renis.Database.Models; +using Renis.Repositories; + +namespace renis_backend.Services.Polis +{ + public class PolisService : IPolisService + { + private readonly IPolisRepository _repository; + private readonly ILogger _logger; + + public PolisService(IPolisRepository repository, ILogger logger) + { + _repository = repository; + _logger = logger; + } + + public async Task GeneratePolis() + { + try + { + string number = new string(Enumerable.Repeat("0123456789", 9).Select(c => c[new Random().Next(c.Length)]).ToArray()); + await _repository.AddPolis( new Renis.Database.Models.Polis(){ + Number = $"{number.Substring(0, 3)}-{number.Substring(3, 3)}-{number.Substring(6, 3)}", + }); + return await _repository.GetPolisByNumber($"{number.Substring(0, 3)}-{number.Substring(3, 3)}-{number.Substring(6, 3)}"); + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + throw; + } + } + + public async Task GetPolis(long id) + { + try + { + return await _repository.GetPolisById(id); + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + throw; + } + } + } +} \ No newline at end of file diff --git a/Services/UserPolises/IUserPolisService.cs b/Services/UserPolises/IUserPolisService.cs new file mode 100644 index 0000000..0293754 --- /dev/null +++ b/Services/UserPolises/IUserPolisService.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Renis.Database.Models; + +namespace renis_backend.Services.UserPolises +{ + public interface IUserPolisService + { + public Task AddUserPolis(User user, Renis.Database.Models.Polis polis); + } +} \ No newline at end of file diff --git a/Services/UserPolises/UserPolisService.cs b/Services/UserPolises/UserPolisService.cs new file mode 100644 index 0000000..0f51135 --- /dev/null +++ b/Services/UserPolises/UserPolisService.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Renis.Database.Models; +using Renis.Repositories; + +namespace renis_backend.Services.UserPolises +{ + public class UserPolisService : IUserPolisService + { + private readonly IUserPolisRepository _userPolisRepository; + private readonly Logger _logger; + public UserPolisService( IUserPolisRepository userPolisRepository, Logger logger) + { + _userPolisRepository = userPolisRepository; + _logger = logger; + } + public Task AddUserPolis(User user, Renis.Database.Models.Polis polis) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Services/Users/IUserService.cs b/Services/Users/IUserService.cs index aa24b45..a53ab9c 100644 --- a/Services/Users/IUserService.cs +++ b/Services/Users/IUserService.cs @@ -2,11 +2,13 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Renis.Database.Models; namespace renis_backend.Services.Users { public interface IUserService { - public Task GenerateUser(); + public Task GenerateUser(); + public Task GetUserAsync(string phone); } } \ No newline at end of file diff --git a/Services/Users/UserService.cs b/Services/Users/UserService.cs index 006d810..a65886c 100644 --- a/Services/Users/UserService.cs +++ b/Services/Users/UserService.cs @@ -15,12 +15,10 @@ namespace renis_backend.Services.Users { private readonly ILogger _logger; private readonly IUserRepository _userRepository; - private readonly IJwtService _jwtService; - public UserService( ILogger logger, IUserRepository userRepository, IJwtService jwtService) + public UserService( ILogger logger, IUserRepository userRepository) { _logger = logger; _userRepository = userRepository; - _jwtService = jwtService; } @@ -45,7 +43,7 @@ namespace renis_backend.Services.Users throw new UserException(ex.Message); } } - public async Task GenerateUser() + public async Task GenerateUser() { try { @@ -66,7 +64,7 @@ namespace renis_backend.Services.Users }; if(await _userRepository.AddUser(user)) { - return _jwtService.GenerateAccessToken(user); + return await _userRepository.GetUserByPhone(phone); } throw new UserException("User already exists"); }