Skip to content

Commit 19c2197

Browse files
Merge pull request #14 from OneBus/dev-eduardo
♻️refactor: Updating Line Validators
2 parents 2e5c818 + ae5d545 commit 19c2197

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

OneBus.Application/Validators/Line/CreateLineDTOValidator.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ public class CreateLineDTOValidator : AbstractValidator<CreateLineDTO>
1111
{
1212
private readonly ILineRepository _lineRepository;
1313

14-
//TODO: Não deixar a mesma linha pode pode ter sentido de ida e volta, mas não de circular e
15-
//uma linha circular não pode ter outro cadastro com sentido de ida ou volta
14+
public const string InvalidDirectionType = "Tipo de Direção inválida, verifique se a linha é circular ou a direção é repetida.";
15+
1616
public CreateLineDTOValidator(ILineRepository lineRepository)
1717
{
1818
_lineRepository = lineRepository;
1919

2020
RuleFor(c => c.Type)
2121
.Must(ValidationUtils.IsValidEnumValue<LineType>)
2222
.OverridePropertyName("Tipo");
23-
23+
2424
RuleFor(c => c.DirectionType)
2525
.Must(ValidationUtils.IsValidEnumValue<DirectionType>)
26+
.MustAsync(IsValidDirectionTypeAsync)
27+
.WithMessage(InvalidDirectionType)
2628
.OverridePropertyName("Tipo de Direção");
2729

2830
RuleFor(c => c.Number)
@@ -36,6 +38,28 @@ public CreateLineDTOValidator(ILineRepository lineRepository)
3638
.OverridePropertyName("Nome");
3739
}
3840

41+
private async Task<bool> IsValidDirectionTypeAsync(CreateLineDTO lineDTO, byte directionType, CancellationToken cancellationToken = default)
42+
{
43+
var lines = await _lineRepository.GetManyAsync(c => c.Number.ToLower().Equals(lineDTO.Number.ToLower()) && c.Type == lineDTO.Type,
44+
cancellationToken: cancellationToken);
45+
46+
if (lines is null || !lines.Any())
47+
return true;
48+
49+
if (lines.Any(c => c.DirectionType == directionType))
50+
return false;
51+
52+
if (directionType is (byte)DirectionType.Circular &&
53+
lines.Any(c => c.DirectionType is (byte)DirectionType.Ida or (byte)DirectionType.Volta))
54+
return false;
55+
56+
if (directionType is (byte)DirectionType.Ida or (byte)DirectionType.Volta &&
57+
lines.Any(c => c.DirectionType is (byte)DirectionType.Circular))
58+
return false;
59+
60+
return true;
61+
}
62+
3963
private async Task<bool> IsNumberInUse(string number, byte type, byte directionType, CancellationToken cancellationToken = default)
4064
{
4165
return await _lineRepository.AnyAsync(c => c.Number.ToLower().Equals(number.ToLower()) && c.Type == type && c.DirectionType == directionType,

OneBus.Application/Validators/Line/UpdateLineDTOValidator.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public UpdateLineDTOValidator(ILineRepository lineRepository)
1919

2020
RuleFor(c => c.DirectionType)
2121
.Must(ValidationUtils.IsValidEnumValue<DirectionType>)
22+
.MustAsync(IsValidDirectionTypeAsync)
23+
.WithMessage(CreateLineDTOValidator.InvalidDirectionType)
2224
.OverridePropertyName("Tipo de Direção");
2325

2426
RuleFor(c => c.Number)
@@ -32,6 +34,33 @@ public UpdateLineDTOValidator(ILineRepository lineRepository)
3234
.OverridePropertyName("Nome");
3335
}
3436

37+
private async Task<bool> IsValidDirectionTypeAsync(UpdateLineDTO lineDTO, byte directionType, CancellationToken cancellationToken = default)
38+
{
39+
var line = await _lineRepository.GetOneAsync(c => c.Id == lineDTO.Id, cancellationToken: cancellationToken);
40+
41+
if (line is null)
42+
return false;
43+
44+
var lines = await _lineRepository.GetManyAsync(c => c.Number.ToLower().Equals(line.Number.ToLower()) && c.Type == line.Type,
45+
cancellationToken: cancellationToken);
46+
47+
if (lines is null || !lines.Any())
48+
return false;
49+
50+
if (lines.Any(c => c.DirectionType == directionType))
51+
return false;
52+
53+
if (directionType is (byte)DirectionType.Circular &&
54+
lines.Any(c => c.DirectionType is (byte)DirectionType.Ida or (byte)DirectionType.Volta))
55+
return false;
56+
57+
if (directionType is (byte)DirectionType.Ida or (byte)DirectionType.Volta &&
58+
lines.Any(c => c.DirectionType is (byte)DirectionType.Circular))
59+
return false;
60+
61+
return true;
62+
}
63+
3564
private async Task<bool> IsNumberInUse(long id, string number, CancellationToken cancellationToken = default)
3665
{
3766
var line = await _lineRepository.GetOneAsync(c => c.Id == id, cancellationToken: cancellationToken);

0 commit comments

Comments
 (0)