11using CourseLibrary . API . Contracts . Authors ;
22using CourseLibrary . API . Filters ;
33using CourseLibrary . API . Models . Authors ;
4- using CourseLibrary . API . Models . Exceptions ;
54using CourseLibrary . API . Pagination ;
65using CourseLibrary . API . Services . V1 . Authors ;
76using Microsoft . AspNetCore . Mvc ;
@@ -31,16 +30,9 @@ public AuthorsController(IAuthorOrchestrationService authorOrchestrationService)
3130 [ ProducesResponseType ( typeof ( string ) , StatusCodes . Status500InternalServerError ) ]
3231 public async ValueTask < IActionResult > GetAuthorAsync ( [ FromRoute ] Guid authorId , [ FromServices ] IOptions < ApiBehaviorOptions > apiBehaviorOptions , CancellationToken cancellationToken )
3332 {
34- try
35- {
36- Author author = await _authorOrchestrationService . RetrieveAuthorByIdAsync ( authorId , cancellationToken ) ;
33+ Author author = await _authorOrchestrationService . RetrieveAuthorByIdAsync ( authorId , cancellationToken ) ;
3734
38- return Ok ( ( AuthorDto ) author ) ;
39- }
40- catch ( Exception exception )
41- {
42- return HandleException ( exception , apiBehaviorOptions , ControllerContext ) ;
43- }
35+ return Ok ( ( AuthorDto ) author ) ;
4436 }
4537
4638 [ HttpPost ]
@@ -52,16 +44,9 @@ public async ValueTask<IActionResult> GetAuthorAsync([FromRoute] Guid authorId,
5244 [ ProducesResponseType ( typeof ( string ) , StatusCodes . Status500InternalServerError ) ]
5345 public async Task < IActionResult > PostAuthorAsync ( [ FromBody ] AuthorForCreation authorForCreation , [ FromServices ] IOptions < ApiBehaviorOptions > apiBehaviorOptions , CancellationToken cancellationToken )
5446 {
55- try
56- {
57- Author addedAuthor = await _authorOrchestrationService . CreateAuthorAsync ( ( Author ) authorForCreation , cancellationToken ) ;
47+ Author addedAuthor = await _authorOrchestrationService . CreateAuthorAsync ( ( Author ) authorForCreation , cancellationToken ) ;
5848
59- return CreatedAtRoute ( nameof ( GetAuthorAsync ) , new { authorId = addedAuthor . Id } , ( AuthorCreatedDto ) addedAuthor ) ;
60- }
61- catch ( Exception exception )
62- {
63- return HandleException ( exception , apiBehaviorOptions , ControllerContext ) ;
64- }
49+ return CreatedAtRoute ( nameof ( GetAuthorAsync ) , new { authorId = addedAuthor . Id } , ( AuthorCreatedDto ) addedAuthor ) ;
6550 }
6651
6752 [ HttpPatch ( "{authorId}" ) ]
@@ -73,18 +58,11 @@ public async Task<IActionResult> PostAuthorAsync([FromBody] AuthorForCreation au
7358 [ ProducesResponseType ( typeof ( string ) , StatusCodes . Status500InternalServerError ) ]
7459 public async Task < IActionResult > PatchAuthorAsync ( [ FromRoute ] Guid authorId , [ FromBody ] AuthorForUpdate authorForUpdate , [ FromServices ] IOptions < ApiBehaviorOptions > apiBehaviorOptions , CancellationToken cancellationToken )
7560 {
76- try
77- {
78- Author author = ( Author ) authorForUpdate ;
79- author . Id = authorId ;
80- Author storageAuthor = await _authorOrchestrationService . ModifyAuthorAsync ( author , cancellationToken ) ;
61+ Author author = ( Author ) authorForUpdate ;
62+ author . Id = authorId ;
63+ Author storageAuthor = await _authorOrchestrationService . ModifyAuthorAsync ( author , cancellationToken ) ;
8164
82- return Ok ( ( AuthorUpdatedDto ) storageAuthor ) ;
83- }
84- catch ( Exception exception )
85- {
86- return HandleException ( exception , apiBehaviorOptions , ControllerContext ) ;
87- }
65+ return Ok ( ( AuthorUpdatedDto ) storageAuthor ) ;
8866 }
8967
9068 [ HttpDelete ( "{authorId}" ) ]
@@ -95,16 +73,9 @@ public async Task<IActionResult> PatchAuthorAsync([FromRoute] Guid authorId, [Fr
9573 [ ProducesResponseType ( typeof ( string ) , StatusCodes . Status500InternalServerError ) ]
9674 public async Task < IActionResult > DeleteAuthorAsync ( Guid authorId , [ FromServices ] IOptions < ApiBehaviorOptions > apiBehaviorOptions , CancellationToken cancellationToken )
9775 {
98- try
99- {
100- await _authorOrchestrationService . RemoveAuthorByIdAsync ( authorId , cancellationToken ) ;
76+ await _authorOrchestrationService . RemoveAuthorByIdAsync ( authorId , cancellationToken ) ;
10177
102- return NoContent ( ) ;
103- }
104- catch ( Exception exception )
105- {
106- return HandleException ( exception , apiBehaviorOptions , ControllerContext ) ;
107- }
78+ return NoContent ( ) ;
10879 }
10980
11081 [ HttpGet ( Name = nameof ( SearchAuthorsAsync ) ) ]
@@ -113,37 +84,25 @@ public async Task<IActionResult> DeleteAuthorAsync(Guid authorId, [FromServices]
11384 [ ProducesResponseType ( typeof ( string ) , StatusCodes . Status500InternalServerError ) ]
11485 public ActionResult < IEnumerable < AuthorDto > > SearchAuthorsAsync ( [ FromQuery ] AuthorResourceParameters authorResourceParameters )
11586 {
116- try
117- {
118- PagedList < Author > storagePagedAuthors = _authorOrchestrationService . SearchAuthors ( authorResourceParameters ) ;
119-
120- PaginationMetaData PaginationMetaData = new ( )
121- {
122- TotalCount = storagePagedAuthors . TotalCount ,
123- PageSize = storagePagedAuthors . PageSize ,
124- CurrentPage = storagePagedAuthors . CurrentPage ,
125- TotalPages = storagePagedAuthors . TotalPages ,
126- HasPrevious = storagePagedAuthors . HasPrevious ,
127- HasNext = storagePagedAuthors . HasNext ,
128- PreviousPageLink = storagePagedAuthors . HasPrevious ? CreateAuthorResourceUri ( authorResourceParameters , ResourceUriType . PreviousPage ) : string . Empty ,
129- NextPageLink = storagePagedAuthors . HasNext ? CreateAuthorResourceUri ( authorResourceParameters , ResourceUriType . NextPage ) : string . Empty
130- } ;
131-
132- Response . Headers . Add ( "X-Pagination" , JsonSerializer . Serialize ( PaginationMetaData ) ) ;
133-
134- List < AuthorDto > authorsDtos = new ( ) ;
135-
136- foreach ( Author author in storagePagedAuthors )
137- {
138- authorsDtos . Add ( ( AuthorDto ) author ) ;
139- }
140-
141- return Ok ( authorsDtos ) ;
142- }
143- catch ( Exception exception )
87+ PagedList < Author > storagePagedAuthors = _authorOrchestrationService . SearchAuthors ( authorResourceParameters ) ;
88+
89+ PaginationMetaData paginationMetaData = new ( )
14490 {
145- return ( ActionResult ) HandleException ( exception ) ;
146- }
91+ TotalCount = storagePagedAuthors . TotalCount ,
92+ PageSize = storagePagedAuthors . PageSize ,
93+ CurrentPage = storagePagedAuthors . CurrentPage ,
94+ TotalPages = storagePagedAuthors . TotalPages ,
95+ HasPrevious = storagePagedAuthors . HasPrevious ,
96+ HasNext = storagePagedAuthors . HasNext ,
97+ PreviousPageLink = storagePagedAuthors . HasPrevious ? CreateAuthorResourceUri ( authorResourceParameters , ResourceUriType . PreviousPage ) : string . Empty ,
98+ NextPageLink = storagePagedAuthors . HasNext ? CreateAuthorResourceUri ( authorResourceParameters , ResourceUriType . NextPage ) : string . Empty
99+ } ;
100+
101+ Response . Headers . Append ( "X-Pagination" , JsonSerializer . Serialize ( paginationMetaData ) ) ;
102+
103+ List < AuthorDto > authorsDto = storagePagedAuthors . Select ( author => ( AuthorDto ) author ) . ToList ( ) ;
104+
105+ return Ok ( authorsDto ) ;
147106 }
148107
149108 private string CreateAuthorResourceUri ( AuthorResourceParameters authorResourceParameters , ResourceUriType type )
@@ -188,33 +147,4 @@ private string CreateAuthorResourceUri(AuthorResourceParameters authorResourcePa
188147
189148 return resourceUri is null ? string . Empty : resourceUri . Replace ( "http://" , "https://" ) ;
190149 }
191-
192- private IActionResult HandleException ( Exception exception , IOptions < ApiBehaviorOptions > ? apiBehaviorOptions = null , ActionContext ? actionContext = null )
193- {
194- switch ( exception )
195- {
196- case ResourceParametersException :
197- return BadRequest ( exception . Message ) ;
198- case CancellationException :
199- return NoContent ( ) ;
200- case ValidationException when exception . InnerException is NotFoundEntityException < Author > :
201- return NotFound ( GetInnerMessage ( exception ) ) ;
202- case ValidationException :
203- if ( apiBehaviorOptions is null || actionContext is null )
204- throw new ArgumentNullException ( nameof ( apiBehaviorOptions ) ) ;
205-
206- SetModelState ( ModelState , ( ValidationException ) exception ) ;
207-
208- return apiBehaviorOptions ! . Value . InvalidModelStateResponseFactory ( actionContext ! ) ;
209- case IDependencyException when ( exception . InnerException is DbConflictException ) :
210- return Conflict ( exception . Message ) ;
211- case IDependencyException when ( exception . InnerException is LockedEntityException < Author > ) :
212- return Problem ( GetInnerMessage ( exception ) ) ;
213- case IServiceException :
214- return Problem ( StaticData . ControllerMessages . InternalServerError ) ;
215- default :
216- LoggingBroker . LogError ( exception ) ;
217- return Problem ( StaticData . ControllerMessages . InternalServerError ) ;
218- }
219- }
220150}
0 commit comments