Skip to content

Commit

Permalink
Merge pull request #135 from Team-GAJI/feature/#67-user-studyroom/GAJ…
Browse files Browse the repository at this point in the history
…I-98

🎨 [Fix] #67 - 스터디룸 가져오기 API 수정
  • Loading branch information
karryred authored Aug 21, 2024
2 parents d87f396 + 9651304 commit ead8a0d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private <T> Slice<T> checkLastPage(Pageable pageable, List<T> postList) {
private OrderSpecifier orderBySortType(SortType sortTypeCond) {
return switch (sortTypeCond) {
case HOT -> commnuityPost.popularityScore.desc(); // HOT: 인기점수 내림차순
case LIKE -> commnuityPost.createdAt.desc(); // LIKE: 좋아요 내림차순
case LIKE -> commnuityPost.likeCnt.desc(); // LIKE: 좋아요 내림차순
case HIT -> commnuityPost.hit.desc(); // HIT: 조회수 내림차순
default -> commnuityPost.createdAt.desc(); // null or RECENT: 최신순(생성일자 내림차순)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@

public interface RoomCustomRepository {
public Slice<Tuple> findAllOngoingRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable);
public Slice<Tuple> findAllOngoingRoomsByUser(User user, Pageable pageable);
public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable);
public Slice<Tuple> findAllEndedRoomsByUser(User user, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public Slice<Tuple> findAllOngoingRoomsByUser(User user, LocalDate cursorDate, L
List<Tuple> ongoingRooms = jpaQueryFactory.select(room.id, room.name, room.description, room.thumbnailUrl, room.studyStartDay)
.from(room)
.where(room.id.in(userRoomIds)
.and(room.studyEndDay.after(getCurrentDay()))
.and(room.studyStartDay.before(getCurrentDay()).or(room.studyStartDay.goe(getCurrentDay()))) // 시작일이 현재 날짜 이전이거나 이후인 경우 포함
.and(room.studyEndDay.after(getCurrentDay().minusDays(1)))
.and(getCursorCondition(cursorDate, cursorId)))
.orderBy(room.studyStartDay.desc(), room.id.asc())
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인
Expand All @@ -43,20 +42,33 @@ public Slice<Tuple> findAllOngoingRoomsByUser(User user, LocalDate cursorDate, L
return checkLastPage(pageable, ongoingRooms);
}

public Slice<Tuple> findAllOngoingRoomsByUser(User user, Pageable pageable) {
List<Long> userRoomIds = jpaQueryFactory
.select(studyMate.room.id)
.from(studyMate)
.where(studyMate.user.eq(user))
.fetch();

public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable) {
LocalDate now = LocalDate.now();
List<Tuple> ongoingRooms = jpaQueryFactory.select(room.id, room.name, room.description, room.thumbnailUrl, room.studyStartDay)
.from(room)
.where(room.id.in(userRoomIds)
.and(room.studyEndDay.after(getCurrentDay().minusDays(1))))
.orderBy(room.studyStartDay.desc(), room.id.asc())
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인
.fetch();

BooleanExpression cursorCondition = (room.studyStartDay.eq(cursorDate).and(room.id.gt(cursorId)))
.or(room.studyStartDay.lt(cursorDate));
return checkLastPage(pageable, ongoingRooms);
}


public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable) {
List<Long> userRoomIds = jpaQueryFactory
.select(studyMate.room.id)
.from(studyMate)
.where(studyMate.user.eq(user))
.fetch();

List<Tuple> ongoingRooms = jpaQueryFactory.select(room.id, room.name, room.description, room.thumbnailUrl, room.studyStartDay)
List<Tuple> endedRooms = jpaQueryFactory.select(room.id, room.name, room.description, room.thumbnailUrl, room.studyStartDay)
.from(room)
.where(room.id.in(userRoomIds)
.and(room.studyEndDay.before(getCurrentDay()))
Expand All @@ -65,7 +77,25 @@ public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Lon
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인
.fetch();

return checkLastPage(pageable, ongoingRooms);
return checkLastPage(pageable, endedRooms);
}

public Slice<Tuple> findAllEndedRoomsByUser(User user, Pageable pageable) {
List<Long> userRoomIds = jpaQueryFactory
.select(studyMate.room.id)
.from(studyMate)
.where(studyMate.user.eq(user))
.fetch();

List<Tuple> endedRooms = jpaQueryFactory.select(room.id, room.name, room.description, room.thumbnailUrl, room.studyStartDay)
.from(room)
.where(room.id.in(userRoomIds)
.and(room.studyEndDay.before(getCurrentDay())))
.orderBy(room.studyStartDay.desc(), room.id.asc())
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인
.fetch();

return checkLastPage(pageable, endedRooms);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class RoomBoard {
@JoinColumn(name = "room_id")
private Room room;

//추후 삭제합시다. RoomPostType으로 한정된 게시판을 관리한다면 필요 없을거같습니다.
private String name;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,25 @@ public Slice<Tuple> getUserRoomList(Long userId, LocalDate cursorDate, Long curs
User user = userRepository.findById(userId)
.orElseThrow(() -> new RestApiException(UserErrorStatus._USER_NOT_FOUND));

cursorDate = cursorDate == null ? LocalDate.now() : cursorDate;
cursorId = cursorId == null ? 0 : cursorId;

PageRequest pageRequest = PageRequest.of(0, size);

Slice<Tuple> roomList;

if(type == RoomTypeEnum.ONGOING) {
roomList = roomCustomRepository.findAllOngoingRoomsByUser(user, cursorDate, cursorId, pageRequest);
if(cursorId != null || cursorDate != null) {
roomList = roomCustomRepository.findAllOngoingRoomsByUser(user, cursorDate, cursorId, pageRequest);
}
else {
roomList = roomCustomRepository.findAllOngoingRoomsByUser(user, pageRequest);
}
}
else{
roomList = roomCustomRepository.findAllEndedRoomsByUser(user, cursorDate, cursorId, pageRequest);
if(cursorId != null || cursorDate != null) {
roomList = roomCustomRepository.findAllEndedRoomsByUser(user, cursorDate, cursorId, pageRequest);
}
else {
roomList = roomCustomRepository.findAllEndedRoomsByUser(user, pageRequest);
}
}

return roomList;
Expand Down

0 comments on commit ead8a0d

Please sign in to comment.