diff --git a/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryService.java b/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryService.java new file mode 100644 index 0000000..3e83144 --- /dev/null +++ b/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryService.java @@ -0,0 +1,9 @@ +package org.mjulikelion.baker.service.introduce; + +import org.mjulikelion.baker.dto.response.ResponseDto; +import org.mjulikelion.baker.dto.response.introduce.IntroduceGetResponseData; +import org.springframework.http.ResponseEntity; + +public interface IntroduceQueryService { + ResponseEntity> getStudentIntroduce(String studentId); +} diff --git a/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryServiceImpl.java b/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryServiceImpl.java new file mode 100644 index 0000000..ec0825b --- /dev/null +++ b/src/main/java/org/mjulikelion/baker/service/introduce/IntroduceQueryServiceImpl.java @@ -0,0 +1,82 @@ +package org.mjulikelion.baker.service.introduce; + +import static org.mjulikelion.baker.errorcode.ErrorCode.APPLICATION_INTRODUCE_NOT_FOUND_ERROR; +import static org.mjulikelion.baker.errorcode.ErrorCode.APPLICATION_NOT_FOUND_ERROR; + +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import org.mjulikelion.baker.dto.response.ResponseDto; +import org.mjulikelion.baker.dto.response.introduce.IntroduceGetResponseData; +import org.mjulikelion.baker.exception.ApplicationIntroduceNotFoundException; +import org.mjulikelion.baker.exception.ApplicationNotFoundException; +import org.mjulikelion.baker.model.Application; +import org.mjulikelion.baker.model.ApplicationIntroduce; +import org.mjulikelion.baker.model.Introduce; +import org.mjulikelion.baker.repository.ApplicationIntroduceRepository; +import org.mjulikelion.baker.repository.ApplicationRepository; +import org.mjulikelion.baker.vo.IntroduceDetailVO; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@AllArgsConstructor +public class IntroduceQueryServiceImpl implements IntroduceQueryService { + private final ApplicationRepository applicationRepository; + private final ApplicationIntroduceRepository applicationIntroduceRepository; + + @Override + @Transactional(readOnly = true) + public ResponseEntity> getStudentIntroduce(String studentId) { + Application application = this.findApplicationByStudentId(studentId); + List applicationIntroduceList = this.findApplicationIntroduces(application.getId()); + List introduceDetailVOList = this.buildIntroduceDetailVOList(applicationIntroduceList); + + IntroduceGetResponseData introduceGetResponseData = IntroduceGetResponseData.builder() + .introduceDetailVOList(introduceDetailVOList) + .part(application.getPart()) + .build(); + + return ResponseEntity.ok(ResponseDto.res(HttpStatus.OK, "OK", introduceGetResponseData)); + } + + private Application findApplicationByStudentId(String studentId) { + return this.applicationRepository.findByStudentId(studentId) + .orElseThrow(() -> new ApplicationNotFoundException(APPLICATION_NOT_FOUND_ERROR)); + } + + private List findApplicationIntroduces(UUID applicationId) { + List applicationIntroduceList = this.applicationIntroduceRepository.findByApplicationId( + applicationId); + if (applicationIntroduceList.isEmpty()) { + throw new ApplicationIntroduceNotFoundException(APPLICATION_INTRODUCE_NOT_FOUND_ERROR); + } + return applicationIntroduceList; + } + + private List buildIntroduceDetailVOList(List applicationIntroduceList) { + Map introduceMap = applicationIntroduceList.stream() + .sorted(Comparator.comparingInt(a -> a.getIntroduce().getSequence())) + .collect(Collectors.toMap( + ApplicationIntroduce::getIntroduce, + ApplicationIntroduce::getContent, + (a, b) -> b, + LinkedHashMap::new + )); + + return introduceMap.keySet().stream() + .map(introduce -> IntroduceDetailVO.builder() + .sequence(introduce.getSequence()) + .title(introduce.getTitle()) + .content(introduceMap.get(introduce)) + .build()) + .collect(Collectors.toList()); + } +} +