Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

InsurersService.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. namespace Diligent.WebAPI.Business.Services
  2. {
  3. public class InsurersService : IInsurersService
  4. {
  5. private readonly DatabaseContext _context;
  6. private readonly IMapper _mapper;
  7. public InsurersService(DatabaseContext context, IMapper mapper)
  8. {
  9. _context = context;
  10. _mapper = mapper;
  11. }
  12. public async Task<List<InsurerViewDto>> GetInsurers()
  13. {
  14. var insurers = await _context.Insurers.Include(x => x.InsuranceCompany).ToListAsync();
  15. return _mapper.Map<List<InsurerViewDto>>(insurers);
  16. }
  17. public async Task<InsurerViewDto?> GetInsurer(long id)
  18. {
  19. var insurer = await _context.Insurers
  20. .Include(x => x.InsuranceCompany)
  21. .FirstOrDefaultAsync(x => x.Id == id);
  22. if (insurer == null)
  23. throw new EntityNotFoundException("Insurer not found");
  24. return _mapper.Map<InsurerViewDto>(insurer);
  25. }
  26. public async Task CreateInsurer(InsurerCreateDto insurerCreateDto)
  27. {
  28. var insurer = _mapper.Map<Insurer>(insurerCreateDto);
  29. await _context.Insurers.AddAsync(insurer);
  30. await _context.SaveChangesAsync();
  31. }
  32. public async Task UpdateInsurer(long insurerId, InsurerUpdateDto insurerUpdateDto)
  33. {
  34. var insurer = _context.InsurancePolicies.Find(insurerId);
  35. if (insurer == null)
  36. throw new EntityNotFoundException("Insurer not found");
  37. _mapper.Map(insurerUpdateDto, insurer);
  38. insurer.UpdatedAtUtc = DateTime.UtcNow;
  39. _context.Entry(insurer).State = EntityState.Modified;
  40. await _context.SaveChangesAsync();
  41. }
  42. public async Task DeleteInsurerAsync(long insurerId)
  43. {
  44. var insurer = _context.InsurancePolicies.Find(insurerId);
  45. if (insurer == null)
  46. throw new EntityNotFoundException("Insurer not found");
  47. _context.Remove(insurer);
  48. await _context.SaveChangesAsync();
  49. }
  50. }
  51. }