@@ -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 ,
0 commit comments