Skip to content

Commit f882588

Browse files
Merge pull request #658 from PathfinderHonorManager/develop
Even better validation
2 parents 638a4b8 + 0664e46 commit f882588

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

PathfinderHonorManager/Service/ClubService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212
using System.Collections.Generic;
1313
using System.Linq;
1414
using FluentValidation;
15+
using PathfinderHonorManager.Validators;
1516

1617
namespace PathfinderHonorManager.Service
1718
{
1819
public class ClubService : IClubService
1920
{
2021
private readonly PathfinderContext _dbContext;
21-
2222
private readonly IMapper _mapper;
23-
2423
private readonly ILogger _logger;
25-
26-
private readonly IValidator<Incoming.ClubDto> _validator;
24+
private readonly ClubValidator _validator;
2725

2826
public ClubService(
2927
PathfinderContext context,
@@ -34,7 +32,7 @@ public ClubService(
3432
_dbContext = context;
3533
_mapper = mapper;
3634
_logger = logger;
37-
_validator = validator;
35+
_validator = (ClubValidator)validator;
3836
}
3937

4038
public async Task<ICollection<Outgoing.ClubDto>> GetAllAsync(CancellationToken token)
@@ -92,6 +90,7 @@ public ClubService(
9290

9391
try
9492
{
93+
_validator.SetExcludeClubId(null);
9594
await _validator.ValidateAsync(club, options => options.ThrowOnFailures().IncludeRuleSets("post"), token);
9695

9796
var entity = _mapper.Map<Club>(club);
@@ -125,6 +124,7 @@ public ClubService(
125124

126125
if (entity.ClubCode != club.ClubCode)
127126
{
127+
_validator.SetExcludeClubId(id);
128128
await _validator.ValidateAsync(club, options => options.ThrowOnFailures().IncludeRuleSets("post"), token);
129129
}
130130

PathfinderHonorManager/Validators/ClubValidator.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
using PathfinderHonorManager.DataAccess;
44
using PathfinderHonorManager.Dto.Incoming;
55
using System;
6+
using System.Linq;
67
using System.Text.RegularExpressions;
78

89
namespace PathfinderHonorManager.Validators
910
{
1011
public class ClubValidator : AbstractValidator<ClubDto>
1112
{
1213
private readonly PathfinderContext _dbContext;
14+
private Guid? _excludeClubId;
1315

1416
public ClubValidator(PathfinderContext dbContext)
1517
{
1618
_dbContext = dbContext;
1719
SetUpValidation();
1820
}
1921

22+
public void SetExcludeClubId(Guid? clubId)
23+
{
24+
_excludeClubId = clubId;
25+
}
26+
2027
private void SetUpValidation()
2128
{
2229
RuleFor(c => c.Name)
@@ -36,7 +43,14 @@ private void SetUpValidation()
3643
{
3744
RuleFor(c => c.ClubCode)
3845
.MustAsync(async (code, token) =>
39-
!await _dbContext.Clubs.AnyAsync(c => c.ClubCode == code, token))
46+
{
47+
var query = _dbContext.Clubs.Where(c => c.ClubCode == code);
48+
if (_excludeClubId.HasValue)
49+
{
50+
query = query.Where(c => c.ClubID != _excludeClubId.Value);
51+
}
52+
return !await query.AnyAsync(token);
53+
})
4054
.WithMessage(c => $"Club code {c.ClubCode} is already in use.");
4155
});
4256
}

0 commit comments

Comments
 (0)