renis-backend/Services/Polis/PolisService.cs

51 lines
1.6 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.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;
}
}
}
}