Skip to content

Commit

Permalink
refactor: 게시물 등록 시 퀘스트 링크를 검증
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Nov 2, 2023
1 parent 7a7271f commit 067bacd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/daybyquest/participant/domain/Participants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/daybyquest/post/domain/Posts.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
}

Expand Down

0 comments on commit 067bacd

Please sign in to comment.