Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] Feat/#587 핀 댓글 기능 추가 구현 #601

Merged
merged 36 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
110e964
feat: 핀 댓글 뼈대 코드 작성
kpeel5839 Oct 16, 2023
f5685c0
refactor: 핀 Comment Exception 수정
kpeel5839 Oct 16, 2023
6fe015d
feat: 현재 유저가 해당 pin 에다가 댓글을 작성할 수 있는지 확인하는 메서드 추가
kpeel5839 Oct 16, 2023
cc717f0
feat: 댓글 생성 API 구현
kpeel5839 Oct 16, 2023
c39f04a
feat: 댓글 조회 API 구현
kpeel5839 Oct 16, 2023
4a7424d
refactor: PinResponseComment 에서 Parent, Child Comment 로 메서드 분리
kpeel5839 Oct 16, 2023
843e577
feat: 핀 댓글 수정, 삭제 API 구현
kpeel5839 Oct 16, 2023
efa640d
refactor: 어드민은 Comment 에 대한 모든 권한을 가지도록 수정
kpeel5839 Oct 16, 2023
04fb540
refactor: @OnDelete 를 추가함으로써 DDL 에 외래키 제약 조건 설정에 on Delete Cascade 집어넣기
kpeel5839 Oct 17, 2023
20dd07a
fix: canChange 여부 로직 수정
kpeel5839 Oct 17, 2023
dd701c0
test: 댓글 조회 통합 테스트 작성
kpeel5839 Oct 17, 2023
7741412
test: 댓글 생성 통합 테스트 작성
kpeel5839 Oct 17, 2023
d160dd2
test: 댓글 수정 통합 테스트 작성
kpeel5839 Oct 17, 2023
48b5c4b
test: 댓글 삭제 통합 테스트 작성
kpeel5839 Oct 17, 2023
8b98dfa
docs: 댓글 생성 문서 작성
kpeel5839 Oct 17, 2023
d6a5816
docs: 댓글 조회 문서 작성
kpeel5839 Oct 17, 2023
4d6cff1
docs: 댓글 수정 및 삭제 문서 작성
kpeel5839 Oct 17, 2023
efc2d0e
test: pinComment 생성, 수정 테스트
kpeel5839 Oct 17, 2023
f4e6416
refactor: 핀 댓글 읽기 권한 필터링 추가
kpeel5839 Oct 17, 2023
8a96d6e
test: 핀 댓글 조회 테스트 추가
kpeel5839 Oct 17, 2023
7b6cf26
test: 핀 댓글 생성 테스트 추가
kpeel5839 Oct 17, 2023
c66802a
test: 핀 수정 테스트 추가
kpeel5839 Oct 17, 2023
166b875
test: 핀 삭제 테스트 추가
kpeel5839 Oct 17, 2023
807d15d
style: 개행 조정
kpeel5839 Oct 17, 2023
26bead6
refactor: 정적 팩토리 메서드로 댓글 or 대댓글 나눔
kpeel5839 Oct 18, 2023
909ebf4
refactor: parent, child comment 분기로 인한 중복코드 제거
kpeel5839 Oct 18, 2023
d2d2fa5
refactor: PinIntegrationTest 개행 수정 및 변수명 수정
kpeel5839 Oct 18, 2023
4245228
refactor: EOF 추가
kpeel5839 Oct 18, 2023
38e35fd
refactor: Rest 하게 URI 변경
kpeel5839 Oct 18, 2023
c179ae0
refactor: PinCommandService Parameter 순서 변경
kpeel5839 Oct 18, 2023
97f5ea7
refactor: isGroup -> hasPermission 으로 변경
kpeel5839 Oct 18, 2023
b30b387
style: 공백 제거
kpeel5839 Oct 18, 2023
cd75da7
refactor: @DisplayName 에 개발 용어 대신 도메인 용어를 사용하도록 수정
kpeel5839 Oct 18, 2023
937a63d
refactor: 핀 대대댓글을 달 수 없도록 수정
kpeel5839 Oct 18, 2023
1232bf7
fix: 핀 댓글 조회 통합테스트 예외발생하는 문제 수정
kpeel5839 Oct 18, 2023
fb43e32
fix: git 충돌 해결
kpeel5839 Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/src/docs/asciidoc/pin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,23 @@ operation::pin-controller-test/add-image[snippets='http-request,http-response']
=== 핀 이미지 삭제

operation::pin-controller-test/remove-image[snippets='http-request,http-response']

=== 핀 댓글 생성

operation::pin-controller-test/create-parent-pin-comment[snippets='http-request,http-response']

=== 핀 대댓글 생성

operation::pin-controller-test/create-child-pin-comment[snippets='http-request,http-response']

=== 핀 댓글 조회

operation::pin-controller-test/find-all-pin-comment-by-pin-id[snippets='http-request,http-response']

=== 핀 댓글 수정

operation::pin-controller-test/update-pin-comment[snippets='http-request,http-response']

=== 핀 댓글 삭제

operation::pin-controller-test/remove-pin-comment[snippets='http-request,http-response']
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protected AuthMember(
public abstract boolean canTopicUpdate(Topic topic);

public abstract boolean canPinCreateOrUpdate(Topic topic);

public abstract boolean canPinCommentCreate(Topic topic);

public Long getMemberId() {
return memberId;
Expand All @@ -36,4 +38,10 @@ public boolean isSameMember(Long memberId) {
return Objects.equals(memberId, this.memberId);
}

public abstract boolean isAdmin();

public abstract boolean isUser();

public abstract boolean isGuest();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,24 @@ public boolean canPinCreateOrUpdate(Topic topic) {
return true;
}

}
@Override
public boolean canPinCommentCreate(Topic topic) {
return true;
}

@Override
public boolean isAdmin() {
return true;
}

@Override
public boolean isUser() {
return false;
}

@Override
public boolean isGuest() {
return false;
}

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 덕분에 용어 알아갑니다

Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ public boolean canPinCreateOrUpdate(Topic topic) {
return false;
}

}
@Override
public boolean canPinCommentCreate(Topic topic) {
return false;
}

@Override
public boolean isAdmin() {
return false;
}

@Override
public boolean isUser() {
return false;
}

@Override
public boolean isGuest() {
return true;
}

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POSIX!

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbefine.mapbefine.auth.domain.member;

import com.mapbefine.mapbefine.auth.domain.AuthMember;
import com.mapbefine.mapbefine.topic.domain.Publicity;
import com.mapbefine.mapbefine.topic.domain.Topic;
import com.mapbefine.mapbefine.topic.domain.TopicStatus;
import java.util.List;
Expand Down Expand Up @@ -42,6 +43,28 @@ public boolean canPinCreateOrUpdate(Topic topic) {
return topicStatus.isAllMembers() || hasPermission(topic.getId());
}

@Override
public boolean canPinCommentCreate(Topic topic) {
Publicity publicity = topic.getPublicity();

return publicity == Publicity.PUBLIC || isGroup(topic.getId());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P4. 이건 이번 PR 해당하는 내용만은 아닌데 제가 코드 다시 보니까 이해가 안가서 질문드립니당..
isGroup 이랑 hasPermission이랑 어떻게 다른건가요..???
createdTopic.contains(topicId)랑 isCreator(topicId)랑 같은거 아닌가염?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 다시 보니까 그렇네요 허허허허허

publicity == Publicity.PUBLIC -> publicity.isPublic() 으로 바꾸는 것과

도이가 말씀해주신 부분들 수정해서 올리도록 할게요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isGroup -> hasPermission 일괄적으로 적용했습니다.

}

@Override
public boolean isAdmin() {
return false;
}

@Override
public boolean isUser() {
return true;
}

@Override
public boolean isGuest() {
return false;
}

private boolean isCreator(Long topicId) {
return createdTopic.contains(topicId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.mapbefine.mapbefine.pin.application;

import static com.mapbefine.mapbefine.image.exception.ImageErrorCode.IMAGE_FILE_IS_NULL;
import static com.mapbefine.mapbefine.pin.exception.PinCommentErrorCode.FORBIDDEN_PIN_COMMENT_CREATE;
import static com.mapbefine.mapbefine.pin.exception.PinCommentErrorCode.FORBIDDEN_PIN_COMMENT_DELETE;
import static com.mapbefine.mapbefine.pin.exception.PinCommentErrorCode.FORBIDDEN_PIN_COMMENT_UPDATE;
import static com.mapbefine.mapbefine.pin.exception.PinCommentErrorCode.PIN_COMMENT_NOT_FOUND;
import static com.mapbefine.mapbefine.pin.exception.PinErrorCode.FORBIDDEN_PIN_CREATE_OR_UPDATE;
import static com.mapbefine.mapbefine.pin.exception.PinErrorCode.ILLEGAL_PIN_ID;
import static com.mapbefine.mapbefine.pin.exception.PinErrorCode.ILLEGAL_PIN_IMAGE_ID;
Expand All @@ -16,12 +20,18 @@
import com.mapbefine.mapbefine.member.domain.Member;
import com.mapbefine.mapbefine.member.domain.MemberRepository;
import com.mapbefine.mapbefine.pin.domain.Pin;
import com.mapbefine.mapbefine.pin.domain.PinComment;
import com.mapbefine.mapbefine.pin.domain.PinCommentRepository;
import com.mapbefine.mapbefine.pin.domain.PinImage;
import com.mapbefine.mapbefine.pin.domain.PinImageRepository;
import com.mapbefine.mapbefine.pin.domain.PinRepository;
import com.mapbefine.mapbefine.pin.dto.request.PinCommentCreateRequest;
import com.mapbefine.mapbefine.pin.dto.request.PinCommentUpdateRequest;
import com.mapbefine.mapbefine.pin.dto.request.PinCreateRequest;
import com.mapbefine.mapbefine.pin.dto.request.PinImageCreateRequest;
import com.mapbefine.mapbefine.pin.dto.request.PinUpdateRequest;
import com.mapbefine.mapbefine.pin.exception.PinCommentException.PinCommentForbiddenException;
import com.mapbefine.mapbefine.pin.exception.PinCommentException.PinCommentNotFoundException;
import com.mapbefine.mapbefine.pin.exception.PinException.PinBadRequestException;
import com.mapbefine.mapbefine.pin.exception.PinException.PinForbiddenException;
import com.mapbefine.mapbefine.topic.domain.Topic;
Expand All @@ -46,21 +56,24 @@ public class PinCommandService {
private final MemberRepository memberRepository;
private final PinImageRepository pinImageRepository;
private final ImageService imageService;
private final PinCommentRepository pinCommentRepository;

public PinCommandService(
PinRepository pinRepository,
LocationRepository locationRepository,
TopicRepository topicRepository,
MemberRepository memberRepository,
PinImageRepository pinImageRepository,
ImageService imageService
ImageService imageService,
PinCommentRepository pinCommentRepository
) {
this.pinRepository = pinRepository;
this.locationRepository = locationRepository;
this.topicRepository = topicRepository;
this.memberRepository = memberRepository;
this.pinImageRepository = pinImageRepository;
this.imageService = imageService;
this.pinCommentRepository = pinCommentRepository;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. 필드 순서, 생성자 파라미터 순서에서 Service와 Repository를 분류하면 어떨까요?
Repository가 다음에 추가될 확률이 더 높으니 ImageService를 첫번째 파라미터, 필드로 올려도 좋을 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영하겠습니다~!


public long save(
Expand Down Expand Up @@ -193,4 +206,81 @@ private void validatePinCreateOrUpdate(AuthMember authMember, Topic topic) {

throw new PinForbiddenException(FORBIDDEN_PIN_CREATE_OR_UPDATE);
}

public Long savePinComment(AuthMember authMember, PinCommentCreateRequest request) {
Pin pin = findPin(request.pinId());
validatePinCommentCreate(authMember, pin);
Member member = findMember(authMember.getMemberId());
PinComment parentPinComment = findParentPinComment(request.parentPinCommentId());

PinComment pinComment = PinComment.of(pin, parentPinComment, member, request.content());
pinCommentRepository.save(pinComment);

return pinComment.getId();
}

private void validatePinCommentCreate(AuthMember authMember, Pin pin) {
if (authMember.canPinCommentCreate(pin.getTopic())) {
return;
}

throw new PinCommentForbiddenException(FORBIDDEN_PIN_COMMENT_CREATE);
}

private PinComment findParentPinComment(Long parentPinComment) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Id !!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

허걱 이런 실수를

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영 완!

if (Objects.isNull(parentPinComment)) {
return null;
}

return pinCommentRepository.findById(parentPinComment)
.orElseThrow(() -> new PinCommentNotFoundException(PIN_COMMENT_NOT_FOUND, parentPinComment));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null값을 그대로 넘겨주기보단, 상황에 따라 다르게 초기화된 pinComment를 넘겨주는건 어떤가요 ? 생성자에 null넣기 좀 그러면, 정팩메로 분리해도 되구요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영해보겠슴둥~~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영을 완료하긴 했지만 쥬니가 말씀하신 방법이 이게 맞는지는 모르겠어요! 한번 확인 부탁드려용!


public void updatePinComment(
AuthMember member,
Long pinCommentId,
PinCommentUpdateRequest request
) {
PinComment pinComment = findPinComment(pinCommentId);

validatePinCommentUpdate(member, pinComment);

pinComment.updateContent(request.content());
}

private void validatePinCommentUpdate(AuthMember authMember, PinComment pinComment) {
if (isPinCommentCreatorOrAdmin(authMember, pinComment)) {
return;
}

throw new PinCommentForbiddenException(FORBIDDEN_PIN_COMMENT_UPDATE);
}

private boolean isPinCommentCreatorOrAdmin(AuthMember authMember, PinComment pinComment) {
Long creatorId = pinComment.getCreator().getId();

return authMember.isSameMember(creatorId) || authMember.isAdmin();
}

private PinComment findPinComment(Long pinCommentId) {
return pinCommentRepository.findById(pinCommentId)
.orElseThrow(() -> new PinCommentNotFoundException(PIN_COMMENT_NOT_FOUND, pinCommentId));
}

public void deletePinComment(AuthMember member, Long pinCommentId) {
PinComment pinComment = findPinComment(pinCommentId);

validatePinCommentDelete(member, pinComment);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidatePinCommentUpdate를 재사용하면 안 되는건가요 ?

Copy link
Collaborator Author

@kpeel5839 kpeel5839 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외 내에 들어가는 Error Code가 달라서 이렇게 하긴 했어요! Error Code를 받아서 처리하는 방식으로 할까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

근데 이거와 별개로 생각해보니까 canPinCommentCreate 를 기존 코드의 canRead 로 바꿀 수 있더라고요 허허허 이것도 같이 바꿀게용

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니네 허허허 canRead 로 하면 Guest 를 못거르네 허허허

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니네 거르네 허허허

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니네 안거르네


pinCommentRepository.delete(pinComment);
}

private void validatePinCommentDelete(AuthMember authMember, PinComment pinComment) {
if (isPinCommentCreatorOrAdmin(authMember, pinComment)) {
return;
}

throw new PinCommentForbiddenException(FORBIDDEN_PIN_COMMENT_DELETE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

import com.mapbefine.mapbefine.auth.domain.AuthMember;
import com.mapbefine.mapbefine.pin.domain.Pin;
import com.mapbefine.mapbefine.pin.domain.PinComment;
import com.mapbefine.mapbefine.pin.domain.PinCommentRepository;
import com.mapbefine.mapbefine.pin.domain.PinRepository;
import com.mapbefine.mapbefine.pin.dto.response.PinCommentResponse;
import com.mapbefine.mapbefine.pin.dto.response.PinDetailResponse;
import com.mapbefine.mapbefine.pin.dto.response.PinResponse;
import com.mapbefine.mapbefine.pin.exception.PinException.PinForbiddenException;
import com.mapbefine.mapbefine.pin.exception.PinException.PinNotFoundException;
import com.mapbefine.mapbefine.topic.domain.Topic;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,8 +24,10 @@
public class PinQueryService {

private final PinRepository pinRepository;
private final PinCommentRepository pinCommentRepository;

public PinQueryService(PinRepository pinRepository) {
public PinQueryService(PinRepository pinRepository, PinCommentRepository pinCommentRepository) {
this.pinCommentRepository = pinCommentRepository;
this.pinRepository = pinRepository;
}

Expand Down Expand Up @@ -56,4 +62,35 @@ public List<PinResponse> findAllPinsByMemberId(AuthMember authMember, Long membe
.map(PinResponse::from)
.toList();
}

public List<PinCommentResponse> findAllPinCommentByPinId(AuthMember member, Long pinId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAllPinComment's'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

껄껄껄껄

Pin pin = findPin(pinId);
validateReadAuth(member, pin.getTopic());

List<PinComment> pinComments = pinCommentRepository.findAllByPinId(pinId);

return pinComments.stream()
.map(pinComment -> pinCommentToResponse(member, pinComment))
.toList();
}

private Pin findPin(Long pinId) {
return pinRepository.findById(pinId)
.orElseThrow(() -> new PinNotFoundException(PIN_NOT_FOUND, pinId));
}

private PinCommentResponse pinCommentToResponse(AuthMember member, PinComment pinComment) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 좀 별론데

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋ 어떻게 할 수 있을까요??

Long creatorId = pinComment.getCreator().getId();

boolean canChange = (Objects.nonNull(member.getMemberId())
&& member.isSameMember(creatorId))
|| member.isAdmin();

if (pinComment.isParentComment()) {
return PinCommentResponse.ofParentComment(pinComment, canChange);
}

return PinCommentResponse.ofChildComment(pinComment, canChange);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class Pin extends BaseTimeEntity {
private Topic topic;

@ManyToOne
@JoinColumn(name = "member_id")
@JoinColumn(name = "member_id", nullable = false)
private Member creator;

@Column(nullable = false)
Expand Down
Loading
Loading