renis-backend/Services/Cars/CarsService.cs
2024-09-07 17:09:18 +03:00

55 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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}";
}
}
}