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