| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- namespace Diligent.WebAPI.Business.Services
- {
- public class InsurancePoliciesService : IInsurancePoliciesService
- {
- private readonly DatabaseContext _context;
- private readonly IWebhookPublisherService _webhookPublisher;
- private readonly IMapper _mapper;
-
- public InsurancePoliciesService(DatabaseContext context, IWebhookPublisherService webhookPublisher, IMapper mapper)
- {
- _context = context;
- _webhookPublisher = webhookPublisher;
- _mapper = mapper;
- }
-
- public async Task<List<InsurancePolicyViewDto>> GetInsurancePolicies()
- {
- var insurancePolicies = await _context.InsurancePolicies
- .Include(i => i.Insurer)
- .ThenInclude(k => k.InsuranceCompany)
- .ToListAsync();
-
- var insurancePoliciesDto = _mapper.Map<List<InsurancePolicyViewDto>>(insurancePolicies);
- return insurancePoliciesDto;
- }
-
- public async Task<InsurancePolicyViewDto?> GetInsurancePolicy(long id)
- {
- var insurancePolicy = await _context.InsurancePolicies
- .Include(i => i.Insurer)
- .ThenInclude(k => k.InsuranceCompany)
- .FirstOrDefaultAsync(i => i.Id == id);
-
- if (insurancePolicy == null)
- throw new EntityNotFoundException("Insurance policy not found");
-
- var insurancePolicyDto = _mapper.Map<InsurancePolicyViewDto>(insurancePolicy);
- return insurancePolicyDto;
- }
-
- public async Task CreateInsurancePolicy(InsurancePolicyCreateDto insurancePolicyCreateDto)
- {
- var insurancePolicy = _mapper.Map<InsurancePolicy>(insurancePolicyCreateDto);
- var result = await _context.InsurancePolicies.AddAsync(insurancePolicy);
-
- await _webhookPublisher.PublishAsync("insurancePolicy.created", result);
- await _context.SaveChangesAsync();
- }
-
- public async Task UpdateInsurancePolicy(long insurancePolicyId, InsurancePolicyUpdateDto insurancePolicyUpdateDto)
- {
- var insurancePolicy = _context.InsurancePolicies.Find(insurancePolicyId);
- if (insurancePolicy == null)
- throw new EntityNotFoundException("Insurance policy not found");
-
- _mapper.Map(insurancePolicyUpdateDto, insurancePolicy);
- insurancePolicy.UpdatedAtUtc = DateTime.Now;
-
- _context.Entry(insurancePolicy).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- }
-
- public async Task DeleteInsurancePolicy(long insurancePolicyId)
- {
- var insurancePolicy = _context.InsurancePolicies.Find(insurancePolicyId);
- if (insurancePolicy == null)
- throw new EntityNotFoundException("Insurance policy not found");
-
- _context.Remove(insurancePolicy);
- await _context.SaveChangesAsync();
- }
- }
- }
|