renis-backend/Services/Cars/CarsService.cs

55 lines
1.9 KiB
C#
Raw Normal View History

2024-09-07 14:09:18 +00:00
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}";
}
}
}