You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InsurersService.cs 2.1KB

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