Skip to content

Commit

Permalink
[Feature] 퀘스트 뱃지 보상을 구현한다 (#71)
Browse files Browse the repository at this point in the history
* feature: 뱃지 보상 받기 구현

* refactor: 게시물 등록 시 퀘스트 링크를 검증
  • Loading branch information
vectorch9 authored Nov 2, 2023
1 parent 0722bb5 commit 9571b59
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/main/java/daybyquest/badge/domain/BadgeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

interface BadgeRepository extends Repository<Badge, Long> {

Badge save(Badge badge);
Badge save(final Badge badge);

Optional<Badge> findById(Long id);
Optional<Badge> findById(final Long id);

boolean existsById(final Long id);
}
11 changes: 11 additions & 0 deletions src/main/java/daybyquest/badge/domain/Badges.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package daybyquest.badge.domain;

import daybyquest.global.error.exception.NotExistBadgeException;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/main/java/daybyquest/badge/domain/OwningRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.util.Optional;
import org.springframework.data.repository.Repository;

public interface OwningRepository extends Repository<Owning, OwningId> {
interface OwningRepository extends Repository<Owning, OwningId> {

Owning save(Owning owning);
Owning save(final Owning owning);

Optional<Owning> findByUserId(Long userId);
Optional<Owning> findByUserId(final Long userId);

}
24 changes: 24 additions & 0 deletions src/main/java/daybyquest/badge/domain/Ownings.java
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/daybyquest/participant/domain/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
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
9 changes: 8 additions & 1 deletion src/main/java/daybyquest/quest/domain/Quests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package daybyquest.quest.domain;

import daybyquest.badge.domain.Badges;
import daybyquest.global.error.exception.NotExistQuestException;
import org.springframework.stereotype.Component;

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

Expand Down

0 comments on commit 9571b59

Please sign in to comment.