Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

InsurancePoliciesService.cs 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Diagnostics.CodeAnalysis;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. [ExcludeFromCodeCoverage]
  5. public class InsurancePoliciesService : IInsurancePoliciesService
  6. {
  7. private readonly DatabaseContext _context;
  8. private readonly IWebhookPublisherService _webhookPublisher;
  9. private readonly IMapper _mapper;
  10. public InsurancePoliciesService(DatabaseContext context, IWebhookPublisherService webhookPublisher, IMapper mapper)
  11. {
  12. _context = context;
  13. _webhookPublisher = webhookPublisher;
  14. _mapper = mapper;
  15. }
  16. public async Task<List<InsurancePolicyViewDto>> GetInsurancePolicies()
  17. {
  18. var insurancePolicies = await _context.InsurancePolicies
  19. .Include(i => i.Insurer)
  20. .ThenInclude(k => k.InsuranceCompany)
  21. .ToListAsync();
  22. var insurancePoliciesDto = _mapper.Map<List<InsurancePolicyViewDto>>(insurancePolicies);
  23. return insurancePoliciesDto;
  24. }
  25. public async Task<InsurancePolicyViewDto?> GetInsurancePolicy(long id)
  26. {
  27. var insurancePolicy = await _context.InsurancePolicies
  28. .Include(i => i.Insurer)
  29. .ThenInclude(k => k.InsuranceCompany)
  30. .FirstOrDefaultAsync(i => i.Id == id);
  31. if (insurancePolicy == null)
  32. throw new EntityNotFoundException("Insurance policy not found");
  33. var insurancePolicyDto = _mapper.Map<InsurancePolicyViewDto>(insurancePolicy);
  34. return insurancePolicyDto;
  35. }
  36. public async Task CreateInsurancePolicy(InsurancePolicyCreateDto insurancePolicyCreateDto)
  37. {
  38. var insurancePolicy = _mapper.Map<InsurancePolicy>(insurancePolicyCreateDto);
  39. var result = await _context.InsurancePolicies.AddAsync(insurancePolicy);
  40. await _webhookPublisher.PublishAsync("insurancePolicy.created", result);
  41. await _context.SaveChangesAsync();
  42. }
  43. public async Task UpdateInsurancePolicy(long insurancePolicyId, InsurancePolicyUpdateDto insurancePolicyUpdateDto)
  44. {
  45. var insurancePolicy = _context.InsurancePolicies.Find(insurancePolicyId);
  46. if (insurancePolicy == null)
  47. throw new EntityNotFoundException("Insurance policy not found");
  48. _mapper.Map(insurancePolicyUpdateDto, insurancePolicy);
  49. insurancePolicy.UpdatedAtUtc = DateTime.Now;
  50. _context.Entry(insurancePolicy).State = EntityState.Modified;
  51. await _context.SaveChangesAsync();
  52. }
  53. public async Task DeleteInsurancePolicy(long insurancePolicyId)
  54. {
  55. var insurancePolicy = _context.InsurancePolicies.Find(insurancePolicyId);
  56. if (insurancePolicy == null)
  57. throw new EntityNotFoundException("Insurance policy not found");
  58. _context.Remove(insurancePolicy);
  59. await _context.SaveChangesAsync();
  60. }
  61. }
  62. }