Skip to content

Commit

Permalink
Merge pull request #23 from jisung-in/refactor/20-talkroom-refactor-1
Browse files Browse the repository at this point in the history
[Refactor] TalkRoom API 리펙토링
  • Loading branch information
AHNYUNKI authored Mar 23, 2024
2 parents 513b449 + 5bf26d2 commit 6174567
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.jisungin.api.talkroom.request.TalkRoomSearchRequest;
import com.jisungin.application.response.PageResponse;
import com.jisungin.application.talkroom.TalkRoomService;
import com.jisungin.application.talkroom.response.TalkRoomQueryResponse;
import com.jisungin.application.talkroom.response.TalkRoomResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,7 +32,7 @@ public ApiResponse<TalkRoomResponse> createTalkRoom(@Valid @RequestBody TalkRoom
}

@GetMapping("/talk-rooms")
public ApiResponse<PageResponse> getTalkRooms(@ModelAttribute TalkRoomSearchRequest search) {
public ApiResponse<PageResponse<TalkRoomQueryResponse>> getTalkRooms(@ModelAttribute TalkRoomSearchRequest search) {
return ApiResponse.ok(talkRoomService.getTalkRooms(search.toService()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jisungin.api.talkroom.request;

import com.jisungin.application.talkroom.request.TalkRoomCreateServiceRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -17,23 +18,29 @@ public class TalkRoomCreateRequest {
@NotNull(message = "책 isbn은 필수입니다.")
private String bookIsbn;

@NotEmpty(message = "주제는 필수 입니다.")
@NotBlank(message = "주제는 필수 입니다.")
@Size(min = 1, max = 20, message = "20자 이하로 작성해야 합니다.")
private String title;

@NotEmpty(message = "내용은 필수 입니다.")
@Size(min = 1, max = 2000, message = "2000자 이하로 작성해야 합니다.")
private String content;

@NotEmpty(message = "참가 조건은 1개 이상 체크해야합니다.")
private List<String> readingStatus = new ArrayList<>();

@Builder
private TalkRoomCreateRequest(String bookIsbn, String content, List<String> readingStatus) {
private TalkRoomCreateRequest(String bookIsbn, String title, String content, List<String> readingStatus) {
this.bookIsbn = bookIsbn;
this.title = title;
this.content = content;
this.readingStatus = readingStatus;
}

public TalkRoomCreateServiceRequest toServiceRequest() {
return TalkRoomCreateServiceRequest.builder()
.bookIsbn(bookIsbn)
.title(title)
.content(content)
.readingStatus(readingStatus)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ public class TalkRoomEditRequest {

private Long id;

@Size(min = 1, max = 20, message = "20자 이하로 작성해야 합니다.")
private String title;

@Size(min = 1, max = 2000, message = "2000자 이하로 작성해야 합니다.")
private String content;

@NotEmpty(message = "참가 조건은 1개 이상 체크해야합니다.")
private List<String> readingStatus = new ArrayList<>();

@Builder
private TalkRoomEditRequest(Long id, String content, List<String> readingStatus) {
private TalkRoomEditRequest(Long id, String title, String content, List<String> readingStatus) {
this.id = id;
this.title = title;
this.content = content;
this.readingStatus = readingStatus;
}

public TalkRoomEditServiceRequest toServiceRequest() {
return TalkRoomEditServiceRequest.builder()
.id(id)
.title(title)
.content(content)
.readingStatus(readingStatus)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jisungin.application.talkroom.request.TalkRoomCreateServiceRequest;
import com.jisungin.application.talkroom.request.TalkRoomEditServiceRequest;
import com.jisungin.application.talkroom.request.TalkRoomSearchServiceRequest;
import com.jisungin.application.talkroom.response.TalkRoomQueryResponse;
import com.jisungin.application.talkroom.response.TalkRoomResponse;
import com.jisungin.domain.ReadingStatus;
import com.jisungin.domain.book.Book;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class TalkRoomService {
// TODO. 토큰 정보를 가져오는 기능을 구현하면 변경할 예정
@Transactional
public TalkRoomResponse createTalkRoom(TalkRoomCreateServiceRequest request, String userEmail) {
// User user = userRepository.findById(userId)
// .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

User user = userRepository.findByName(userEmail)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

Expand All @@ -48,10 +52,10 @@ public TalkRoomResponse createTalkRoom(TalkRoomCreateServiceRequest request, Str
readingStatus.stream().map(status -> TalkRoomRole.roleCreate(talkRoom, status))
.forEach(talkRoomRoleRepository::save);

return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, book.getUrl());
return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, book.getUrl(), book.getTitle());
}

public PageResponse getTalkRooms(TalkRoomSearchServiceRequest search) {
public PageResponse<TalkRoomQueryResponse> getTalkRooms(TalkRoomSearchServiceRequest search) {
return talkRoomRepository.getTalkRooms(search);
}

Expand All @@ -60,7 +64,7 @@ public TalkRoomResponse editTalkRoom(TalkRoomEditServiceRequest request, String
User user = userRepository.findByName(userEmail)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

TalkRoom talkRoom = talkRoomRepository.findByIdWithUser(request.getId());
TalkRoom talkRoom = talkRoomRepository.findByIdWithUserAndBook(request.getId());

if (!talkRoom.isTalkRoomOwner(user.getId())) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_REQUEST);
Expand All @@ -75,7 +79,8 @@ public TalkRoomResponse editTalkRoom(TalkRoomEditServiceRequest request, String
readingStatus.stream().map(status -> TalkRoomRole.roleCreate(talkRoom, status))
.forEach(talkRoomRoleRepository::save);

return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, talkRoom.getBook().getUrl());
return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, talkRoom.getBook().getUrl(),
talkRoom.getBook().getTitle());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ public class TalkRoomCreateServiceRequest {

private String bookIsbn;

private String title;

private String content;

private List<String> readingStatus;

@Builder
private TalkRoomCreateServiceRequest(String bookIsbn, String content, List<String> readingStatus) {
private TalkRoomCreateServiceRequest(String bookIsbn, String title, String content, List<String> readingStatus) {
this.bookIsbn = bookIsbn;
this.title = title;
this.content = content;
this.readingStatus = readingStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ public class TalkRoomEditServiceRequest {

private Long id;

@Size(min = 1, max = 20, message = "20자 이하로 작성해야 합니다.")
private String title;

@Size(min = 1, max = 2000, message = "2000자 이하로 작성해야 합니다.")
private String content;

@NotEmpty(message = "참가 조건은 1개 이상 체크해야합니다.")
private List<String> readingStatus;

@Builder
private TalkRoomEditServiceRequest(Long id, String content, List<String> readingStatus) {
private TalkRoomEditServiceRequest(Long id, String title, String content, List<String> readingStatus) {
this.id = id;
this.title = title;
this.content = content;
this.readingStatus = readingStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ public class TalkRoomQueryResponse {

private Long talkRoomId;
private String userName;
private String title;
private String content;
private String bookName;
private String bookImage;
private List<TalkRoomQueryReadingStatus> readingStatuses = new ArrayList<>();

@Builder
@QueryProjection
public TalkRoomQueryResponse(Long talkRoomId, String userName, String content, String bookImage) {
public TalkRoomQueryResponse(Long talkRoomId, String userName, String title, String content, String bookName,
String bookImage) {
this.talkRoomId = talkRoomId;
this.userName = userName;
this.title = title;
this.content = content;
this.bookName = bookName;
this.bookImage = bookImage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@
public class TalkRoomResponse {

private String userName;
private String title;
private String bookName;
private String content;
private List<ReadingStatus> readingStatuses;
private String bookImage;

@Builder
@QueryProjection
public TalkRoomResponse(String userName, String content, List<ReadingStatus> readingStatuses, String bookImage) {
public TalkRoomResponse(String userName, String title, String content, String bookName,
List<ReadingStatus> readingStatuses, String bookImage) {
this.userName = userName;
this.title = title;
this.content = content;
this.bookName = bookName;
this.readingStatuses = readingStatuses;
this.bookImage = bookImage;
}

public static TalkRoomResponse of(String userName, TalkRoom talkRoom, List<ReadingStatus> readingStatuses,
String bookImage) {
String bookImage, String bookName) {
return TalkRoomResponse.builder()
.userName(userName)
.title(talkRoom.getTitle())
.content(talkRoom.getContent())
.bookName(bookName)
.readingStatuses(readingStatuses)
.bookImage(bookImage)
.build();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/jisungin/domain/talkroom/TalkRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,32 @@ public class TalkRoom extends BaseEntity {
@JoinColumn(name = "user_id")
private User user;

@Column(name = "talk_room_title", length = 20)
private String title;

@Lob
@Column(name = "talk_room_content", length = 2000)
private String content;

@Builder
private TalkRoom(Book book, User user, String content) {
private TalkRoom(Book book, User user, String title, String content) {
this.book = book;
this.user = user;
this.title = title;
this.content = content;
}

public static TalkRoom create(TalkRoomCreateServiceRequest request, Book book, User user) {
return TalkRoom.builder()
.book(book)
.user(user)
.title(request.getTitle())
.content(request.getContent())
.build();
}

public void edit(TalkRoomEditServiceRequest request) {
this.title = request.getTitle() != null ? request.getTitle() : title;
this.content = request.getContent() != null ? request.getContent() : content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public interface TalkRoomRepository extends JpaRepository<TalkRoom, Long>, TalkR
"select t from TalkRoom t join t.user u where t.id = :talkRoomId"
)
TalkRoom findByIdWithUser(@Param("talkRoomId") Long talkRoomId);

@Query(
"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 @@ -2,9 +2,10 @@

import com.jisungin.application.response.PageResponse;
import com.jisungin.application.talkroom.request.TalkRoomSearchServiceRequest;
import com.jisungin.application.talkroom.response.TalkRoomQueryResponse;

public interface TalkRoomRepositoryCustom {

PageResponse getTalkRooms(TalkRoomSearchServiceRequest search);
PageResponse<TalkRoomQueryResponse> getTalkRooms(TalkRoomSearchServiceRequest search);

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ private List<TalkRoomQueryResponse> findTalkRoom(TalkRoomSearchServiceRequest se
return queryFactory.select(new QTalkRoomQueryResponse(
talkRoom.id.as("talkRoomId"),
user.name.as("userName"),
talkRoom.title,
talkRoom.content,
book.title,
book.url.as("bookImage")
))
.from(talkRoom)
Expand Down
Loading

0 comments on commit 6174567

Please sign in to comment.