使用元组重构 java spring boot 代码

问题描述 投票:0回答:0

我有一个代码,我想重构它以删除元组,但我不确定如何在不使用元组的情况下在其他方法中重用 validateInfractionId 和 validateInfractionVersion 方法中的列表。

我最大的问题是,我需要在 validateLineId 和 validateRegistrationId 中重用 validateInfractionId 和 validateInfractionVersion 方法中的标签属性列表,并且我想避免多次调用标签属性 API。

这是我的代码:

@Slf4j
@Service
@RequiredArgsConstructor
public class CreateTicketValidationService {

    private final InfractionService infractionService;
    private final TagClient tagClient;
    private final TagDetailService tagDetailService;

    public Mono<CreationTicketDTO> perform(CreationTicketDTO ticketDTO) {
        return getTagDetail(ticketDTO)
                .flatMap(TupleUtils.function(this::getInfraction))
                .flatMap(TupleUtils.function(this::validateInfractionId))
                .flatMap(TupleUtils.function(this::validateInfractionVersion))
                .flatMap(TupleUtils.function(this::validateLineId))
                .flatMap(TupleUtils.function(this::validateRegistrationId))
                .map(tuple -> ticketDTO);
    }

    private Mono<Tuple2<CreationTicketDTO, TagDetailDTO>> getTagDetail(CreationTicketDTO ticketDTO) {
        return tagDetailService.getTagByNumberAndType(ticketDTO.getTagNumber(), ticketDTO.getTagType())
                .map(tagDetailDTO -> Tuples.of(ticketDTO, tagDetailDTO));
    }

    private Mono<Tuple3<CreationTicketDTO, TagDetailDTO, InfractionDTO>> getInfraction(
            CreationTicketDTO ticketDTO, TagDetailDTO tagDetailDTO) {
        return infractionService.getInfractionByIdAndVersion(ticketDTO.getInfractionId(), ticketDTO.getVersion())
                .map(infractionDTO -> Tuples.of(ticketDTO, tagDetailDTO, infractionDTO));
    }

    private Mono<Tuple4<CreationTicketDTO, TagDetailDTO, InfractionDTO, List<TagAttributeDTO>>> validateInfractionId(
            CreationTicketDTO ticketDTO, TagDetailDTO tagDetailDTO, InfractionDTO infractionDTO) {
        return tagClient
                .getTagAttributeBySodsTagIdAndAttributeId(tagDetailDTO.getSodsTagId(), INFRACTION_ID.getCode())
                .filter(tagAttributeDTO -> infractionDTO.getInfractionId().equalsIgnoreCase(tagAttributeDTO.getAttributeValue()))
                .switchIfEmpty(Mono.error(new BadRequestException(String.format(
                        "InfractionId=%s is not valid for tagNumber=%s and tagType=%s",
                        infractionDTO.getInfractionId(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType()))))
                .collectList()
                .map(infractionIdTagAttributeDTOS ->
                        Tuples.of(ticketDTO, tagDetailDTO, infractionDTO, infractionIdTagAttributeDTOS));
    }

    private Mono<Tuple5<CreationTicketDTO, TagDetailDTO, InfractionDTO, List<TagAttributeDTO>, List<TagAttributeDTO>>> validateInfractionVersion(
            CreationTicketDTO ticketDTO, TagDetailDTO tagDetailDTO, InfractionDTO infractionDTO,
            List<TagAttributeDTO> infractionIdTagAttributeDTOS) {
        return tagClient
                .getTagAttributeBySodsTagIdAndAttributeId(tagDetailDTO.getSodsTagId(), VERSION.getCode())
                .filter(tagAttributeDTO -> infractionDTO.getVersion().equalsIgnoreCase(tagAttributeDTO.getAttributeValue()))
                .switchIfEmpty(Mono.error(new BadRequestException(String.format(
                        "Version=%s is not valid for tagNumber=%s and tagType=%s",
                        infractionDTO.getVersion(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType()))))
                .collectList()
                .map(infractionVersionTagAttributeDTOS ->
                        Tuples.of(ticketDTO, tagDetailDTO, infractionDTO, infractionIdTagAttributeDTOS, infractionVersionTagAttributeDTOS));
    }

    private Mono<Tuple5<CreationTicketDTO, TagDetailDTO, InfractionDTO, List<TagAttributeDTO>, List<TagAttributeDTO>>> validateLineId(
            CreationTicketDTO ticketDTO, TagDetailDTO tagDetailDTO, InfractionDTO infractionDTO,
            List<TagAttributeDTO> infractionIdTagAttributeDTOS, List<TagAttributeDTO> infractionVersionTagAttributeDTOS) {
        if (ticketDTO.getLineId() != null) {
            List<TagAttributeDTO> lineIdInfractionIdTagAttributeDTOS = infractionIdTagAttributeDTOS.stream()
                    .filter(tagAttributeDTO -> ticketDTO.getLineId().toString().equalsIgnoreCase(tagAttributeDTO.getGroupValue()))
                    .toList();

            if (lineIdInfractionIdTagAttributeDTOS.isEmpty()) {
                return Mono.error(new BadRequestException(String.format(
                        "InfractionId=%s is not valid for tagNumber=%s, tagType=%s and lineId=%s",
                        infractionDTO.getInfractionId(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType(), ticketDTO.getLineId())));
            }

            List<TagAttributeDTO> lineIdInfractionVersionTagAttributeDTOS = infractionVersionTagAttributeDTOS.stream()
                    .filter(tagAttributeDTO -> ticketDTO.getLineId().toString().equalsIgnoreCase(tagAttributeDTO.getGroupValue()))
                    .toList();

            if (lineIdInfractionVersionTagAttributeDTOS.isEmpty()) {
                return Mono.error(new BadRequestException(String.format(
                        "Version=%s is not valid for tagNumber=%s, tagType=%s and lineId=%s",
                        infractionDTO.getVersion(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType(), ticketDTO.getLineId())));
            }
        }

        return Mono.just(Tuples.of(ticketDTO, tagDetailDTO, infractionDTO, infractionIdTagAttributeDTOS, infractionVersionTagAttributeDTOS));
    }

    private Mono<Tuple5<CreationTicketDTO, TagDetailDTO, InfractionDTO, List<TagAttributeDTO>, List<TagAttributeDTO>>> validateRegistrationId(
            CreationTicketDTO ticketDTO, TagDetailDTO tagDetailDTO, InfractionDTO infractionDTO,
            List<TagAttributeDTO> infractionIdTagAttributeDTOS, List<TagAttributeDTO> infractionVersionTagAttributeDTOS) {
        if (ticketDTO.getRegistrationId() != null) {
            return tagClient
                    .getTagAttributeBySodsTagIdAndAttributeId(tagDetailDTO.getSodsTagId(), REGISTRATION_ID.getCode())
                    .filter(tagAttributeDTO -> ticketDTO.getRegistrationId().toString().equalsIgnoreCase(tagAttributeDTO.getAttributeValue()))
                    .next()
                    .switchIfEmpty(Mono.error(new BadRequestException(String.format(
                            "RegistrationId=%s is not valid for tagNumber=%s and tagType=%s",
                            ticketDTO.getRegistrationId(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType()))))
                    .flatMap(registrationIdTagAttributeDTO -> {
                        List<TagAttributeDTO> lineIdInfractionIdTagAttributeDTOS = infractionIdTagAttributeDTOS.stream()
                                .filter(tagAttributeDTO ->
                                        registrationIdTagAttributeDTO.getGroupValue().equalsIgnoreCase(tagAttributeDTO.getGroupValue()))
                                .toList();

                        if (lineIdInfractionIdTagAttributeDTOS.isEmpty()) {
                            return Mono.error(new BadRequestException(String.format(
                                    "InfractionId=%s is not valid for tagNumber=%s, tagType=%s and registrationId=%s",
                                    infractionDTO.getInfractionId(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType(), ticketDTO.getRegistrationId())));
                        }

                        List<TagAttributeDTO> lineIdInfractionVersionTagAttributeDTOS = infractionVersionTagAttributeDTOS.stream()
                                .filter(tagAttributeDTO -> registrationIdTagAttributeDTO.getGroupValue().equalsIgnoreCase(tagAttributeDTO.getGroupValue()))
                                .toList();

                        if (lineIdInfractionVersionTagAttributeDTOS.isEmpty()) {
                            return Mono.error(new BadRequestException(String.format(
                                    "Version=%s is not valid for tagNumber=%s, tagType=%s and registrationId=%s",
                                    infractionDTO.getVersion(), tagDetailDTO.getTagNumber(), tagDetailDTO.getTagType(), ticketDTO.getRegistrationId())));
                        }

                        return Mono.just(Tuples.of(ticketDTO, tagDetailDTO, infractionDTO, infractionIdTagAttributeDTOS, infractionVersionTagAttributeDTOS));
                    });
        }

        return Mono.just(Tuples.of(ticketDTO, tagDetailDTO, infractionDTO, infractionIdTagAttributeDTOS, infractionVersionTagAttributeDTOS));
    }
}

关于如何重构它有什么建议吗?

java spring-boot spring-webflux project-reactor
© www.soinside.com 2019 - 2024. All rights reserved.