Skip to content

Commit

Permalink
refactor: dto를 interface로 프로젝션하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kong-hana01 committed Mar 13, 2024
1 parent d1bb360 commit 2627eea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CommentResponse comment(final Long roomId, final Long memberId, final Com
Comment comment = new Comment(roomId, request.comment(), memberId);
commentRepository.save(comment);

return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.nickName());
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname());
}

public void validateExistRoom(final Long roomId) {
Expand Down Expand Up @@ -107,6 +107,6 @@ public CommentResponse update(
MemberNickname memberNickname = memberRepository.getMemberNickname(memberId);
Comment comment = commentRepository.getCommentById(commentId);
comment.updateComment(request.comment(), memberId);
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.nickName());
return CommentResponse.of(comment, comment.isOwner(memberId), memberNickname.getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.exception.MemberException.NotFoundException;
import com.digginroom.digginroom.repository.dto.MemberNickname;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MemberRepository extends JpaRepository<Member, Long> {

Expand All @@ -26,10 +20,7 @@ default Member getMemberById(final Long id) {
return findById(id).orElseThrow(NotFoundException::new);
}

@Query("SELECT new com.digginroom.digginroom.repository.dto.MemberNickname(id, nickname.nickname) "
+ "FROM Member "
+ "WHERE id = :id ")
Optional<MemberNickname> findMemberNicknameById(@Param("id") final Long id);
Optional<MemberNickname> findMemberNicknameById(final Long id);

default MemberNickname getMemberNickname(final Long id) {
return findMemberNicknameById(id).orElseThrow(NotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package com.digginroom.digginroom.repository.dto;

public record MemberNickname(Long memberId, String nickName) {
import org.springframework.beans.factory.annotation.Value;

public interface MemberNickname {

@Value("#{target.nickname}")
String getNickname();
}

0 comments on commit 2627eea

Please sign in to comment.