22using Dotnet . Samples . AspNetCore . WebApi . Models ;
33using Dotnet . Samples . AspNetCore . WebApi . Services ;
44using FluentValidation ;
5+ using Microsoft . AspNetCore . Authorization ;
56using Microsoft . AspNetCore . Mvc ;
67
78namespace Dotnet . Samples . AspNetCore . WebApi . Controllers ;
@@ -26,7 +27,7 @@ IValidator<PlayerRequestModel> validator
2627 /// <response code="201">Created</response>
2728 /// <response code="400">Bad Request</response>
2829 /// <response code="409">Conflict</response>
29- [ HttpPost ]
30+ [ HttpPost ( Name = "Create" ) ]
3031 [ Consumes ( MediaTypeNames . Application . Json ) ]
3132 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status201Created ) ]
3233 [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
@@ -45,11 +46,11 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
4546 return TypedResults . BadRequest ( errors ) ;
4647 }
4748
48- if ( await playerService . RetrieveByIdAsync ( player . Id ) != null )
49+ if ( await playerService . RetrieveBySquadNumberAsync ( player . SquadNumber ) != null )
4950 {
5051 logger . LogWarning (
51- "POST /players failed: Player with ID {Id } already exists" ,
52- player . Id
52+ "POST /players failed: Player with Squad Number {SquadNumber } already exists" ,
53+ player . SquadNumber
5354 ) ;
5455 return TypedResults . Conflict ( ) ;
5556 }
@@ -58,8 +59,8 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
5859
5960 logger . LogInformation ( "POST /players created: {@Player}" , result ) ;
6061 return TypedResults . CreatedAtRoute (
61- routeName : "GetById " ,
62- routeValues : new { id = result . Id } ,
62+ routeName : "RetrieveBySquadNumber " ,
63+ routeValues : new { squadNumber = result . Dorsal } ,
6364 value : result
6465 ) ;
6566 }
@@ -73,7 +74,7 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
7374 /// </summary>
7475 /// <response code="200">OK</response>
7576 /// <response code="404">Not Found</response>
76- [ HttpGet ]
77+ [ HttpGet ( Name = "Retrieve" ) ]
7778 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
7879 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
7980 public async Task < IResult > GetAsync ( )
@@ -98,10 +99,12 @@ public async Task<IResult> GetAsync()
9899 /// <param name="id">The ID of the Player</param>
99100 /// <response code="200">OK</response>
100101 /// <response code="404">Not Found</response>
101- [ HttpGet ( "{id:long}" , Name = "GetById" ) ]
102+ [ Authorize ( Roles = "Admin" ) ]
103+ [ ApiExplorerSettings ( IgnoreApi = true ) ]
104+ [ HttpGet ( "{id:Guid}" , Name = "RetrieveById" ) ]
102105 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
103106 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
104- public async Task < IResult > GetByIdAsync ( [ FromRoute ] long id )
107+ public async Task < IResult > GetByIdAsync ( [ FromRoute ] Guid id )
105108 {
106109 var player = await playerService . RetrieveByIdAsync ( id ) ;
107110 if ( player != null )
@@ -122,7 +125,7 @@ public async Task<IResult> GetByIdAsync([FromRoute] long id)
122125 /// <param name="squadNumber">The Squad Number of the Player</param>
123126 /// <response code="200">OK</response>
124127 /// <response code="404">Not Found</response>
125- [ HttpGet ( "squad /{squadNumber:int}" ) ]
128+ [ HttpGet ( "squadNumber /{squadNumber:int}" , Name = "RetrieveBySquadNumber ") ]
126129 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
127130 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
128131 public async Task < IResult > GetBySquadNumberAsync ( [ FromRoute ] int squadNumber )
@@ -149,19 +152,23 @@ public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
149152 * ---------------------------------------------------------------------- */
150153
151154 /// <summary>
152- /// Updates (entirely) a Player by its ID
155+ /// Updates (entirely) a Player by its Squad Number
153156 /// </summary>
154- /// <param name="id">The ID of the Player</param>
157+ ///
155158 /// <param name="player">The PlayerRequestModel</param>
159+ /// <param name="squadNumber">The Squad Number of the Player</param>
156160 /// <response code="204">No Content</response>
157161 /// <response code="400">Bad Request</response>
158162 /// <response code="404">Not Found</response>
159- [ HttpPut ( "{id} " ) ]
163+ [ HttpPut ( "{squadNumber:int}" , Name = "Update ") ]
160164 [ Consumes ( MediaTypeNames . Application . Json ) ]
161165 [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
162166 [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
163167 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
164- public async Task < IResult > PutAsync ( [ FromRoute ] long id , [ FromBody ] PlayerRequestModel player )
168+ public async Task < IResult > PutAsync (
169+ [ FromRoute ] int squadNumber ,
170+ [ FromBody ] PlayerRequestModel player
171+ )
165172 {
166173 var validation = await validator . ValidateAsync ( player ) ;
167174 if ( ! validation . IsValid )
@@ -170,16 +177,20 @@ public async Task<IResult> PutAsync([FromRoute] long id, [FromBody] PlayerReques
170177 . Errors . Select ( error => new { error . PropertyName , error . ErrorMessage } )
171178 . ToArray ( ) ;
172179
173- logger . LogWarning ( "PUT /players/{Id} validation failed: {@Errors}" , id , errors ) ;
180+ logger . LogWarning (
181+ "PUT /players/{squadNumber} validation failed: {@Errors}" ,
182+ squadNumber ,
183+ errors
184+ ) ;
174185 return TypedResults . BadRequest ( errors ) ;
175186 }
176- if ( await playerService . RetrieveByIdAsync ( id ) == null )
187+ if ( await playerService . RetrieveBySquadNumberAsync ( squadNumber ) == null )
177188 {
178- logger . LogWarning ( "PUT /players/{Id } not found" , id ) ;
189+ logger . LogWarning ( "PUT /players/{SquadNumber } not found" , squadNumber ) ;
179190 return TypedResults . NotFound ( ) ;
180191 }
181192 await playerService . UpdateAsync ( player ) ;
182- logger . LogInformation ( "PUT /players/{Id } updated: {@Player}" , id , player ) ;
193+ logger . LogInformation ( "PUT /players/{SquadNumber } updated: {@Player}" , squadNumber , player ) ;
183194 return TypedResults . NoContent ( ) ;
184195 }
185196
@@ -188,25 +199,25 @@ public async Task<IResult> PutAsync([FromRoute] long id, [FromBody] PlayerReques
188199 * ---------------------------------------------------------------------- */
189200
190201 /// <summary>
191- /// Deletes a Player by its ID
202+ /// Deletes a Player by its Squad Number
192203 /// </summary>
193- /// <param name="id ">The ID of the Player</param>
204+ /// <param name="squadNumber ">The Squad Number of the Player</param>
194205 /// <response code="204">No Content</response>
195206 /// <response code="404">Not Found</response>
196- [ HttpDelete ( "{id:long} " ) ]
207+ [ HttpDelete ( "{squadNumber:int}" , Name = "Delete ") ]
197208 [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
198209 [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
199- public async Task < IResult > DeleteAsync ( [ FromRoute ] long id )
210+ public async Task < IResult > DeleteAsync ( [ FromRoute ] int squadNumber )
200211 {
201- if ( await playerService . RetrieveByIdAsync ( id ) == null )
212+ if ( await playerService . RetrieveBySquadNumberAsync ( squadNumber ) == null )
202213 {
203- logger . LogWarning ( "DELETE /players/{Id } not found" , id ) ;
214+ logger . LogWarning ( "DELETE /players/{SquadNumber } not found" , squadNumber ) ;
204215 return TypedResults . NotFound ( ) ;
205216 }
206217 else
207218 {
208- await playerService . DeleteAsync ( id ) ;
209- logger . LogInformation ( "DELETE /players/{Id } deleted" , id ) ;
219+ await playerService . DeleteAsync ( squadNumber ) ;
220+ logger . LogInformation ( "DELETE /players/{SquadNumber } deleted" , squadNumber ) ;
210221 return TypedResults . NoContent ( ) ;
211222 }
212223 }
0 commit comments