| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using AutoMapper;
- using Diligent.WebAPI.Business.MongoServices;
- using Diligent.WebAPI.Data.Entities;
- using Diligent.WebAPI.Host.Exceptions;
- using Diligent.WebAPI.Host.MongoDTOs.InsuranceCompanyMongo;
- using Diligent.WebAPI.Host.MongoDTOs.InsurerMongo;
- using Microsoft.AspNetCore.Mvc;
-
- namespace Diligent.WebAPI.Host.MongoController
- {
- [ApiVersion("1.0")]
- [ApiController]
- [Route("v{version:apiVersion}/[controller]")]
- public class InsurerController : ControllerBase
- {
- private readonly InsurerService _insurerService;
- private readonly InsuranceCompanyService _insuranceCompanyService;
- private readonly IMapper _mapper;
-
- public InsurerController(IMapper mapper, InsurerService insurerService, InsuranceCompanyService insuranceCompanyService)
- {
- _mapper = mapper;
- _insurerService = insurerService;
- _insuranceCompanyService = insuranceCompanyService;
- }
-
- [HttpGet]
- public async Task<IActionResult> GetAll()
- {
- var insurers = _mapper.Map<List<InsurerReturnDTO>>(await _insurerService.GetInsurersAsync());
-
- return Ok(insurers);
- }
-
-
- [HttpGet("{id:length(24)}")]
- public async Task<ActionResult<InsurerReturnDTO>> Get(string id)
- {
- var insurer = await _insurerService.GetByIdAsync(id);
-
- if (insurer is null)
- {
- throw new NotFoundException("Insurer not found");
- }
-
- var returnInsurer = _mapper.Map<InsurerReturnDTO>(insurer);
-
- if(returnInsurer.InsuranceCompanyId != null)
- {
- returnInsurer.InsuranceCompany = _mapper.Map<InsuranceCompanyReadDTO>(await _insuranceCompanyService.GetByIdAsync(returnInsurer.InsuranceCompanyId));
- }
-
- return returnInsurer;
- }
-
- [HttpPost]
- public async Task<ActionResult> Post(InsurerSaveDTO request)
- {
- var insurer = _mapper.Map<InsurerMongo>(request);
- await _insurerService.CreateInsurer(insurer);
-
- var returnInsurer = _mapper.Map<InsurerReturnDTO>(insurer);
- if(returnInsurer.InsuranceCompanyId != null)
- {
- var insuranceCompany = _mapper.Map<InsuranceCompanyReadDTO>(await _insuranceCompanyService.GetByIdAsync(returnInsurer.InsuranceCompanyId));
- returnInsurer.InsuranceCompany = insuranceCompany;
- }
-
- return CreatedAtAction(nameof(Get), new { id = insurer.Id }, returnInsurer);
- }
-
- [HttpPut("{id:length(24)}")]
- public async Task<ActionResult> Update(string id, InsurerMongo request)
- {
- var insurer = await _insurerService.GetByIdAsync(id);
-
- if (insurer is null)
- {
- throw new NotFoundException("Insurer not found");
- }
-
- request.Id = insurer.Id;
-
- await _insurerService.UpdateInsurer(id, request);
-
- return Ok();
- }
-
- [HttpDelete("{id:length(24)}")]
- public async Task<ActionResult> Delete(string id)
- {
- var insurer = await _insurerService.GetByIdAsync(id);
-
- if (insurer is null)
- {
- throw new NotFoundException("Insurer not found");
- }
-
- await _insurerService.DeleteInsurerAsync(id);
-
- return Ok();
- }
- }
- }
|