11using System . Net . Mime ;
22using Dotnet . Samples . AspNetCore . WebApi . Models ;
33using Dotnet . Samples . AspNetCore . WebApi . Services ;
4+ using FluentValidation ;
45using Microsoft . AspNetCore . Mvc ;
56
67namespace Dotnet . Samples . AspNetCore . WebApi . Controllers ;
78
89[ ApiController ]
910[ Route ( "[controller]" ) ]
1011[ Produces ( "application/json" ) ]
11- public class PlayerController ( IPlayerService playerService , ILogger < PlayerController > logger )
12- : ControllerBase
12+ public class PlayerController (
13+ IPlayerService playerService ,
14+ ILogger < PlayerController > logger ,
15+ IValidator < PlayerRequestModel > validator
16+ ) : ControllerBase
1317{
1418 private readonly IPlayerService _playerService = playerService ;
1519 private readonly ILogger < PlayerController > _logger = logger ;
20+ private readonly IValidator < PlayerRequestModel > validator = validator ;
1621
1722 /* -------------------------------------------------------------------------
1823 * HTTP POST
@@ -32,23 +37,30 @@ public class PlayerController(IPlayerService playerService, ILogger<PlayerContro
3237 [ ProducesResponseType ( StatusCodes . Status409Conflict ) ]
3338 public async Task < IResult > PostAsync ( [ FromBody ] PlayerRequestModel player )
3439 {
35- if ( ! ModelState . IsValid )
40+ var validation = await validator . ValidateAsync ( player ) ;
41+
42+ if ( ! validation . IsValid )
3643 {
37- return TypedResults . BadRequest ( ) ;
44+ var errors = validation
45+ . Errors . Select ( error => new { error . PropertyName , error . ErrorMessage } )
46+ . ToArray ( ) ;
47+
48+ _logger . LogWarning ( "POST validation failed: {@Errors}" , errors ) ;
49+ return TypedResults . BadRequest ( errors ) ;
3850 }
39- else if ( await _playerService . RetrieveByIdAsync ( player . Id ) != null )
51+
52+ if ( await _playerService . RetrieveByIdAsync ( player . Id ) != null )
4053 {
4154 return TypedResults . Conflict ( ) ;
4255 }
43- else
44- {
45- var result = await _playerService . CreateAsync ( player ) ;
46- return TypedResults . CreatedAtRoute (
47- routeName : "GetById" ,
48- routeValues : new { id = result . Id } ,
49- value : result
50- ) ;
51- }
56+
57+ var result = await _playerService . CreateAsync ( player ) ;
58+
59+ return TypedResults . CreatedAtRoute (
60+ routeName : "GetById" ,
61+ routeValues : new { id = result . Id } ,
62+ value : result
63+ ) ;
5264 }
5365
5466 /* -------------------------------------------------------------------------
@@ -142,20 +154,26 @@ public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
142154 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
143155 public async Task < IResult > PutAsync ( [ FromRoute ] long id , [ FromBody ] PlayerRequestModel player )
144156 {
145- if ( ! ModelState . IsValid )
157+ var validation = await validator . ValidateAsync ( player ) ;
158+
159+ if ( ! validation . IsValid )
146160 {
147- return TypedResults . BadRequest ( ) ;
161+ var errors = validation
162+ . Errors . Select ( error => new { error . PropertyName , error . ErrorMessage } )
163+ . ToArray ( ) ;
164+
165+ _logger . LogWarning ( "PUT /players/{Id} validation failed: {@Errors}" , id , errors ) ;
166+ return TypedResults . BadRequest ( errors ) ;
148167 }
149- else if ( await _playerService . RetrieveByIdAsync ( id ) == null )
168+
169+ if ( await _playerService . RetrieveByIdAsync ( id ) == null )
150170 {
151171 return TypedResults . NotFound ( ) ;
152172 }
153- else
154- {
155- await _playerService . UpdateAsync ( player ) ;
156173
157- return TypedResults . NoContent ( ) ;
158- }
174+ await _playerService . UpdateAsync ( player ) ;
175+
176+ return TypedResults . NoContent ( ) ;
159177 }
160178
161179 /* -------------------------------------------------------------------------
0 commit comments