services
This commit is contained in:
parent
c1b3843e2a
commit
97d53618f6
55
Services/Cars/CarsService.cs
Normal file
55
Services/Cars/CarsService.cs
Normal 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}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
12
Services/Cars/ICarsService.cs
Normal file
12
Services/Cars/ICarsService.cs
Normal 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);
|
||||
}
|
||||
}
|
@ -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<IQueryable<Renis.Database.Models.Polis>> GetPolises();
|
||||
public Task<Renis.Database.Models.Polis> GeneratePolis();
|
||||
public Task<Renis.Database.Models.Polis> GetPolis(long id);
|
||||
}
|
||||
}
|
51
Services/Polis/PolisService.cs
Normal file
51
Services/Polis/PolisService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
Services/UserPolises/IUserPolisService.cs
Normal file
13
Services/UserPolises/IUserPolisService.cs
Normal 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);
|
||||
}
|
||||
}
|
24
Services/UserPolises/UserPolisService.cs
Normal file
24
Services/UserPolises/UserPolisService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<string> GenerateUser();
|
||||
public Task<User> GenerateUser();
|
||||
public Task<User> GetUserAsync(string phone);
|
||||
}
|
||||
}
|
@ -15,12 +15,10 @@ namespace renis_backend.Services.Users
|
||||
{
|
||||
private readonly ILogger<UserService> _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IJwtService _jwtService;
|
||||
public UserService( ILogger<UserService> logger, IUserRepository userRepository, IJwtService jwtService)
|
||||
public UserService( ILogger<UserService> 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<string> GenerateUser()
|
||||
public async Task<User> 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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user