|
| 1 | +//----------------------------------------------------------- |
| 2 | +// Copyright (c) Coalition of Good-Hearted Engineers |
| 3 | +// Free To Use To Build Reliable Library Management Solutions |
| 4 | +//----------------------------------------------------------- |
| 5 | + |
| 6 | +using LibraryManagement.Api.Models.Foundations.Readers; |
| 7 | +using LibraryManagement.Api.Models.Foundations.Readers.Exceptions; |
| 8 | +using LibraryManagement.Api.Services.Foundations.Readers; |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using RESTFulSense.Controllers; |
| 11 | + |
| 12 | +namespace LibraryManagement.Api.Controllers |
| 13 | +{ |
| 14 | + [ApiController] |
| 15 | + [Route("api/[controller]")] |
| 16 | + public class ReadersController : RESTFulController |
| 17 | + { |
| 18 | + private readonly IReaderService readerService; |
| 19 | + |
| 20 | + public ReadersController(IReaderService readerService) |
| 21 | + { |
| 22 | + this.readerService = readerService; |
| 23 | + } |
| 24 | + |
| 25 | + [HttpPost] |
| 26 | + public async ValueTask<ActionResult<Reader>> PostReaderAsync(Reader reader) |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + Reader postedReader = |
| 31 | + await this.readerService.AddReaderAsync(reader); |
| 32 | + |
| 33 | + return Created(postedReader); |
| 34 | + } |
| 35 | + catch (ReaderValidationException readerValidationException) |
| 36 | + { |
| 37 | + return BadRequest(readerValidationException.InnerException); |
| 38 | + } |
| 39 | + catch (ReaderDependencyValidationException readerDependencyValidationException) |
| 40 | + when (readerDependencyValidationException.InnerException is AlreadyExistsReaderException) |
| 41 | + { |
| 42 | + return Conflict(readerDependencyValidationException.InnerException); |
| 43 | + } |
| 44 | + catch (ReaderDependencyValidationException readerDependencyValidationException) |
| 45 | + { |
| 46 | + return BadRequest(readerDependencyValidationException.InnerException); |
| 47 | + } |
| 48 | + catch (ReaderDependencyException readerDependencyException) |
| 49 | + { |
| 50 | + return InternalServerError(readerDependencyException.InnerException); |
| 51 | + } |
| 52 | + catch (ReaderServiceException readerServiceException) |
| 53 | + { |
| 54 | + return InternalServerError(readerServiceException.InnerException); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments