-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
✨[feature] #67 유저 스터디룸 목록 조회 API
- Loading branch information
Showing
13 changed files
with
291 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package gaji.service.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class QueryDSLConfig{ | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,4 @@ private Info apiInfo() { | |
.description("GAJI API 명세서입니다.") | ||
.version("1.0.0"); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/gaji/service/domain/common/annotation/CheckPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package gaji.service.domain.common.annotation; | ||
|
||
import gaji.service.domain.common.validation.PageNumberValidator; | ||
import jakarta.validation.Payload; | ||
import jakarta.validation.Constraint; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Documented | ||
@Constraint(validatedBy = PageNumberValidator.class) | ||
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CheckPage { | ||
String message() default "유효하지 않은 페이지 숫자 입니다."; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/gaji/service/domain/common/validation/PageNumberValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package gaji.service.domain.common.validation; | ||
|
||
import gaji.service.domain.common.annotation.CheckPage; | ||
import gaji.service.global.exception.code.status.GlobalErrorStatus; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PageNumberValidator implements ConstraintValidator<CheckPage, Integer> { | ||
|
||
@Override | ||
public void initialize(CheckPage constraintAnnotation) { | ||
ConstraintValidator.super.initialize(constraintAnnotation); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Integer value, ConstraintValidatorContext context) { | ||
boolean isValid = value>=0; | ||
|
||
if (!isValid){ | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(GlobalErrorStatus._INTERNAL_PAGE_ERROR.toString()) | ||
.addConstraintViolation(); | ||
} | ||
|
||
return isValid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package gaji.service.domain.enums; | ||
|
||
public enum RoomTypeEnum { | ||
ONGOING, ENDED | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/gaji/service/domain/room/repository/RoomCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package gaji.service.domain.room.repository; | ||
|
||
import com.querydsl.core.Tuple; | ||
import gaji.service.domain.user.entity.User; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface RoomCustomRepository { | ||
public Slice<Tuple> findAllOngoingRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable); | ||
public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable); | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/gaji/service/domain/room/repository/RoomCustomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package gaji.service.domain.room.repository; | ||
|
||
import com.querydsl.core.Tuple; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import gaji.service.domain.room.entity.QRoom; | ||
import gaji.service.domain.studyMate.QStudyMate; | ||
import gaji.service.domain.user.entity.User; | ||
import lombok.AllArgsConstructor; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Repository | ||
@AllArgsConstructor | ||
public class RoomCustomRepositoryImpl implements RoomCustomRepository { | ||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
private final QRoom room = QRoom.room; | ||
private final QStudyMate studyMate = QStudyMate.studyMate; | ||
|
||
public Slice<Tuple> findAllOngoingRoomsByUser(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) | ||
.from(room) | ||
.where(room.id.in(userRoomIds) | ||
.and(room.studyEndDay.after(getCurrentDay())) | ||
.and(getCursorCondition(cursorDate, cursorId))) | ||
.orderBy(room.studyStartDay.desc(), room.id.asc()) | ||
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인 | ||
.fetch(); | ||
|
||
return checkLastPage(pageable, ongoingRooms); | ||
} | ||
|
||
|
||
public Slice<Tuple> findAllEndedRoomsByUser(User user, LocalDate cursorDate, Long cursorId, Pageable pageable) { | ||
LocalDate now = LocalDate.now(); | ||
|
||
BooleanExpression cursorCondition = (room.studyStartDay.eq(cursorDate).and(room.id.gt(cursorId))) | ||
.or(room.studyStartDay.lt(cursorDate)); | ||
|
||
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) | ||
.from(room) | ||
.where(room.id.in(userRoomIds) | ||
.and(room.studyEndDay.before(getCurrentDay())) | ||
.and(getCursorCondition(cursorDate, cursorId))) | ||
.orderBy(room.studyStartDay.desc(), room.id.asc()) | ||
.limit(pageable.getPageSize()+1) // size보다 1개 더 가져와서 다음 페이지 여부 확인 | ||
.fetch(); | ||
|
||
return checkLastPage(pageable, ongoingRooms); | ||
} | ||
|
||
|
||
private Slice<Tuple> checkLastPage(Pageable pageable, List<Tuple> roomList) { | ||
boolean hasNext = false; | ||
|
||
if (roomList.size() > pageable.getPageSize()) { | ||
hasNext = true; | ||
roomList.remove(pageable.getPageSize()); // 더 가져왔을 시, 삭제 | ||
} | ||
return new SliceImpl<Tuple>(roomList, pageable, hasNext); | ||
} | ||
|
||
private BooleanExpression getCursorCondition(LocalDate cursorDate, Long cursorId) { | ||
return (room.studyStartDay.eq(cursorDate).and(room.id.gt(cursorId))) | ||
.or(room.studyStartDay.lt(cursorDate)); | ||
} | ||
|
||
private LocalDate getCurrentDay() { | ||
return LocalDate.now(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/gaji/service/domain/user/service/UserQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
package gaji.service.domain.user.service; | ||
|
||
|
||
import com.querydsl.core.Tuple; | ||
import gaji.service.domain.enums.RoomTypeEnum; | ||
import gaji.service.domain.user.entity.User; | ||
import org.springframework.data.domain.Slice; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface UserQueryService { | ||
|
||
boolean existUserById(Long userId); | ||
User findUserById(Long userId); | ||
Slice<Tuple> getUserRoomList(Long userId, LocalDate cursorDate, Long cursorId, RoomTypeEnum type, int size); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.