diff --git a/src/main/java/daybyquest/badge/domain/BadgeRepository.java b/src/main/java/daybyquest/badge/domain/BadgeRepository.java index 2fc88dd..8517a13 100644 --- a/src/main/java/daybyquest/badge/domain/BadgeRepository.java +++ b/src/main/java/daybyquest/badge/domain/BadgeRepository.java @@ -5,7 +5,9 @@ interface BadgeRepository extends Repository { - Badge save(Badge badge); + Badge save(final Badge badge); - Optional findById(Long id); + Optional findById(final Long id); + + boolean existsById(final Long id); } diff --git a/src/main/java/daybyquest/badge/domain/Badges.java b/src/main/java/daybyquest/badge/domain/Badges.java index 2b8af46..9487a35 100644 --- a/src/main/java/daybyquest/badge/domain/Badges.java +++ b/src/main/java/daybyquest/badge/domain/Badges.java @@ -1,5 +1,6 @@ package daybyquest.badge.domain; +import daybyquest.global.error.exception.NotExistBadgeException; import org.springframework.stereotype.Component; @Component @@ -14,4 +15,14 @@ public class Badges { public void save(final Badge badge) { badgeRepository.save(badge); } + + public void validateExistentById(final Long id) { + if (!badgeRepository.existsById(id)) { + throw new NotExistBadgeException(); + } + } + + public Badge getById(final Long id) { + return badgeRepository.findById(id).orElseThrow(NotExistBadgeException::new); + } } diff --git a/src/main/java/daybyquest/badge/domain/OwningRepository.java b/src/main/java/daybyquest/badge/domain/OwningRepository.java index 350a8cf..53625ab 100644 --- a/src/main/java/daybyquest/badge/domain/OwningRepository.java +++ b/src/main/java/daybyquest/badge/domain/OwningRepository.java @@ -3,10 +3,10 @@ import java.util.Optional; import org.springframework.data.repository.Repository; -public interface OwningRepository extends Repository { +interface OwningRepository extends Repository { - Owning save(Owning owning); + Owning save(final Owning owning); - Optional findByUserId(Long userId); + Optional findByUserId(final Long userId); } \ No newline at end of file diff --git a/src/main/java/daybyquest/badge/domain/Ownings.java b/src/main/java/daybyquest/badge/domain/Ownings.java new file mode 100644 index 0000000..08778b2 --- /dev/null +++ b/src/main/java/daybyquest/badge/domain/Ownings.java @@ -0,0 +1,24 @@ +package daybyquest.badge.domain; + +import org.springframework.stereotype.Component; + +@Component +public class Ownings { + + private final OwningRepository owningRepository; + + private final Badges badges; + + Ownings(final OwningRepository owningRepository, final Badges badges) { + this.owningRepository = owningRepository; + this.badges = badges; + } + + public void save(final Owning owning) { + owningRepository.save(owning); + } + + public void saveByUserIdAndBadgeId(final Long userId, final Long badgeId) { + save(new Owning(userId, badges.getById(badgeId))); + } +} diff --git a/src/main/java/daybyquest/global/error/exception/NotExistBadgeException.java b/src/main/java/daybyquest/global/error/exception/NotExistBadgeException.java new file mode 100644 index 0000000..932f41f --- /dev/null +++ b/src/main/java/daybyquest/global/error/exception/NotExistBadgeException.java @@ -0,0 +1,10 @@ +package daybyquest.global.error.exception; + +import daybyquest.global.error.ExceptionCode; + +public class NotExistBadgeException extends CustomException { + + public NotExistBadgeException() { + super(ExceptionCode.NOT_EXIST_BADGE); + } +} diff --git a/src/main/java/daybyquest/participant/application/TakeRewardService.java b/src/main/java/daybyquest/participant/application/TakeRewardService.java index e9e6354..577d6a1 100644 --- a/src/main/java/daybyquest/participant/application/TakeRewardService.java +++ b/src/main/java/daybyquest/participant/application/TakeRewardService.java @@ -1,5 +1,6 @@ package daybyquest.participant.application; +import daybyquest.badge.domain.Ownings; import daybyquest.participant.domain.Participant; import daybyquest.participant.domain.Participants; import org.springframework.stereotype.Service; @@ -10,13 +11,19 @@ public class TakeRewardService { private final Participants participants; - public TakeRewardService(final Participants participants) { + private final Ownings ownings; + + public TakeRewardService(final Participants participants, final Ownings ownings) { this.participants = participants; + this.ownings = ownings; } @Transactional public void invoke(final Long loginId, final Long questId) { final Participant participant = participants.getByUserIdAndQuestId(loginId, questId); - participant.takeReward(); + final Long badgeId = participant.takeReward(); + if (badgeId != null) { + ownings.saveByUserIdAndBadgeId(loginId, badgeId); + } } } diff --git a/src/main/java/daybyquest/participant/domain/Participant.java b/src/main/java/daybyquest/participant/domain/Participant.java index c9150be..e1c8d49 100644 --- a/src/main/java/daybyquest/participant/domain/Participant.java +++ b/src/main/java/daybyquest/participant/domain/Participant.java @@ -50,10 +50,11 @@ public Long getQuestId() { return quest.getId(); } - public void takeReward() { + public Long takeReward() { validateDidNotTakeReward(); validateRewardCount(); state = FINISHED; + return quest.getBadgeId(); } private void validateDidNotTakeReward() { 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(); } diff --git a/src/main/java/daybyquest/quest/domain/Quests.java b/src/main/java/daybyquest/quest/domain/Quests.java index a23eff2..2f3ebe1 100644 --- a/src/main/java/daybyquest/quest/domain/Quests.java +++ b/src/main/java/daybyquest/quest/domain/Quests.java @@ -1,5 +1,6 @@ package daybyquest.quest.domain; +import daybyquest.badge.domain.Badges; import daybyquest.global.error.exception.NotExistQuestException; import org.springframework.stereotype.Component; @@ -8,11 +9,17 @@ public class Quests { private final QuestRepository questRepository; - Quests(final QuestRepository questRepository) { + private final Badges badges; + + Quests(final QuestRepository questRepository, final Badges badges) { this.questRepository = questRepository; + this.badges = badges; } public Long save(final Quest quest) { + if (quest.getBadgeId() != null) { + badges.validateExistentById(quest.getBadgeId()); + } return questRepository.save(quest).getId(); }