Created
September 28, 2016 13:39
-
-
Save ktabriga/b33116d819299343ee7ecd4628bb477f to your computer and use it in GitHub Desktop.
Functional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ErroDTO validationErro(String mensagem) { | |
return ErroDTO.from("anoLetivo", "validation", mensagem); | |
} | |
private Either<ErroDTO, AnoLetivo> validations(AnoLetivo anoLetivo) { | |
if (anoLetivo.getAno().toString().length() != 4) { | |
return left(validationErro("Ano inválido")); | |
} | |
if ( | |
anoLetivoRepository.findByAno(anoLetivo.getAno()) | |
.filter(found -> !found.getId().equals(anoLetivo.getId())) | |
.isPresent() | |
) { | |
return left(validationErro("Já foi cadastrado um ano letivo deste ano " + anoLetivo.getAno())); | |
} | |
return right(anoLetivo); | |
} | |
public Either<ErroDTO, AnoLetivo> saveAno(AnoLetivo anoLetivo) { | |
log.debug("Request to save AnoLetivo : {}", anoLetivo); | |
return validations(anoLetivo) | |
.map(this::manterAnoAtivo) | |
.map(anoLetivoRepository::save); | |
} | |
private AnoLetivo manterAnoAtivo(AnoLetivo anoLetivo) { | |
if (!anoLetivo.isBoAtivo()) return anoLetivo; | |
anoLetivoRepository.findAll() | |
.stream() | |
.filter(AnoLetivo::isBoAtivo) | |
.forEach(a -> { | |
a.setBoAtivo(false); | |
anoLetivoRepository.save(a); | |
}); | |
return anoLetivo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment