@@ -30,7 +30,7 @@ IValidator<PlayerRequestModel> validator
3030 [ HttpPost ( Name = "Create" ) ]
3131 [ Consumes ( MediaTypeNames . Application . Json ) ]
3232 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status201Created ) ]
33- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
33+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status400BadRequest ) ]
3434 [ ProducesResponseType ( StatusCodes . Status409Conflict ) ]
3535 public async Task < IResult > PostAsync ( [ FromBody ] PlayerRequestModel player )
3636 {
@@ -39,11 +39,15 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
3939 if ( ! validation . IsValid )
4040 {
4141 var errors = validation
42- . Errors . Select ( error => new { error . PropertyName , error . ErrorMessage } )
43- . ToArray ( ) ;
42+ . Errors . GroupBy ( e => e . PropertyName )
43+ . ToDictionary ( g => g . Key , g => g . Select ( e => e . ErrorMessage ) . ToArray ( ) ) ;
4444
4545 logger . LogWarning ( "POST /players validation failed: {@Errors}" , errors ) ;
46- return TypedResults . BadRequest ( errors ) ;
46+ return TypedResults . ValidationProblem (
47+ errors ,
48+ detail : "See the errors field for details." ,
49+ instance : HttpContext ? . Request ? . Path . ToString ( )
50+ ) ;
4751 }
4852
4953 if ( await playerService . RetrieveBySquadNumberAsync ( player . SquadNumber ) != null )
@@ -76,7 +80,7 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
7680 /// <response code="404">Not Found</response>
7781 [ HttpGet ( Name = "Retrieve" ) ]
7882 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
79- [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
83+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status404NotFound ) ]
8084 public async Task < IResult > GetAsync ( )
8185 {
8286 var players = await playerService . RetrieveAsync ( ) ;
@@ -89,7 +93,12 @@ public async Task<IResult> GetAsync()
8993 else
9094 {
9195 logger . LogWarning ( "GET /players not found" ) ;
92- return TypedResults . NotFound ( ) ;
96+ return TypedResults . Problem (
97+ statusCode : StatusCodes . Status404NotFound ,
98+ title : "Not Found" ,
99+ detail : "No players were found." ,
100+ instance : HttpContext ? . Request ? . Path . ToString ( )
101+ ) ;
93102 }
94103 }
95104
@@ -102,7 +111,7 @@ public async Task<IResult> GetAsync()
102111 [ Authorize ]
103112 [ HttpGet ( "{id:Guid}" , Name = "RetrieveById" ) ]
104113 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
105- [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
114+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status404NotFound ) ]
106115 public async Task < IResult > GetByIdAsync ( [ FromRoute ] Guid id )
107116 {
108117 var player = await playerService . RetrieveByIdAsync ( id ) ;
@@ -114,7 +123,12 @@ public async Task<IResult> GetByIdAsync([FromRoute] Guid id)
114123 else
115124 {
116125 logger . LogWarning ( "GET /players/{Id} not found" , id ) ;
117- return TypedResults . NotFound ( ) ;
126+ return TypedResults . Problem (
127+ statusCode : StatusCodes . Status404NotFound ,
128+ title : "Not Found" ,
129+ detail : $ "Player with Id '{ id } ' was not found.",
130+ instance : HttpContext ? . Request ? . Path . ToString ( )
131+ ) ;
118132 }
119133 }
120134
@@ -126,7 +140,7 @@ public async Task<IResult> GetByIdAsync([FromRoute] Guid id)
126140 /// <response code="404">Not Found</response>
127141 [ HttpGet ( "squadNumber/{squadNumber:int}" , Name = "RetrieveBySquadNumber" ) ]
128142 [ ProducesResponseType < PlayerResponseModel > ( StatusCodes . Status200OK ) ]
129- [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
143+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status404NotFound ) ]
130144 public async Task < IResult > GetBySquadNumberAsync ( [ FromRoute ] int squadNumber )
131145 {
132146 var player = await playerService . RetrieveBySquadNumberAsync ( squadNumber ) ;
@@ -142,7 +156,12 @@ public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
142156 else
143157 {
144158 logger . LogWarning ( "GET /players/squadNumber/{SquadNumber} not found" , squadNumber ) ;
145- return TypedResults . NotFound ( ) ;
159+ return TypedResults . Problem (
160+ statusCode : StatusCodes . Status404NotFound ,
161+ title : "Not Found" ,
162+ detail : $ "Player with Squad Number '{ squadNumber } ' was not found.",
163+ instance : HttpContext ? . Request ? . Path . ToString ( )
164+ ) ;
146165 }
147166 }
148167
@@ -162,8 +181,8 @@ public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
162181 [ HttpPut ( "squadNumber/{squadNumber:int}" , Name = "Update" ) ]
163182 [ Consumes ( MediaTypeNames . Application . Json ) ]
164183 [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
165- [ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
166- [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
184+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status400BadRequest ) ]
185+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status404NotFound ) ]
167186 public async Task < IResult > PutAsync (
168187 [ FromRoute ] int squadNumber ,
169188 [ FromBody ] PlayerRequestModel player
@@ -173,20 +192,29 @@ [FromBody] PlayerRequestModel player
173192 if ( ! validation . IsValid )
174193 {
175194 var errors = validation
176- . Errors . Select ( error => new { error . PropertyName , error . ErrorMessage } )
177- . ToArray ( ) ;
195+ . Errors . GroupBy ( e => e . PropertyName )
196+ . ToDictionary ( g => g . Key , g => g . Select ( e => e . ErrorMessage ) . ToArray ( ) ) ;
178197
179198 logger . LogWarning (
180199 "PUT /players/squadNumber/{SquadNumber} validation failed: {@Errors}" ,
181200 squadNumber ,
182201 errors
183202 ) ;
184- return TypedResults . BadRequest ( errors ) ;
203+ return TypedResults . ValidationProblem (
204+ errors ,
205+ detail : "See the errors field for details." ,
206+ instance : HttpContext ? . Request ? . Path . ToString ( )
207+ ) ;
185208 }
186209 if ( await playerService . RetrieveBySquadNumberAsync ( squadNumber ) == null )
187210 {
188211 logger . LogWarning ( "PUT /players/squadNumber/{SquadNumber} not found" , squadNumber ) ;
189- return TypedResults . NotFound ( ) ;
212+ return TypedResults . Problem (
213+ statusCode : StatusCodes . Status404NotFound ,
214+ title : "Not Found" ,
215+ detail : $ "Player with Squad Number '{ squadNumber } ' was not found.",
216+ instance : HttpContext ? . Request ? . Path . ToString ( )
217+ ) ;
190218 }
191219 if ( player . SquadNumber != squadNumber )
192220 {
@@ -195,11 +223,11 @@ [FromBody] PlayerRequestModel player
195223 squadNumber ,
196224 player . SquadNumber
197225 ) ;
198- return TypedResults . BadRequest (
199- new
200- {
201- Error = "Squad number in the route does not match squad number in the request body."
202- }
226+ return TypedResults . Problem (
227+ statusCode : StatusCodes . Status400BadRequest ,
228+ title : "Bad Request" ,
229+ detail : "Squad number in the route does not match squad number in the request body." ,
230+ instance : HttpContext ? . Request ? . Path . ToString ( )
203231 ) ;
204232 }
205233 await playerService . UpdateAsync ( player ) ;
@@ -229,13 +257,18 @@ [FromBody] PlayerRequestModel player
229257 /// <response code="404">Not Found</response>
230258 [ HttpDelete ( "squadNumber/{squadNumber:int}" , Name = "Delete" ) ]
231259 [ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
232- [ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
260+ [ ProducesResponseType < ProblemDetails > ( StatusCodes . Status404NotFound ) ]
233261 public async Task < IResult > DeleteAsync ( [ FromRoute ] int squadNumber )
234262 {
235263 if ( await playerService . RetrieveBySquadNumberAsync ( squadNumber ) == null )
236264 {
237265 logger . LogWarning ( "DELETE /players/squadNumber/{SquadNumber} not found" , squadNumber ) ;
238- return TypedResults . NotFound ( ) ;
266+ return TypedResults . Problem (
267+ statusCode : StatusCodes . Status404NotFound ,
268+ title : "Not Found" ,
269+ detail : $ "Player with Squad Number '{ squadNumber } ' was not found.",
270+ instance : HttpContext ? . Request ? . Path . ToString ( )
271+ ) ;
239272 }
240273 else
241274 {
0 commit comments