From ffd54d188b046a063f51e405c16f995506589ba1 Mon Sep 17 00:00:00 2001 From: Yeaeun411 Date: Sun, 14 Jan 2024 00:02:48 +0900 Subject: [PATCH] Fix: mainPage api err --- .../controller/StudyController.java | 13 +++--- .../response/StudyMainPageResponseDto.java | 28 +++++++++++++ .../com/umc/StudyFlexBE/entity/Category.java | 3 ++ .../repository/StudyRepository.java | 3 +- .../umc/StudyFlexBE/service/StudyService.java | 41 ++++++++++++++++--- 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/umc/StudyFlexBE/dto/response/StudyMainPageResponseDto.java diff --git a/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java b/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java index 6afda8a..056c715 100644 --- a/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java +++ b/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java @@ -3,7 +3,6 @@ import com.umc.StudyFlexBE.dto.request.StudyNoticeReq; import com.umc.StudyFlexBE.dto.request.StudyReq; import com.umc.StudyFlexBE.dto.response.*; -import com.umc.StudyFlexBE.entity.Study; import com.umc.StudyFlexBE.service.StudyService; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -51,19 +50,19 @@ public BaseResponse participation(@PathVariable Long study_id){ return new BaseResponse<>(BaseResponseStatus.SUCCESS, "스터디 참여에 성공했습니다."); } @GetMapping("/latest") - public ResponseEntity> getLatestStudies() { - List latestStudies = studyService.getLatestStudies(); + public ResponseEntity> getLatestStudies() { + List latestStudies = studyService.getLatestStudies(); return ResponseEntity.ok(latestStudies); } @GetMapping("/open") - public ResponseEntity> getOpenStudies() { - List openStudies = studyService.getOpenStudies(); + public ResponseEntity> getOpenStudies() { + List openStudies = studyService.getOpenStudies(); return ResponseEntity.ok(openStudies); } @GetMapping("/ranking") - public ResponseEntity> getStudyRanking() { - List rankedStudies = studyService.getRankedStudies(); + public ResponseEntity> getStudyRanking() { + List rankedStudies = studyService.getRankedStudies(); return ResponseEntity.ok(rankedStudies); } diff --git a/src/main/java/com/umc/StudyFlexBE/dto/response/StudyMainPageResponseDto.java b/src/main/java/com/umc/StudyFlexBE/dto/response/StudyMainPageResponseDto.java new file mode 100644 index 0000000..a1aedd0 --- /dev/null +++ b/src/main/java/com/umc/StudyFlexBE/dto/response/StudyMainPageResponseDto.java @@ -0,0 +1,28 @@ +package com.umc.StudyFlexBE.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/* +"studyId": 1, + "studyName": "파이썬 마스터", + "thumbnailUrl": "https://example.com/images/study-python-thumbnail.jpg", + "studyStatus": "모집중", + "maxMembers": 5, + "currentMembers": 3, + */ +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class StudyMainPageResponseDto { + private int studyId; + private String studyName; + private String thumbnailUrl; + private String studyStatus; + private int maxMembers; + private int currentMembers; + +} diff --git a/src/main/java/com/umc/StudyFlexBE/entity/Category.java b/src/main/java/com/umc/StudyFlexBE/entity/Category.java index 4cd447c..6999386 100644 --- a/src/main/java/com/umc/StudyFlexBE/entity/Category.java +++ b/src/main/java/com/umc/StudyFlexBE/entity/Category.java @@ -1,5 +1,6 @@ package com.umc.StudyFlexBE.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; @@ -21,6 +22,8 @@ public class Category { private String name; @OneToOne(mappedBy = "category") + @JsonIgnore + private Study study; } diff --git a/src/main/java/com/umc/StudyFlexBE/repository/StudyRepository.java b/src/main/java/com/umc/StudyFlexBE/repository/StudyRepository.java index 3b04a6e..1259e8a 100644 --- a/src/main/java/com/umc/StudyFlexBE/repository/StudyRepository.java +++ b/src/main/java/com/umc/StudyFlexBE/repository/StudyRepository.java @@ -2,6 +2,7 @@ import com.umc.StudyFlexBE.entity.Category; import com.umc.StudyFlexBE.entity.Study; +import com.umc.StudyFlexBE.entity.StudyStatus; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -18,7 +19,7 @@ public interface StudyRepository extends JpaRepository { List findTop5ByOrderByCreatedAtDesc(); - List findByStatus(String status); + List findByStatus(StudyStatus studyStatus); boolean existsByName(String name); diff --git a/src/main/java/com/umc/StudyFlexBE/service/StudyService.java b/src/main/java/com/umc/StudyFlexBE/service/StudyService.java index 4de7e7d..72033d7 100644 --- a/src/main/java/com/umc/StudyFlexBE/service/StudyService.java +++ b/src/main/java/com/umc/StudyFlexBE/service/StudyService.java @@ -51,11 +51,34 @@ public StudyService( this.awsS3Service = awsS3Service; } - public List getLatestStudies() { - return studyRepository.findTop5ByOrderByCreatedAtDesc(); + public List getLatestStudies() { + List latestStudies = studyRepository.findTop5ByOrderByCreatedAtDesc(); + + return latestStudies.stream() + .map(study -> StudyMainPageResponseDto.builder() + .studyId(study.getId().intValue()) + .studyName(study.getName()) + .thumbnailUrl(study.getThumbnailUrl()) + .studyStatus(study.getStatus().toString()) + .maxMembers(study.getMaxMembers()) + .currentMembers(study.getCurrentMembers()) + .build()) + .collect(Collectors.toList()); } - public List getOpenStudies() { - return studyRepository.findByStatus("모집중"); // 또는 StudyStatus.모집중, enum 사용시 + + public List getOpenStudies() { + List openedStudies = studyRepository.findByStatus(StudyStatus.valueOf("RECRUITING")); + + return openedStudies.stream() + .map(study -> StudyMainPageResponseDto.builder() + .studyId(study.getId().intValue()) + .studyName(study.getName()) + .thumbnailUrl(study.getThumbnailUrl()) + .studyStatus(study.getStatus().toString()) + .maxMembers(study.getMaxMembers()) + .currentMembers(study.getCurrentMembers()) + .build()) + .collect(Collectors.toList()); } public void checkDuplicateStudyName(String name){ @@ -160,12 +183,20 @@ public Long createStudy(StudyReq studyReq, String email){ } - public List getRankedStudies() { + public List getRankedStudies() { List studies = studyRepository.findAll(); studies.forEach(this::calculateRankScore); return studies.stream() .sorted((s1, s2) -> Double.compare(s2.getRankScore(), s1.getRankScore())) .limit(3) + .map(study -> StudyMainPageResponseDto.builder() + .studyId(study.getId().intValue()) // Long을 int로 변환 (필요에 따라 조정) + .studyName(study.getName()) + .thumbnailUrl(study.getThumbnailUrl()) + .studyStatus(study.getStatus().toString()) + .maxMembers(study.getMaxMembers()) + .currentMembers(study.getCurrentMembers()) + .build()) .collect(Collectors.toList()); }