From 067bacde8ce1762df4bffb707d94c0fae8ad3361 Mon Sep 17 00:00:00 2001 From: DH CHOI Date: Thu, 2 Nov 2023 13:59:43 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EC=8B=9C=20=ED=80=98=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=EB=A5=BC=20=EA=B2=80=EC=A6=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/daybyquest/participant/domain/Participants.java | 7 +++++++ src/main/java/daybyquest/post/domain/Posts.java | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/daybyquest/participant/domain/Participants.java b/src/main/java/daybyquest/participant/domain/Participants.java index 8e7ced8..e761d0f 100644 --- a/src/main/java/daybyquest/participant/domain/Participants.java +++ b/src/main/java/daybyquest/participant/domain/Participants.java @@ -40,6 +40,13 @@ private void validateNotExistent(final Participant participant) { } } + public void validateExistent(final Long userId, final Long questId) { + if (!participantRepository.existsByUserIdAndQuestId(userId, + questId)) { + throw new InvalidDomainException(NOT_ACCEPTED_QUEST); + } + } + public Participant getByUserIdAndQuestId(final Long userId, final Long questId) { return participantRepository.findByUserIdAndQuestId(userId, questId) .orElseThrow(() -> new InvalidDomainException(NOT_ACCEPTED_QUEST)); diff --git a/src/main/java/daybyquest/post/domain/Posts.java b/src/main/java/daybyquest/post/domain/Posts.java index 9c2cf13..60e5051 100644 --- a/src/main/java/daybyquest/post/domain/Posts.java +++ b/src/main/java/daybyquest/post/domain/Posts.java @@ -1,6 +1,7 @@ package daybyquest.post.domain; import daybyquest.global.error.exception.NotExistPostException; +import daybyquest.participant.domain.Participants; import daybyquest.user.domain.Users; import org.springframework.stereotype.Component; @@ -11,13 +12,19 @@ public class Posts { private final Users users; - Posts(final PostRepository postRepository, final Users users) { + private final Participants participants; + + Posts(final PostRepository postRepository, final Users users, final Participants participants) { this.postRepository = postRepository; this.users = users; + this.participants = participants; } public Long save(final Post post) { users.validateExistentById(post.getUserId()); + if (post.getQuestId() != null) { + participants.validateExistent(post.getUserId(), post.getQuestId()); + } return postRepository.save(post).getId(); }