Skip to content

Commit 5a37281

Browse files
author
Brian Cummings
committed
Added moving pathfinder clubs
1 parent 5a90544 commit 5a37281

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
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/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)