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 all 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 @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -14,9 +15,9 @@ public interface AtlasRepository extends JpaRepository<Atlas, Long> {

@Modifying(clearAutomatically = true)
@Query("delete from Atlas a where a.member.id = :memberId")
void deleteAllByMemberId(Long memberId);
void deleteAllByMemberId(@Param("memberId") Long memberId);

@Modifying(clearAutomatically = true)
@Query("delete from Atlas a where a.topic.id = :topicId")
void deleteAllByTopicId(Long topicId);
void deleteAllByTopicId(@Param("topicId") Long topicId);
}
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;
}

}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public User(
@Override
public boolean canRead(Topic topic) {
TopicStatus topicStatus = topic.getTopicStatus();
return topicStatus.isPublic() || isGroup(topic.getId());

return topicStatus.isPublic() || hasPermission(topic.getId());
}

@Override
public boolean canDelete(Topic topic) {
TopicStatus topicStatus = topic.getTopicStatus();

return topicStatus.isPrivate() && isCreator(topic.getId());
}

Expand All @@ -39,19 +41,36 @@ public boolean canTopicUpdate(Topic topic) {
@Override
public boolean canPinCreateOrUpdate(Topic topic) {
TopicStatus topicStatus = topic.getTopicStatus();

return topicStatus.isAllMembers() || hasPermission(topic.getId());
}

private boolean isCreator(Long topicId) {
return createdTopic.contains(topicId);
@Override
public boolean canPinCommentCreate(Topic topic) {
return canRead(topic);
}

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

private boolean isGroup(Long topicId) {
return isCreator(topicId) || hasPermission(topicId);
@Override
public boolean isUser() {
return true;
}

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

private boolean isCreator(Long topicId) {
return createdTopic.contains(topicId);
}

private boolean hasPermission(Long topicId) {
return createdTopic.contains(topicId) || topicsWithPermission.contains(topicId);
return isCreator(topicId) || topicsWithPermission.contains(topicId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

Expand All @@ -13,10 +14,10 @@ public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

@Modifying(clearAutomatically = true)
@Query("delete from Bookmark b where b.member.id = :memberId")
void deleteAllByMemberId(Long memberId);
void deleteAllByMemberId(@Param("memberId") Long memberId);

@Modifying(clearAutomatically = true)
@Query("delete from Bookmark b where b.topic.id = :topicId")
void deleteAllByTopicId(Long topicId);
void deleteAllByTopicId(@Param("topicId") Long topicId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PermissionRepository extends JpaRepository<Permission, Long> {

Expand All @@ -13,10 +14,10 @@ public interface PermissionRepository extends JpaRepository<Permission, Long> {

@Modifying(clearAutomatically = true)
@Query("delete from Permission p where p.member.id = :memberId")
void deleteAllByMemberId(Long memberId);
void deleteAllByMemberId(@Param("memberId") Long memberId);

@Modifying(clearAutomatically = true)
@Query("delete from Permission p where p.topic.id = :topicId")
void deleteAllByTopicId(Long topicId);
void deleteAllByTopicId(@Param("topicId") Long topicId);

}
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,13 +20,19 @@
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.event.PinUpdateEvent;
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 @@ -45,29 +55,32 @@ public class PinCommandService {
private static final double DUPLICATE_LOCATION_DISTANCE_METERS = 10.0;

private final ApplicationEventPublisher eventPublisher;
private final ImageService imageService;
private final PinRepository pinRepository;
private final LocationRepository locationRepository;
private final TopicRepository topicRepository;
private final MemberRepository memberRepository;
private final PinImageRepository pinImageRepository;
private final ImageService imageService;
private final PinCommentRepository pinCommentRepository;

public PinCommandService(
ApplicationEventPublisher eventPublisher,
ImageService imageService,
PinRepository pinRepository,
LocationRepository locationRepository,
TopicRepository topicRepository,
MemberRepository memberRepository,
PinImageRepository pinImageRepository,
ImageService imageService
PinCommentRepository pinCommentRepository
) {
this.imageService = imageService;
this.eventPublisher = eventPublisher;
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 @@ -202,4 +215,90 @@ 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 pinComment = createPinComment(
pin,
member,
request.parentPinCommentId(),
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 createPinComment(
Pin pin,
Member member,
Long parentPinCommentId,
String content
) {
if (Objects.isNull(parentPinCommentId)) {
return PinComment.ofParentPinComment(pin, member, content);
}

PinComment parentPinComment = findPinComment(parentPinCommentId);

return PinComment.ofChildPinComment(pin, parentPinComment, member, content);
}

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);
}

}
Loading
Loading