1111using Microsoft . AspNetCore . Authorization ;
1212using System . Linq ;
1313using Microsoft . AspNetCore . Routing ;
14+ using Microsoft . Extensions . Logging ;
1415
1516namespace PathfinderHonorManager . Controllers
1617{
@@ -24,16 +25,18 @@ namespace PathfinderHonorManager.Controllers
2425 public class PathfindersController : CustomApiController
2526 {
2627 private readonly IPathfinderService _pathfinderService ;
28+ private readonly ILogger < PathfindersController > _logger ;
2729
2830 private string GetClubCodeFromContext ( )
2931 {
3032 var clubCode = HttpContext . User . FindFirst ( "clubCode" ) ? . Value ;
3133 return clubCode ;
3234 }
3335
34- public PathfindersController ( IPathfinderService pathfinderService )
36+ public PathfindersController ( IPathfinderService pathfinderService , ILogger < PathfindersController > logger )
3537 {
3638 _pathfinderService = pathfinderService ;
39+ _logger = logger ;
3740 }
3841
3942 // GET Pathfinders
@@ -48,13 +51,17 @@ public PathfindersController(IPathfinderService pathfinderService)
4851 public async Task < ActionResult < IEnumerable < Outgoing . PathfinderDependantDto > > > GetAll ( CancellationToken token , bool showInactive = false )
4952 {
5053 var clubCode = GetClubCodeFromContext ( ) ;
54+ _logger . LogInformation ( "Getting all pathfinders for club {ClubCode}, showInactive: {ShowInactive}" , clubCode , showInactive ) ;
55+
5156 var pathfinders = await _pathfinderService . GetAllAsync ( clubCode , showInactive , token ) ;
5257
5358 if ( pathfinders == null || ! pathfinders . Any ( ) )
5459 {
60+ _logger . LogWarning ( "No pathfinders found for club {ClubCode}" , clubCode ) ;
5561 return NotFound ( ) ;
5662 }
5763
64+ _logger . LogInformation ( "Retrieved {Count} pathfinders for club {ClubCode}" , pathfinders . Count ( ) , clubCode ) ;
5865 return Ok ( pathfinders ) ;
5966 }
6067
@@ -71,13 +78,17 @@ public PathfindersController(IPathfinderService pathfinderService)
7178 public async Task < IActionResult > GetByIdAsync ( Guid id , CancellationToken token )
7279 {
7380 var clubCode = GetClubCodeFromContext ( ) ;
81+ _logger . LogInformation ( "Getting pathfinder with ID {PathfinderId} for club {ClubCode}" , id , clubCode ) ;
82+
7483 var pathfinder = await _pathfinderService . GetByIdAsync ( id , clubCode , token ) ;
7584
7685 if ( pathfinder == default )
7786 {
87+ _logger . LogWarning ( "Pathfinder with ID {PathfinderId} not found for club {ClubCode}" , id , clubCode ) ;
7888 return NotFound ( ) ;
7989 }
8090
91+ _logger . LogInformation ( "Retrieved pathfinder with ID {PathfinderId} for club {ClubCode}" , id , clubCode ) ;
8192 return Ok ( pathfinder ) ;
8293 }
8394
@@ -95,24 +106,28 @@ public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
95106 public async Task < IActionResult > PostAsync ( [ FromBody ] Incoming . PathfinderDto newPathfinder , CancellationToken token )
96107 {
97108 var clubCode = GetClubCodeFromContext ( ) ;
109+ _logger . LogInformation ( "Creating new pathfinder for club {ClubCode}" , clubCode ) ;
110+
98111 try
99112 {
100113 var pathfinder = await _pathfinderService . AddAsync ( newPathfinder , clubCode , token ) ;
101114
115+ _logger . LogInformation ( "Created pathfinder with ID {PathfinderId} for club {ClubCode}" , pathfinder . PathfinderID , clubCode ) ;
102116 return CreatedAtRoute (
103117 routeValues : GetByIdAsync ( pathfinder . PathfinderID , token ) ,
104118 pathfinder ) ;
105119 }
106120 catch ( FluentValidation . ValidationException ex )
107121 {
122+ _logger . LogWarning ( ex , "Validation failed while creating pathfinder for club {ClubCode}" , clubCode ) ;
108123 UpdateModelState ( ex ) ;
109124 return ValidationProblem ( ModelState ) ;
110125 }
111126 catch ( DbUpdateException ex )
112127 {
128+ _logger . LogError ( ex , "Database error while creating pathfinder for club {ClubCode}" , clubCode ) ;
113129 return ValidationProblem ( ex . Message ) ;
114130 }
115-
116131 }
117132
118133 // PUT Pathfinders/{pathfinderId}
@@ -130,21 +145,30 @@ public async Task<IActionResult> PostAsync([FromBody] Incoming.PathfinderDto new
130145 public async Task < IActionResult > PutAsync ( Guid pathfinderId , [ FromBody ] Incoming . PutPathfinderDto updatedPathfinder , CancellationToken token )
131146 {
132147 var clubCode = GetClubCodeFromContext ( ) ;
148+ _logger . LogInformation ( "Updating pathfinder with ID {PathfinderId} for club {ClubCode}" , pathfinderId , clubCode ) ;
149+
133150 try
134151 {
135152 var pathfinder = await _pathfinderService . UpdateAsync ( pathfinderId , updatedPathfinder , clubCode , token ) ;
136153
137- return pathfinder != default
138- ? Ok ( pathfinder )
139- : NotFound ( ) ;
154+ if ( pathfinder == default )
155+ {
156+ _logger . LogWarning ( "Pathfinder with ID {PathfinderId} not found for club {ClubCode}" , pathfinderId , clubCode ) ;
157+ return NotFound ( ) ;
158+ }
159+
160+ _logger . LogInformation ( "Updated pathfinder with ID {PathfinderId} for club {ClubCode}" , pathfinderId , clubCode ) ;
161+ return Ok ( pathfinder ) ;
140162 }
141163 catch ( FluentValidation . ValidationException ex )
142164 {
165+ _logger . LogWarning ( ex , "Validation failed while updating pathfinder with ID {PathfinderId} for club {ClubCode}" , pathfinderId , clubCode ) ;
143166 UpdateModelState ( ex ) ;
144167 return ValidationProblem ( ModelState ) ;
145168 }
146169 catch ( DbUpdateException ex )
147170 {
171+ _logger . LogError ( ex , "Database error while updating pathfinder with ID {PathfinderId} for club {ClubCode}" , pathfinderId , clubCode ) ;
148172 return ValidationProblem ( ex . Message ) ;
149173 }
150174 }
@@ -163,6 +187,8 @@ public async Task<IActionResult> PutAsync(Guid pathfinderId, [FromBody] Incoming
163187 public async Task < IActionResult > BulkPutPathfindersAsync ( [ FromBody ] IEnumerable < Incoming . BulkPutPathfinderDto > bulkData , CancellationToken token )
164188 {
165189 var clubCode = GetClubCodeFromContext ( ) ;
190+ _logger . LogInformation ( "Bulk updating {Count} pathfinders for club {ClubCode}" , bulkData . Count ( ) , clubCode ) ;
191+
166192 var responses = new List < object > ( ) ;
167193
168194 foreach ( var data in bulkData )
@@ -178,9 +204,19 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
178204 status = pathfinder != null ? StatusCodes . Status200OK : StatusCodes . Status404NotFound ,
179205 pathfinderId = item . PathfinderId ,
180206 } ) ;
207+
208+ if ( pathfinder == null )
209+ {
210+ _logger . LogWarning ( "Pathfinder with ID {PathfinderId} not found during bulk update for club {ClubCode}" , item . PathfinderId , clubCode ) ;
211+ }
212+ else
213+ {
214+ _logger . LogInformation ( "Updated pathfinder with ID {PathfinderId} during bulk update for club {ClubCode}" , item . PathfinderId , clubCode ) ;
215+ }
181216 }
182217 catch ( FluentValidation . ValidationException ex )
183218 {
219+ _logger . LogWarning ( ex , "Validation failed while bulk updating pathfinder with ID {PathfinderId} for club {ClubCode}" , item . PathfinderId , clubCode ) ;
184220 responses . Add ( new
185221 {
186222 status = StatusCodes . Status400BadRequest ,
@@ -190,6 +226,7 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
190226 }
191227 catch ( DbUpdateException ex )
192228 {
229+ _logger . LogError ( ex , "Database error while bulk updating pathfinder with ID {PathfinderId} for club {ClubCode}" , item . PathfinderId , clubCode ) ;
193230 responses . Add ( new
194231 {
195232 status = StatusCodes . Status400BadRequest ,
@@ -200,6 +237,7 @@ public async Task<IActionResult> BulkPutPathfindersAsync([FromBody] IEnumerable<
200237 }
201238 }
202239
240+ _logger . LogInformation ( "Completed bulk update of {Count} pathfinders for club {ClubCode}" , bulkData . Count ( ) , clubCode ) ;
203241 return StatusCode ( StatusCodes . Status207MultiStatus , responses ) ;
204242 }
205243 }
0 commit comments