Skip to content

Commit

Permalink
Merge pull request #106 from jisung-in/feature/105-my-talkroom-find-api
Browse files Browse the repository at this point in the history
[Feature] 나의 토크방 모아보기 API 구현
  • Loading branch information
AHNYUNKI authored May 22, 2024
2 parents 6e78a0c + 0bfe9fa commit 778532a
Show file tree
Hide file tree
Showing 16 changed files with 713 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/docs/asciidoc/api/talkroom/talkroom.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ include::{snippets}/talkroom/delete/path-parameters.adoc[]
==== HTTP Response

include::{snippets}/talkroom/delete/http-response.adoc[]
include::{snippets}/talkroom/delete/response-fields.adoc[]
include::{snippets}/talkroom/delete/response-fields.adoc[]

[[talkroom-findAllByUser]]
=== 나의 토크방 모아보기

==== HTTP Request

include::{snippets}/talkroom/findUserTalkRoom/http-request.adoc[]
include::{snippets}/talkroom/findUserTalkRoom/query-parameters.adoc[]

==== HTTP Response

include::{snippets}/talkroom/findUserTalkRoom/http-response.adoc[]
include::{snippets}/talkroom/findUserTalkRoom/response-fields.adoc[]
13 changes: 13 additions & 0 deletions src/main/java/com/jisungin/api/talkroom/TalkRoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ public ApiResponse<Void> deleteTalkRoom(@PathVariable Long talkRoomId, @Auth Lon
.build();
}

@GetMapping("/users/talk-rooms")
public ApiResponse<TalkRoomPageResponse> findUserTalkRoom(
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
@RequestParam(value = "userTalkRoomsFilter", required = false) boolean userTalkRoomsFilter,
@RequestParam(value = "commentedFilter", required = false) boolean commentedFilter,
@RequestParam(value = "likedFilter", required = false) boolean likedFilter,
@Auth Long userId) {
return ApiResponse.ok(
talkRoomService.findUserTalkRoom(Offset.of(page, size), size, userTalkRoomsFilter, commentedFilter,
likedFilter, userId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static List<CommentFindAllResponse> create(List<CommentQueryResponse> com
.content(comment.getContent())
.commentLikeCount(comment.getCommentLikeCount())
.commentImages(commentImageUrls)
.registeredDateTime(comment.getRegisteredDateTime())
.registeredDateTime(comment.getRegisteredDateTime().withNano(0))
.build();
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public CommentResponse writeComment(CommentCreateServiceRequest request, Long ta
TalkRoom talkRoom = talkRoomRepository.findById(talkRoomId)
.orElseThrow(() -> new BusinessException(ErrorCode.TALK_ROOM_NOT_FOUND));

Optional<ReadingStatus> userReadingStatus = userLibraryRepository.findByUserId(user.getId());
Optional<ReadingStatus> userReadingStatus = userLibraryRepository.findByUserId(user.getId(),
talkRoom.getBook().getIsbn());

List<ReadingStatus> talkRoomReadingStatus = talkRoomRoleRepository.findTalkRoomRoleByTalkRoomId(
talkRoom.getId());
Expand All @@ -66,6 +67,8 @@ public CommentResponse writeComment(CommentCreateServiceRequest request, Long ta
request.getImageUrls().stream()
.map(url -> CommentImage.createImages(comment, url))
.forEach(commentImageRepository::save);
} else {
commentImageRepository.save(CommentImage.createImages(comment, ""));
}

List<String> imageUrls = commentImageRepository.findByCommentIdWithImageUrl(comment.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,29 @@ public void deleteTalkRoom(Long talkRoomId, Long userId) {
talkRoomRepository.delete(talkRoom);
}

public TalkRoomPageResponse findUserTalkRoom(Long offset, Integer size, boolean userTalkRoomsFilter,
boolean commentedFilter,
boolean likedFilter,
Long userId) {
User user = userRepository.findById(userId).orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

List<TalkRoomQueryResponse> findTalkRoom = talkRoomRepository.findByTalkRoomOwner(offset, size,
userTalkRoomsFilter, commentedFilter,
likedFilter,
user.getId());

Map<Long, List<ReadingStatus>> talkRoomRoleMap = talkRoomRoleRepository.findTalkRoomRoleByIds(
findTalkRoom.stream().map(TalkRoomQueryResponse::getId).toList());

List<TalkRoomFindAllResponse> response = TalkRoomFindAllResponse.create(findTalkRoom,
talkRoomRoleMap);

Long totalCount = talkRoomRepository.countTalkRoomsByUserId(user.getId(), userTalkRoomsFilter, commentedFilter,
likedFilter);

List<Long> likeTalkRoomIds = talkRoomLikeRepository.userLikeTalkRooms(userId);

return TalkRoomPageResponse.of(PageResponse.of(findTalkRoom.size(), totalCount, response), likeTalkRoomIds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static List<TalkRoomFindAllResponse> create(List<TalkRoomQueryResponse> t
.bookThumbnail(talkRoom.getBookThumbnail())
.likeCount(talkRoom.getLikeCount())
.readingStatuses(talkRoomReadingStatus)
.registeredDateTime(talkRoom.getRegisteredDateTime())
.registeredDateTime(talkRoom.getRegisteredDateTime().withNano(0))
.build();
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public interface TalkRoomRepository extends JpaRepository<TalkRoom, Long>, TalkR
"select t from TalkRoom t join t.user u join t.book b where t.id = :talkRoomId"
)
TalkRoom findByIdWithUserAndBook(@Param("talkRoomId") Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ List<TalkRoomQueryResponse> findAllTalkRoom(long offset, int size, String order,

TalkRoomQueryResponse findOneTalkRoom(Long talkRoomId);

List<TalkRoomQueryResponse> findByTalkRoomOwner(Long offset, Integer size, boolean userTalkRoomsFilter,
boolean commentFilter,
boolean likeFilter, Long id);

Long countTalkRoomsByUserId(Long userId, boolean userTalkRoomsFilter, boolean commentFilter, boolean likeFilter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,53 @@ public TalkRoomQueryResponse findOneTalkRoom(Long talkRoomId) {
return findTalkRoomByTalkRoomId(talkRoomId);
}

@Override
public List<TalkRoomQueryResponse> findByTalkRoomOwner(Long offset, Integer size, boolean userTalkRoomsFilter,
boolean commentFilter,
boolean likeFilter, Long userId) {
return queryFactory.select(new QTalkRoomQueryResponse(
talkRoom.id,
user.profileImage,
user.name.as("username"),
talkRoom.title,
talkRoom.content,
book.title.as("bookName"),
book.authors.as("bookAuthor"),
book.thumbnail.as("bookThumbnail"),
talkRoomLike.count().as("likeCount"),
talkRoom.registeredDateTime.as("registeredDateTime")
))
.from(talkRoom)
.join(talkRoom.user, user)
.join(talkRoom.book, book)
.leftJoin(talkRoomLike).on(talkRoom.eq(talkRoomLike.talkRoom))
.leftJoin(comment).on(talkRoom.eq(comment.talkRoom))
.where(userTalkRoomEq(userTalkRoomsFilter, userId), commentEq(commentFilter, userId),
likeEq(likeFilter, userId))
.groupBy(talkRoom.id)
.offset(offset)
.limit(size)
.orderBy(talkRoom.registeredDateTime.desc())
.fetch();
}

@Override
public Long countTalkRoomsByUserId(Long userId, boolean userTalkRoomsFilter, boolean commentFilter,
boolean likeFilter) {
return queryFactory
.select(talkRoom.count())
.from(talkRoom)
.join(talkRoom.book, book)
.leftJoin(talkRoomLike).on(talkRoom.eq(talkRoomLike.talkRoom))
.leftJoin(comment).on(talkRoom.eq(comment.talkRoom))
.join(talkRoom.user, user)
.where(userTalkRoomEq(userTalkRoomsFilter, userId), commentEq(commentFilter, userId),
likeEq(likeFilter, userId))
.fetchOne();
}

// 토크룸 페이징 조회 쿼리

private List<TalkRoomQueryResponse> findTalkRoomBySearch(long offset, int size, String order, String search,
String day, LocalDateTime now) {
JPAQuery<TalkRoomQueryResponse> jpaQuery = queryFactory.select(new QTalkRoomQueryResponse(
Expand Down Expand Up @@ -116,6 +162,18 @@ private List<TalkRoomQueryResponse> findTalkRoomBySearch(long offset, int size,
return response;
}

private BooleanExpression userTalkRoomEq(boolean userTalkRoomsFilter, Long userId) {
return userTalkRoomsFilter ? talkRoom.user.id.eq(userId) : null;
}

private BooleanExpression commentEq(boolean commentFilter, Long userId) {
return commentFilter ? comment.user.id.eq(userId) : null;
}

private BooleanExpression likeEq(boolean likeFilter, Long userId) {
return likeFilter ? talkRoomLike.user.id.eq(userId) : null;
}

private void addJoinByOrder(JPAQuery<?> jpaQuery, OrderType orderType) {
if (RECENT_COMMENT.equals(orderType)) {
jpaQuery
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jisungin.domain.talkroomlike.repository;

public interface TalkRoomLikeRepositoryCustom {

boolean existsTalkRoomLike(Long talkRoomId, Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
public interface UserLibraryRepository extends JpaRepository<UserLibrary, Long>, UserLibraryRepositoryCustom {

@Query(
"SELECT ul.status FROM UserLibrary ul JOIN ul.user u WHERE u.id = :id"
"SELECT ul.status FROM UserLibrary ul JOIN ul.user u JOIN ul.book b WHERE u.id = :id AND b.isbn = :isbn"
)
Optional<ReadingStatus> findByUserId(@Param("id") Long userId);
Optional<ReadingStatus> findByUserId(@Param("id") Long userId, @Param("isbn") String isbn);

@Query(
"SELECT ul FROM UserLibrary ul "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,19 @@ void deleteTalkRoom() throws Exception {
.andExpect(jsonPath("$.message").value("삭제 성공"));
}

@Test
@DisplayName("유저가 생성한 토크방을 조회한다.")
void getTalkRoomsOwner() throws Exception {
// when // then
mockMvc.perform(get("/v1/users/talk-rooms?page=1&size=10&order=recent")
.contentType(APPLICATION_JSON)
.session(mockHttpSession)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.status").value("OK"))
.andExpect(jsonPath("$.message").value("OK"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void writeComment() {
// then
assertThat(response)
.extracting("content", "userName", "imageUrls")
.contains("의견 남기기", "[email protected]", List.of());
.contains("의견 남기기", "[email protected]", List.of(""));
}

@Test
Expand Down Expand Up @@ -514,6 +514,7 @@ void findAllComments() throws Exception {
.talkRoom(talkRoom)
.user(user)
.content("의견 " + i)
.registeredDateTime(LocalDateTime.now())
.build())
.collect(Collectors.toList());

Expand Down Expand Up @@ -553,6 +554,7 @@ void findAllCommentsWithImage() throws Exception {
.talkRoom(talkRoom)
.user(user)
.content("의견 " + i)
.registeredDateTime(LocalDateTime.now())
.build())
.toList();

Expand Down Expand Up @@ -601,6 +603,7 @@ void findAllCommentsWithLike() throws Exception {
.talkRoom(talkRoom)
.user(user)
.content("의견 " + i)
.registeredDateTime(LocalDateTime.now())
.build())
.collect(Collectors.toList());

Expand Down Expand Up @@ -652,6 +655,7 @@ private static TalkRoom createTalkRoom(Book book, User user) {
.title("토크방")
.content("내용")
.user(user)
.registeredDateTime(LocalDateTime.now())
.build();
}

Expand Down
Loading

0 comments on commit 778532a

Please sign in to comment.