namespace Diligent.WebAPI.Business.Services { public class InsurersService : IInsurersService { private readonly DatabaseContext _context; private readonly IMapper _mapper; public InsurersService(DatabaseContext context, IMapper mapper) { _context = context; _mapper = mapper; } public async Task> GetInsurers() { var insurers = await _context.Insurers.Include(x => x.InsuranceCompany).ToListAsync(); return _mapper.Map>(insurers); } public async Task GetInsurer(long id) { var insurer = await _context.Insurers .Include(x => x.InsuranceCompany) .FirstOrDefaultAsync(x => x.Id == id); if (insurer == null) throw new EntityNotFoundException("Insurer not found"); return _mapper.Map(insurer); } public async Task CreateInsurer(InsurerCreateDto insurerCreateDto) { var insurer = _mapper.Map(insurerCreateDto); await _context.Insurers.AddAsync(insurer); await _context.SaveChangesAsync(); } public async Task UpdateInsurer(long insurerId, InsurerUpdateDto insurerUpdateDto) { var insurer = _context.InsurancePolicies.Find(insurerId); if (insurer == null) throw new EntityNotFoundException("Insurer not found"); _mapper.Map(insurerUpdateDto, insurer); insurer.UpdatedAtUtc = DateTime.UtcNow; _context.Entry(insurer).State = EntityState.Modified; await _context.SaveChangesAsync(); } public async Task DeleteInsurerAsync(long insurerId) { var insurer = _context.InsurancePolicies.Find(insurerId); if (insurer == null) throw new EntityNotFoundException("Insurer not found"); _context.Remove(insurer); await _context.SaveChangesAsync(); } } }