Skip to content

Commit 8f40bc7

Browse files
Merge pull request #633 from PathfinderHonorManager/develop
Added logging
2 parents 6b25a0c + fe291bc commit 8f40bc7

File tree

16 files changed

+477
-181
lines changed

16 files changed

+477
-181
lines changed

.github/workflows/dependabot_automerge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
steps:
1313
- name: Dependabot metadata
1414
id: metadata
15-
uses: dependabot/fetch-metadata@v2.2.0
15+
uses: dependabot/fetch-metadata@v2.3.0
1616
with:
1717
github-token: "${{ secrets.GITHUB_TOKEN }}"
1818
- name: Enable auto-merge for Dependabot PRs

PathfinderHonorManager.Tests/PathfinderHonorManager.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="coverlet.msbuild" Version="6.0.3">
11+
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
<PrivateAssets>all</PrivateAssets>
1414
</PackageReference>
1515
<PackageReference Include="FluentValidation" Version="11.11.0" />
1616
<PackageReference Include="Moq" Version="4.20.72" />
1717
<PackageReference Include="NUnit" Version="4.3.2" />
18-
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
19-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
20-
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0" />
21-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
18+
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
20+
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.3" />
21+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
2222
<PackageReference Include="FluentValidation.Validators.UnitTestExtension" Version="1.11.0.2" />
2323
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
24-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.3" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

PathfinderHonorManager/Controllers/ClubController.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.AspNetCore.Mvc;
88
using PathfinderHonorManager.Model;
99
using PathfinderHonorManager.Service.Interfaces;
10+
using Microsoft.Extensions.Logging;
11+
using System.Linq;
1012

1113
namespace PathfinderHonorManager.Controllers
1214
{
@@ -20,10 +22,12 @@ namespace PathfinderHonorManager.Controllers
2022
public class ClubsController : ControllerBase
2123
{
2224
private readonly IClubService _clubService;
25+
private readonly ILogger<ClubsController> _logger;
2326

24-
public ClubsController(IClubService clubService)
27+
public ClubsController(IClubService clubService, ILogger<ClubsController> logger)
2528
{
2629
_clubService = clubService;
30+
_logger = logger;
2731
}
2832

2933
// GET Clubs
@@ -39,24 +43,30 @@ public async Task<ActionResult<IEnumerable<Club>>> GetClubs(CancellationToken to
3943
{
4044
if (clubcode == null)
4145
{
46+
_logger.LogInformation("Getting all clubs");
4247
var clubs = await _clubService.GetAllAsync(token);
4348

4449
if (clubs == default)
4550
{
51+
_logger.LogWarning("No clubs found");
4652
return NotFound();
4753
}
4854

55+
_logger.LogInformation("Retrieved {Count} clubs", clubs.Count());
4956
return Ok(clubs);
5057
}
5158
else
5259
{
60+
_logger.LogInformation("Getting club with code {ClubCode}", clubcode);
5361
var club = await _clubService.GetByCodeAsync(clubcode.ToUpper(), token);
5462

5563
if (club == default)
5664
{
65+
_logger.LogWarning("Club with code {ClubCode} not found", clubcode);
5766
return NotFound();
5867
}
5968

69+
_logger.LogInformation("Retrieved club with code {ClubCode}", clubcode);
6070
return Ok(club);
6171
}
6272
}
@@ -73,13 +83,16 @@ public async Task<ActionResult<IEnumerable<Club>>> GetClubs(CancellationToken to
7383
[HttpGet("{id:guid}")]
7484
public async Task<IActionResult> GetByIdAsync(Guid id, CancellationToken token)
7585
{
86+
_logger.LogInformation("Getting club with ID {ClubId}", id);
7687
var club = await _clubService.GetByIdAsync(id, token);
7788

7889
if (club == default)
7990
{
91+
_logger.LogWarning("Club with ID {ClubId} not found", id);
8092
return NotFound();
8193
}
8294

95+
_logger.LogInformation("Retrieved club with ID {ClubId}", id);
8396
return Ok(club);
8497
}
8598
}

PathfinderHonorManager/Controllers/HonorsController.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using PathfinderHonorManager.Service.Interfaces;
1010
using Incoming = PathfinderHonorManager.Dto.Incoming;
1111
using Microsoft.AspNetCore.Authorization;
12+
using Microsoft.Extensions.Logging;
13+
using System.Linq;
1214

1315
namespace 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

Comments
 (0)