Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions PathfinderHonorManager/Dto/Incoming/PathfinderDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class PutPathfinderDto
public int? Grade { get; set; }

public bool? IsActive { get; set; }

public Guid? ClubID { get; set; }
}

public class BulkPutPathfinderDto
Expand Down
6 changes: 3 additions & 3 deletions PathfinderHonorManager/PathfinderHonorManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Npgsql" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.4" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.4" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="9.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="9.0.4" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.4" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.23.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
</ItemGroup>
<ItemGroup>
<None Remove="Microsoft.Extensions.Diagnostics.HealthChecks" />
Expand Down
31 changes: 19 additions & 12 deletions PathfinderHonorManager/Service/PathfinderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public class PathfinderService : IPathfinderService
return query;
}

private IQueryable<Pathfinder> QueryPathfinderByIdAsync(Guid pathfinderId, string clubCode)
private IQueryable<Pathfinder> QueryPathfinderByIdAsync(Guid pathfinderId)
{
return _dbContext.Pathfinders
.Include(pc => pc.PathfinderClass)
.Include(c => c.Club)
.Where(c => c.Club.ClubCode == clubCode && c.PathfinderID == pathfinderId);
.Include(c => c.Club)
.Where(p => p.PathfinderID == pathfinderId);
}

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

Pathfinder targetPathfinder = await QueryPathfinderByIdAsync(pathfinderId, clubCode)
Pathfinder targetPathfinder = await QueryPathfinderByIdAsync(pathfinderId)
.SingleOrDefaultAsync(token);

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

if (targetPathfinder == default)
var currentClub = await _clubService.GetByCodeAsync(clubCode, token);
if (currentClub == null)
{
_logger.LogWarning("Pathfinder with ID {PathfinderId} not found for club {ClubCode}", pathfinderId, clubCode);
_logger.LogWarning("Current club with code {ClubCode} not found while updating pathfinder {PathfinderId}", clubCode, pathfinderId);
return default;
}

Expand All @@ -168,12 +169,13 @@ await _validator.ValidateAsync(
Email = targetPathfinder.Email,
Grade = updatedPathfinder.Grade,
IsActive = updatedPathfinder.IsActive,
ClubID = club.ClubID
ClubID = updatedPathfinder.ClubID ?? targetPathfinder.ClubID
};

await _validator.ValidateAsync(
mappedPathfinder,
opts => opts.ThrowOnFailures(),
opts => opts.ThrowOnFailures()
.IncludeRuleSets("update"),
token);

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

if (updatedPathfinder.ClubID.HasValue)
{
targetPathfinder.ClubID = updatedPathfinder.ClubID.Value;
}

await _dbContext.SaveChangesAsync(token);
_logger.LogInformation("Updated pathfinder with ID {PathfinderId} for club {ClubCode}", pathfinderId, clubCode);

Expand Down Expand Up @@ -217,7 +224,7 @@ await _validator.ValidateAsync(
{
try
{
var targetPathfinder = await QueryPathfinderByIdAsync(item.PathfinderId, clubCode)
var targetPathfinder = await QueryPathfinderByIdAsync(item.PathfinderId)
.SingleOrDefaultAsync(token);

if (targetPathfinder != null)
Expand Down
16 changes: 15 additions & 1 deletion PathfinderHonorManager/Validators/PathfinderValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ private void SetUpValidation()
RuleFor(p => p.FirstName).NotEmpty();
RuleFor(p => p.LastName).NotEmpty();
RuleFor(p => p.Grade).InclusiveBetween(5, 12);
RuleFor(p => p.ClubID)
.MustAsync(async (id, token) =>
id == null || id == Guid.Empty ||
await _dbContext.Clubs.AnyAsync(c => c.ClubID == id, token))
.WithMessage("Club ID must be valid if provided");
RuleSet(
"post",
() =>
Expand All @@ -40,7 +45,16 @@ private void SetUpValidation()
RuleFor(p => p.ClubID)
.Must(id => id != Guid.Empty)
.WithMessage("User must be associated with a valid club before adding a Pathfinder");

});
RuleSet(
"update",
() =>
{
RuleFor(p => p.ClubID)
.MustAsync(async (id, token) =>
id == null || id == Guid.Empty ||
await _dbContext.Clubs.AnyAsync(c => c.ClubID == id, token))
.WithMessage("New club ID must be valid if provided");
});
}
}
Expand Down
Loading