99using PathfinderHonorManager . Service . Interfaces ;
1010using Incoming = PathfinderHonorManager . Dto . Incoming ;
1111using Microsoft . AspNetCore . Authorization ;
12+ using Microsoft . Extensions . Logging ;
13+ using System . Linq ;
1214
1315namespace PathfinderHonorManager . Controllers
1416{
@@ -22,10 +24,12 @@ namespace PathfinderHonorManager.Controllers
2224 public class HonorsController : CustomApiController
2325 {
2426 private readonly IHonorService _honorService ;
27+ private readonly ILogger < HonorsController > _logger ;
2528
26- public HonorsController ( IHonorService honorService )
29+ public HonorsController ( IHonorService honorService , ILogger < HonorsController > logger )
2730 {
2831 _honorService = honorService ;
32+ _logger = logger ;
2933 }
3034
3135 // GET Honors
@@ -38,13 +42,16 @@ public HonorsController(IHonorService honorService)
3842 [ HttpGet ]
3943 public async Task < ActionResult < IEnumerable < Honor > > > GetHonors ( CancellationToken token )
4044 {
45+ _logger . LogInformation ( "Getting all honors" ) ;
4146 var honors = await this . _honorService . GetAllAsync ( token ) ;
4247
4348 if ( honors == default )
4449 {
50+ _logger . LogWarning ( "No honors found" ) ;
4551 return NotFound ( ) ;
4652 }
4753
54+ _logger . LogInformation ( "Retrieved {Count} honors" , honors . Count ( ) ) ;
4855 return Ok ( honors ) ;
4956 }
5057
@@ -60,13 +67,16 @@ public async Task<ActionResult<IEnumerable<Honor>>> GetHonors(CancellationToken
6067 [ HttpGet ( "{id:guid}" ) ]
6168 public async Task < IActionResult > GetByIdAsync ( Guid id , CancellationToken token )
6269 {
70+ _logger . LogInformation ( "Getting honor with ID {HonorId}" , id ) ;
6371 var honor = await this . _honorService . GetByIdAsync ( id , token ) ;
6472
6573 if ( honor == default )
6674 {
75+ _logger . LogWarning ( "Honor with ID {HonorId} not found" , id ) ;
6776 return NotFound ( ) ;
6877 }
6978
79+ _logger . LogInformation ( "Retrieved honor with ID {HonorId}" , id ) ;
7080 return Ok ( new { id = honor . HonorID , honor } ) ;
7181 }
7282
@@ -83,21 +93,25 @@ public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
8393 [ HttpPost ]
8494 public async Task < ActionResult < Honor > > Post ( [ FromBody ] Incoming . HonorDto newHonor , CancellationToken token )
8595 {
96+ _logger . LogInformation ( "Creating new honor" ) ;
8697 try
8798 {
8899 var honor = await _honorService . AddAsync ( newHonor , token ) ;
89100
101+ _logger . LogInformation ( "Created honor with ID {HonorId}" , honor . HonorID ) ;
90102 return CreatedAtRoute (
91103 routeValues : GetByIdAsync ( honor . HonorID , token ) ,
92104 honor ) ;
93105 }
94106 catch ( FluentValidation . ValidationException ex )
95107 {
108+ _logger . LogWarning ( ex , "Validation failed while creating honor" ) ;
96109 UpdateModelState ( ex ) ;
97110 return ValidationProblem ( ModelState ) ;
98111 }
99112 catch ( DbUpdateException ex )
100113 {
114+ _logger . LogError ( ex , "Database error while creating honor" ) ;
101115 return ValidationProblem ( ex . Message ) ;
102116 }
103117 }
@@ -116,20 +130,37 @@ public async Task<ActionResult<Honor>> Post([FromBody] Incoming.HonorDto newHono
116130 [ HttpPut ( "{id:guid}" ) ]
117131 public async Task < IActionResult > Put ( Guid id , [ FromBody ] Incoming . HonorDto updatedHonor , CancellationToken token )
118132 {
133+ _logger . LogInformation ( "Updating honor with ID {HonorId}" , id ) ;
119134 var honor = await _honorService . GetByIdAsync ( id , token ) ;
120135
121136 if ( honor == default )
122137 {
138+ _logger . LogWarning ( "Honor with ID {HonorId} not found" , id ) ;
123139 return NotFound ( ) ;
124140 }
125141
126- await _honorService . UpdateAsync ( id , updatedHonor , token ) ;
142+ try
143+ {
144+ await _honorService . UpdateAsync ( id , updatedHonor , token ) ;
127145
128- honor = await _honorService . GetByIdAsync ( id , token ) ;
146+ honor = await _honorService . GetByIdAsync ( id , token ) ;
147+ _logger . LogInformation ( "Updated honor with ID {HonorId}" , id ) ;
129148
130- return honor != default
131- ? Ok ( honor )
132- : NotFound ( ) ;
149+ return honor != default
150+ ? Ok ( honor )
151+ : NotFound ( ) ;
152+ }
153+ catch ( FluentValidation . ValidationException ex )
154+ {
155+ _logger . LogWarning ( ex , "Validation failed while updating honor with ID {HonorId}" , id ) ;
156+ UpdateModelState ( ex ) ;
157+ return ValidationProblem ( ModelState ) ;
158+ }
159+ catch ( DbUpdateException ex )
160+ {
161+ _logger . LogError ( ex , "Database error while updating honor with ID {HonorId}" , id ) ;
162+ return ValidationProblem ( ex . Message ) ;
163+ }
133164 }
134165 }
135166}
0 commit comments