This commit is contained in:
ereshkigal 2024-09-07 17:09:18 +03:00
parent c1b3843e2a
commit 97d53618f6
8 changed files with 164 additions and 8 deletions

View File

@ -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<CarsService> _logger;
public CarsService( ICarRepository carsRepository, ILogger<CarsService> logger)
{
_carsRepository = carsRepository;
_logger = logger;
}
public async Task<Car> 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}";
}
}
}

View File

@ -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<Renis.Database.Models.Car> GenerateCar(Renis.Database.Models.Polis polis, Renis.Database.Models.User user);
}
}

View File

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Renis.Database.Models; using Renis.Database.Models;
namespace renis_backend.Services.Polis namespace renis_backend.Services
{ {
public interface IPolisService public interface IPolisService
{ {
public Task<IQueryable<Renis.Database.Models.Polis>> GetPolises(); public Task<Renis.Database.Models.Polis> GeneratePolis();
public Task<Renis.Database.Models.Polis> GetPolis(long id);
} }
} }

View File

@ -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<PolisService> _logger;
public PolisService(IPolisRepository repository, ILogger<PolisService> logger)
{
_repository = repository;
_logger = logger;
}
public async Task<Renis.Database.Models.Polis> 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<Renis.Database.Models.Polis> GetPolis(long id)
{
try
{
return await _repository.GetPolisById(id);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
throw;
}
}
}
}

View File

@ -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<Renis.Database.Models.UserPolis> AddUserPolis(User user, Renis.Database.Models.Polis polis);
}
}

View File

@ -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<UserPolisService> _logger;
public UserPolisService( IUserPolisRepository userPolisRepository, Logger<UserPolisService> logger)
{
_userPolisRepository = userPolisRepository;
_logger = logger;
}
public Task<UserPolis> AddUserPolis(User user, Renis.Database.Models.Polis polis)
{
throw new NotImplementedException();
}
}
}

View File

@ -2,11 +2,13 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Renis.Database.Models;
namespace renis_backend.Services.Users namespace renis_backend.Services.Users
{ {
public interface IUserService public interface IUserService
{ {
public Task<string> GenerateUser(); public Task<User> GenerateUser();
public Task<User> GetUserAsync(string phone);
} }
} }

View File

@ -15,12 +15,10 @@ namespace renis_backend.Services.Users
{ {
private readonly ILogger<UserService> _logger; private readonly ILogger<UserService> _logger;
private readonly IUserRepository _userRepository; private readonly IUserRepository _userRepository;
private readonly IJwtService _jwtService; public UserService( ILogger<UserService> logger, IUserRepository userRepository)
public UserService( ILogger<UserService> logger, IUserRepository userRepository, IJwtService jwtService)
{ {
_logger = logger; _logger = logger;
_userRepository = userRepository; _userRepository = userRepository;
_jwtService = jwtService;
} }
@ -45,7 +43,7 @@ namespace renis_backend.Services.Users
throw new UserException(ex.Message); throw new UserException(ex.Message);
} }
} }
public async Task<string> GenerateUser() public async Task<User> GenerateUser()
{ {
try try
{ {
@ -66,7 +64,7 @@ namespace renis_backend.Services.Users
}; };
if(await _userRepository.AddUser(user)) if(await _userRepository.AddUser(user))
{ {
return _jwtService.GenerateAccessToken(user); return await _userRepository.GetUserByPhone(phone);
} }
throw new UserException("User already exists"); throw new UserException("User already exists");
} }