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.

InsurerController.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.MongoServices;
  3. using Diligent.WebAPI.Data.Entities;
  4. using Diligent.WebAPI.Host.Exceptions;
  5. using Diligent.WebAPI.Host.MongoDTOs.InsuranceCompanyMongo;
  6. using Diligent.WebAPI.Host.MongoDTOs.InsurerMongo;
  7. using Microsoft.AspNetCore.Mvc;
  8. namespace Diligent.WebAPI.Host.MongoController
  9. {
  10. [ApiVersion("1.0")]
  11. [ApiController]
  12. [Route("v{version:apiVersion}/[controller]")]
  13. public class InsurerController : ControllerBase
  14. {
  15. private readonly InsurerService _insurerService;
  16. private readonly InsuranceCompanyService _insuranceCompanyService;
  17. private readonly IMapper _mapper;
  18. public InsurerController(IMapper mapper, InsurerService insurerService, InsuranceCompanyService insuranceCompanyService)
  19. {
  20. _mapper = mapper;
  21. _insurerService = insurerService;
  22. _insuranceCompanyService = insuranceCompanyService;
  23. }
  24. [HttpGet]
  25. public async Task<IActionResult> GetAll()
  26. {
  27. var insurers = _mapper.Map<List<InsurerReturnDTO>>(await _insurerService.GetInsurersAsync());
  28. return Ok(insurers);
  29. }
  30. [HttpGet("{id:length(24)}")]
  31. public async Task<ActionResult<InsurerReturnDTO>> Get(string id)
  32. {
  33. var insurer = await _insurerService.GetByIdAsync(id);
  34. if (insurer is null)
  35. {
  36. throw new NotFoundException("Insurer not found");
  37. }
  38. var returnInsurer = _mapper.Map<InsurerReturnDTO>(insurer);
  39. if(returnInsurer.InsuranceCompanyId != null)
  40. {
  41. returnInsurer.InsuranceCompany = _mapper.Map<InsuranceCompanyReadDTO>(await _insuranceCompanyService.GetByIdAsync(returnInsurer.InsuranceCompanyId));
  42. }
  43. return returnInsurer;
  44. }
  45. [HttpPost]
  46. public async Task<ActionResult> Post(InsurerSaveDTO request)
  47. {
  48. var insurer = _mapper.Map<InsurerMongo>(request);
  49. await _insurerService.CreateInsurer(insurer);
  50. var returnInsurer = _mapper.Map<InsurerReturnDTO>(insurer);
  51. if(returnInsurer.InsuranceCompanyId != null)
  52. {
  53. var insuranceCompany = _mapper.Map<InsuranceCompanyReadDTO>(await _insuranceCompanyService.GetByIdAsync(returnInsurer.InsuranceCompanyId));
  54. returnInsurer.InsuranceCompany = insuranceCompany;
  55. }
  56. return CreatedAtAction(nameof(Get), new { id = insurer.Id }, returnInsurer);
  57. }
  58. [HttpPut("{id:length(24)}")]
  59. public async Task<ActionResult> Update(string id, InsurerMongo request)
  60. {
  61. var insurer = await _insurerService.GetByIdAsync(id);
  62. if (insurer is null)
  63. {
  64. throw new NotFoundException("Insurer not found");
  65. }
  66. request.Id = insurer.Id;
  67. await _insurerService.UpdateInsurer(id, request);
  68. return Ok();
  69. }
  70. [HttpDelete("{id:length(24)}")]
  71. public async Task<ActionResult> Delete(string id)
  72. {
  73. var insurer = await _insurerService.GetByIdAsync(id);
  74. if (insurer is null)
  75. {
  76. throw new NotFoundException("Insurer not found");
  77. }
  78. await _insurerService.DeleteInsurerAsync(id);
  79. return Ok();
  80. }
  81. }
  82. }