Skip to content

Commit

Permalink
feat: 댓글방 생성 시점 변경 (#319)
Browse files Browse the repository at this point in the history
* feat: 댓글방 생성 시점 변경

* refactor: 불필요한 도메인 OfferingWithRole 제거

* refactor: 불필요한 도메인 CommentWithRole 제거

* refactor: 댓글의 작성자 확인 메서드 추가

* refactor: 댓글방 목록 조회 dto 생성자 추가
  • Loading branch information
helenason authored and ChooSeoyeon committed Oct 11, 2024
1 parent 2216607 commit cdd14c3
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 93 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package com.zzang.chongdae.comment.repository;

import com.zzang.chongdae.comment.domain.CommentWithRole;
import com.zzang.chongdae.comment.repository.entity.CommentEntity;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CommentRepository extends JpaRepository<CommentEntity, Long> {

@Query("""
SELECT new com.zzang.chongdae.comment.domain.CommentWithRole(c, om.role)
FROM CommentEntity as c JOIN OfferingMemberEntity as om
ON c.offering = om.offering AND c.member = om.member
WHERE om.offering = :offering
ORDER BY c.createdAt
""")
List<CommentWithRole> findAllWithRoleByOffering(OfferingEntity offering);
List<CommentEntity> findAllByOfferingOrderByCreatedAt(OfferingEntity offering);

Optional<CommentEntity> findTopByOfferingOrderByCreatedAtDesc(OfferingEntity offering);

Boolean existsByOffering(OfferingEntity offering);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public class CommentEntity extends BaseTimeEntity {
public CommentEntity(MemberEntity member, OfferingEntity offering, String content) {
this(null, member, offering, content);
}

public boolean isOwnedBy(MemberEntity other) {
return this.member.isSame(other);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.zzang.chongdae.comment.service;

import com.zzang.chongdae.comment.domain.CommentWithRole;
import com.zzang.chongdae.comment.repository.CommentRepository;
import com.zzang.chongdae.comment.repository.entity.CommentEntity;
import com.zzang.chongdae.comment.service.dto.CommentAllResponse;
import com.zzang.chongdae.comment.service.dto.CommentAllResponseItem;
import com.zzang.chongdae.comment.service.dto.CommentCreatedAtResponse;
import com.zzang.chongdae.comment.service.dto.CommentLatestResponse;
import com.zzang.chongdae.comment.service.dto.CommentRoomAllResponse;
import com.zzang.chongdae.comment.service.dto.CommentRoomAllResponseItem;
import com.zzang.chongdae.comment.service.dto.CommentSaveRequest;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.repository.MemberRepository;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.OfferingWithRole;
import com.zzang.chongdae.offering.exception.OfferingErrorCode;
import com.zzang.chongdae.offering.repository.OfferingRepository;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import com.zzang.chongdae.offeringmember.domain.OfferingMemberRole;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +23,6 @@
public class CommentService {

private final CommentRepository commentRepository;
private final MemberRepository memberRepository;
private final OfferingRepository offeringRepository;

public Long saveComment(CommentSaveRequest request, MemberEntity member) {
Expand All @@ -41,52 +35,29 @@ public Long saveComment(CommentSaveRequest request, MemberEntity member) {
}

public CommentRoomAllResponse getAllCommentRoom(MemberEntity member) {
List<OfferingWithRole> offeringsWithRole = offeringRepository.findAllWithRoleByMember(member);
List<CommentRoomAllResponseItem> responseItems = offeringsWithRole.stream()
.filter(offeringWithRole -> offeringWithRole.getOffering().hasParticipant())
.map(this::toCommentRoomAllResponseItem)
List<OfferingEntity> commentRooms = offeringRepository.findCommentRoomsByMember(member);
List<CommentRoomAllResponseItem> responseItems = commentRooms.stream()
.map(commentsRoom -> toCommentRoomAllResponseItem(commentsRoom, member))
.toList();
return new CommentRoomAllResponse(responseItems);
}

private CommentRoomAllResponseItem toCommentRoomAllResponseItem(OfferingWithRole offeringWithRole) {
OfferingEntity offering = offeringWithRole.getOffering();
OfferingMemberRole role = offeringWithRole.getRole();
return new CommentRoomAllResponseItem(
offering.getId(),
offering.getTitle(),
toCommentLatestResponse(offering),
role.isProposer());
}

private CommentLatestResponse toCommentLatestResponse(OfferingEntity offering) {
Optional<CommentEntity> rawComment = commentRepository.findTopByOfferingOrderByCreatedAtDesc(offering);
return rawComment
private CommentRoomAllResponseItem toCommentRoomAllResponseItem(OfferingEntity offering, MemberEntity member) {
Optional<CommentEntity> comment = commentRepository.findTopByOfferingOrderByCreatedAtDesc(offering);
CommentLatestResponse commentLatestResponse = comment
.map(CommentLatestResponse::new)
.orElseGet(() -> new CommentLatestResponse(null, null));
return new CommentRoomAllResponseItem(offering, member, commentLatestResponse);
}

public CommentAllResponse getAllComment(Long offeringId, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(offeringId)
.orElseThrow(() -> new MarketException(OfferingErrorCode.NOT_FOUND));

List<CommentWithRole> commentsWithRole = commentRepository.findAllWithRoleByOffering(offering);
List<CommentAllResponseItem> responseItems = commentsWithRole.stream()
.map(commentWithRole -> toCommentAllResponseItem(commentWithRole, member.getId()))
List<CommentEntity> comments = commentRepository.findAllByOfferingOrderByCreatedAt(offering);
List<CommentAllResponseItem> responseItems = comments.stream()
.map(comment -> new CommentAllResponseItem(comment, member))
.toList();
return new CommentAllResponse(responseItems);
}

private CommentAllResponseItem toCommentAllResponseItem(CommentWithRole commentWithRole, long loginMemberId) {
CommentEntity comment = commentWithRole.getComment();
OfferingMemberRole role = commentWithRole.getRole();
MemberEntity member = comment.getMember();
return new CommentAllResponseItem(
comment.getId(),
new CommentCreatedAtResponse(comment.getCreatedAt()),
comment.getContent(),
member.getNickname(),
role.isProposer(),
member.isSameMember(loginMemberId));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.zzang.chongdae.comment.service.dto;

import com.zzang.chongdae.comment.repository.entity.CommentEntity;
import com.zzang.chongdae.member.repository.entity.MemberEntity;

public record CommentAllResponseItem(Long commentId,
CommentCreatedAtResponse createdAt,
String content,
String nickname,
Boolean isProposer,
Boolean isMine) {
boolean isProposer,
boolean isMine) {

public CommentAllResponseItem(CommentEntity comment, MemberEntity member) {
this(comment.getId(),
new CommentCreatedAtResponse(comment.getCreatedAt()),
comment.getContent(),
comment.getMember().getNickname(),
comment.getOffering().isProposedBy(comment.getMember()),
comment.isOwnedBy(member));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.zzang.chongdae.comment.service.dto;

import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;

public record CommentRoomAllResponseItem(Long offeringId,
String offeringTitle,
CommentLatestResponse latestComment,
Boolean isProposer) {
Boolean isProposer,
CommentLatestResponse latestComment) {

public CommentRoomAllResponseItem(OfferingEntity offering,
MemberEntity member,
CommentLatestResponse latestComment) {
this(offering.getId(),
offering.getTitle(),
offering.isProposedBy(member),
latestComment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MemberEntity(String nickname, String password) {
this(null, nickname, password);
}

public boolean isSameMember(Long memberId) {
return this.id.equals(memberId);
public boolean isSame(MemberEntity other) {
return this.equals(other);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.zzang.chongdae.offering.repository;

import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.OfferingWithRole;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -12,12 +11,12 @@
public interface OfferingRepository extends JpaRepository<OfferingEntity, Long> {

@Query("""
SELECT new com.zzang.chongdae.offering.domain.OfferingWithRole(o, om.role)
SELECT o
FROM OfferingEntity as o JOIN OfferingMemberEntity as om
ON o.id = om.offering.id
WHERE om.member = :member
""")
List<OfferingWithRole> findAllWithRoleByMember(MemberEntity member);
List<OfferingEntity> findCommentRoomsByMember(MemberEntity member);

@Query("""
SELECT o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ public OfferingMeeting toOfferingMeeting() {
return new OfferingMeeting(deadline, meetingAddress, meetingAddressDetail, meetingAddressDong);
}

public boolean hasParticipant() {
return currentCount > INITIAL_COUNT;
}

public boolean isStatusGrouping() {
return this.roomStatus.isGrouping();
}

public boolean isProposedBy(MemberEntity other) {
return this.member.isSame(other);
}

public boolean isNotProposedBy(MemberEntity other) {
return !this.member.equals(other);
return !isProposedBy(other);
}

public void updateMeeting(OfferingMeeting offeringMeeting) {
Expand Down

0 comments on commit cdd14c3

Please sign in to comment.