Skip to content

Commit 73c5bc1

Browse files
Merge pull request #661 from PathfinderHonorManager/develop
Added moving pathfinder clubs
2 parents b7b19c7 + 410ee66 commit 73c5bc1

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

PathfinderHonorManager/Dto/Incoming/PathfinderDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class PutPathfinderDto
3131
public int? Grade { get; set; }
3232

3333
public bool? IsActive { get; set; }
34+
35+
public Guid? ClubID { get; set; }
3436
}
3537

3638
public class BulkPutPathfinderDto

PathfinderHonorManager/PathfinderHonorManager.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.4" />
1616
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
1717
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
18-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />
18+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
1919
<PackageReference Include="Npgsql" Version="9.0.3" />
2020
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.4" />
2121
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
2222
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
2323
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
2424
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.4" />
2525
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="9.0.0" />
26-
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.2" />
26+
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.4" />
2727
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="9.0.4" />
2828
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.4" />
2929
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.4" />
3030
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.4" />
3131
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.23.0" />
32-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
32+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
3333
</ItemGroup>
3434
<ItemGroup>
3535
<None Remove="Microsoft.Extensions.Diagnostics.HealthChecks" />

PathfinderHonorManager/Service/PathfinderService.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public class PathfinderService : IPathfinderService
4242
return query;
4343
}
4444

45-
private IQueryable<Pathfinder> QueryPathfinderByIdAsync(Guid pathfinderId, string clubCode)
45+
private IQueryable<Pathfinder> QueryPathfinderByIdAsync(Guid pathfinderId)
4646
{
4747
return _dbContext.Pathfinders
4848
.Include(pc => pc.PathfinderClass)
49-
.Include(c => c.Club)
50-
.Where(c => c.Club.ClubCode == clubCode && c.PathfinderID == pathfinderId);
49+
.Include(c => c.Club)
50+
.Where(p => p.PathfinderID == pathfinderId);
5151
}
5252

5353
public PathfinderService(
@@ -144,18 +144,19 @@ await _validator.ValidateAsync(
144144
{
145145
_logger.LogInformation("Updating pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
146146

147-
Pathfinder targetPathfinder = await QueryPathfinderByIdAsync(pathfinderId, clubCode)
147+
Pathfinder targetPathfinder = await QueryPathfinderByIdAsync(pathfinderId)
148148
.SingleOrDefaultAsync(token);
149149

150-
var club = await _clubService.GetByCodeAsync(clubCode, token);
151-
if (club == null)
150+
if (targetPathfinder == default)
152151
{
153-
_logger.LogWarning("Club with code {ClubCode} not found while updating pathfinder {PathfinderId}", clubCode, pathfinderId);
152+
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found", pathfinderId);
153+
return default;
154154
}
155155

156-
if (targetPathfinder == default)
156+
var currentClub = await _clubService.GetByCodeAsync(clubCode, token);
157+
if (currentClub == null)
157158
{
158-
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found for club {ClubCode}", pathfinderId, clubCode);
159+
_logger.LogWarning("Current club with code {ClubCode} not found while updating pathfinder {PathfinderId}", clubCode, pathfinderId);
159160
return default;
160161
}
161162

@@ -168,12 +169,13 @@ await _validator.ValidateAsync(
168169
Email = targetPathfinder.Email,
169170
Grade = updatedPathfinder.Grade,
170171
IsActive = updatedPathfinder.IsActive,
171-
ClubID = club.ClubID
172+
ClubID = updatedPathfinder.ClubID ?? targetPathfinder.ClubID
172173
};
173174

174175
await _validator.ValidateAsync(
175176
mappedPathfinder,
176-
opts => opts.ThrowOnFailures(),
177+
opts => opts.ThrowOnFailures()
178+
.IncludeRuleSets("update"),
177179
token);
178180

179181
if (mappedPathfinder.Grade != null)
@@ -190,6 +192,11 @@ await _validator.ValidateAsync(
190192
targetPathfinder.IsActive = mappedPathfinder.IsActive;
191193
}
192194

195+
if (updatedPathfinder.ClubID.HasValue)
196+
{
197+
targetPathfinder.ClubID = updatedPathfinder.ClubID.Value;
198+
}
199+
193200
await _dbContext.SaveChangesAsync(token);
194201
_logger.LogInformation("Updated pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);
195202

@@ -217,7 +224,7 @@ await _validator.ValidateAsync(
217224
{
218225
try
219226
{
220-
var targetPathfinder = await QueryPathfinderByIdAsync(item.PathfinderId, clubCode)
227+
var targetPathfinder = await QueryPathfinderByIdAsync(item.PathfinderId)
221228
.SingleOrDefaultAsync(token);
222229

223230
if (targetPathfinder != null)

PathfinderHonorManager/Validators/PathfinderValidator.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ private void SetUpValidation()
2424
RuleFor(p => p.FirstName).NotEmpty();
2525
RuleFor(p => p.LastName).NotEmpty();
2626
RuleFor(p => p.Grade).InclusiveBetween(5, 12);
27+
RuleFor(p => p.ClubID)
28+
.MustAsync(async (id, token) =>
29+
id == null || id == Guid.Empty ||
30+
await _dbContext.Clubs.AnyAsync(c => c.ClubID == id, token))
31+
.WithMessage("Club ID must be valid if provided");
2732
RuleSet(
2833
"post",
2934
() =>
@@ -40,7 +45,16 @@ private void SetUpValidation()
4045
RuleFor(p => p.ClubID)
4146
.Must(id => id != Guid.Empty)
4247
.WithMessage("User must be associated with a valid club before adding a Pathfinder");
43-
48+
});
49+
RuleSet(
50+
"update",
51+
() =>
52+
{
53+
RuleFor(p => p.ClubID)
54+
.MustAsync(async (id, token) =>
55+
id == null || id == Guid.Empty ||
56+
await _dbContext.Clubs.AnyAsync(c => c.ClubID == id, token))
57+
.WithMessage("New club ID must be valid if provided");
4458
});
4559
}
4660
}

0 commit comments

Comments
 (0)