Skip to content

Commit

Permalink
Merge pull request #42 from StudyFlexUMC5th/yeeun2
Browse files Browse the repository at this point in the history
Yeeun2
  • Loading branch information
Yeeun411 authored Jan 13, 2024
2 parents 610a5af + 5bdb2d7 commit f058a3d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,19 +68,19 @@ public BaseResponse<?> participation(@PathVariable Long study_id){
}
}
@GetMapping("/latest")
public ResponseEntity<List<Study>> getLatestStudies() {
List<Study> latestStudies = studyService.getLatestStudies();
public ResponseEntity<List<StudyMainPageResponseDto>> getLatestStudies() {
List<StudyMainPageResponseDto> latestStudies = studyService.getLatestStudies();
return ResponseEntity.ok(latestStudies);
}

@GetMapping("/open")
public ResponseEntity<List<Study>> getOpenStudies() {
List<Study> openStudies = studyService.getOpenStudies();
public ResponseEntity<List<StudyMainPageResponseDto>> getOpenStudies() {
List<StudyMainPageResponseDto> openStudies = studyService.getOpenStudies();
return ResponseEntity.ok(openStudies);
}
@GetMapping("/ranking")
public ResponseEntity<List<Study>> getStudyRanking() {
List<Study> rankedStudies = studyService.getRankedStudies();
public ResponseEntity<List<StudyMainPageResponseDto>> getStudyRanking() {
List<StudyMainPageResponseDto> rankedStudies = studyService.getRankedStudies();
return ResponseEntity.ok(rankedStudies);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
3 changes: 3 additions & 0 deletions src/main/java/com/umc/StudyFlexBE/entity/Category.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.StudyFlexBE.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -21,6 +22,8 @@ public class Category {
private String name;

@OneToOne(mappedBy = "category")
@JsonIgnore

private Study study;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,7 +19,7 @@ public interface StudyRepository extends JpaRepository<Study, Long> {

List<Study> findTop5ByOrderByCreatedAtDesc();

List<Study> findByStatus(String status);
List<Study> findByStatus(StudyStatus studyStatus);

boolean existsByName(String name);

Expand Down
41 changes: 36 additions & 5 deletions src/main/java/com/umc/StudyFlexBE/service/StudyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,34 @@ public StudyService(
this.awsS3Service = awsS3Service;
}

public List<Study> getLatestStudies() {
return studyRepository.findTop5ByOrderByCreatedAtDesc();
public List<StudyMainPageResponseDto> getLatestStudies() {
List<Study> 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<Study> getOpenStudies() {
return studyRepository.findByStatus("모집중"); // 또는 StudyStatus.모집중, enum 사용시

public List<StudyMainPageResponseDto> getOpenStudies() {
List<Study> 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){
Expand Down Expand Up @@ -175,12 +198,20 @@ public StudyRes createStudy(StudyReq studyReq, String email){
}


public List<Study> getRankedStudies() {
public List<StudyMainPageResponseDto> getRankedStudies() {
List<Study> 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());
}

Expand Down

0 comments on commit f058a3d

Please sign in to comment.