Skip to content

Commit

Permalink
[Feature] 게시물 링크 수 증가 처리 시, 조건을 만족하면 사용자를 승급시킨다 (#124)
Browse files Browse the repository at this point in the history
feature: 게시물 링크 수 증가 처리 시, 조건을 만족하면 사용자를 승급시킨다
  • Loading branch information
vectorch9 authored Nov 21, 2023
1 parent 4070d09 commit 364eb2d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/main/java/daybyquest/participant/domain/Participants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import daybyquest.group.domain.GroupUsers;
import daybyquest.quest.domain.Quest;
import daybyquest.quest.domain.Quests;
import daybyquest.user.domain.User;
import daybyquest.user.domain.Users;
import org.springframework.stereotype.Component;

@Component
public class Participants {

public static final int PROMOTE_THRESHOLD = 50;

private static final int MAX_DOING_COUNT = 15;

private static final int MAX_CONTINUE_COUNT = 15;
Expand Down Expand Up @@ -81,4 +84,13 @@ public void validateCountByUserId(final Long userId) {
throw new InvalidDomainException(EXCEED_MAX_QUEST);
}
}

public void increaseLinkedCount(final Long userId, final Long questId) {
final Participant participant = getByUserIdAndQuestId(userId, questId);
participant.increaseLinkedCount();
if (participant.getLinkedCount() == PROMOTE_THRESHOLD) {
final User user = users.getById(userId);
user.promote();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package daybyquest.participant.listener;

import daybyquest.participant.domain.Participant;
import daybyquest.participant.domain.Participants;
import daybyquest.post.domain.SuccessfullyPostLinkedEvent;
import org.springframework.context.event.EventListener;
Expand All @@ -19,8 +18,6 @@ public IncreaseLinkedCountListener(final Participants participants) {
@Transactional
@EventListener
public void listenSuccessfullyPostLinkedEvent(final SuccessfullyPostLinkedEvent event) {
final Participant participant = participants.getByUserIdAndQuestId(event.getUserId(),
event.getQuestId());
participant.increaseLinkedCount();
participants.increaseLinkedCount(event.getUserId(), event.getQuestId());
}
}
17 changes: 13 additions & 4 deletions src/main/java/daybyquest/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import static daybyquest.global.error.ExceptionCode.INVALID_USER_NAME;
import static daybyquest.global.error.ExceptionCode.INVALID_USER_USERNAME;
import static daybyquest.global.error.ExceptionCode.NOT_UPDATABLE_USER;
import static daybyquest.user.domain.UserState.ADMIN;
import static daybyquest.user.domain.UserState.MODERATOR;
import static daybyquest.user.domain.UserState.USER;
import static jakarta.persistence.GenerationType.IDENTITY;

import daybyquest.global.error.exception.InvalidDomainException;
Expand Down Expand Up @@ -76,7 +79,7 @@ public User(String username, String email, String name, Image image) {
this.email = email;
this.name = name;
this.image = image;
this.state = UserState.USER;
this.state = USER;
this.visibility = UserVisibility.PUBLIC;
this.interests = new ArrayList<>();
validate();
Expand Down Expand Up @@ -152,14 +155,20 @@ public void updateImage(Image image) {
}

public boolean isUser() {
return state == UserState.USER || state == UserState.MODERATOR;
return state == USER || state == MODERATOR;
}

public boolean isAdmin() {
return state == UserState.ADMIN;
return state == ADMIN;
}

public boolean isModerator() {
return state == UserState.MODERATOR;
return state == MODERATOR;
}

public void promote() {
if (state == USER) {
this.state = MODERATOR;
}
}
}
46 changes: 46 additions & 0 deletions src/test/java/daybyquest/participant/domain/ParticipantsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static daybyquest.participant.domain.ParticipantState.CONTINUE;
import static daybyquest.participant.domain.ParticipantState.DOING;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static daybyquest.support.fixture.UserFixtures.BOB;
import static daybyquest.support.util.ParticipantUtils.게시물_연결_횟수를_지정한다;
import static daybyquest.user.domain.UserState.MODERATOR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
Expand All @@ -14,6 +17,7 @@
import daybyquest.group.domain.GroupUsers;
import daybyquest.quest.domain.Quest;
import daybyquest.quest.domain.Quests;
import daybyquest.user.domain.User;
import daybyquest.user.domain.Users;
import java.util.Optional;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -228,4 +232,46 @@ public class ParticipantsTest {
assertThatThrownBy(() -> participants.validateCountByUserId(userId))
.isInstanceOf(InvalidDomainException.class);
}

@Test
void 링크된_게시물_수를_증가시킨다() {
// given
final Long userId = 1L;
final Long questId = 2L;
final Quest quest = QUEST_1.일반_퀘스트_생성(questId, null);
QUEST_1.보상_없이_세부사항을_설정한다(quest);
final Participant participant = new Participant(userId, quest);
given(participantRepository.findByUserIdAndQuestId(userId, questId))
.willReturn(Optional.of(participant));

// when
participants.increaseLinkedCount(userId, questId);

// then
assertAll(() -> {
then(participantRepository).should().findByUserIdAndQuestId(userId, questId);
assertThat(participant.getLinkedCount()).isEqualTo(1L);
});
}

@Test
void 링크된_게시물_수가_50개가_되면_사용자를_승급시킨다() {
// given
final Long userId = 1L;
final Long questId = 2L;
final User user = BOB.생성(userId);
final Quest quest = QUEST_1.일반_퀘스트_생성(questId, null);
QUEST_1.보상_없이_세부사항을_설정한다(quest);
final Participant participant = new Participant(userId, quest);
게시물_연결_횟수를_지정한다(participant, 49L);
given(participantRepository.findByUserIdAndQuestId(userId, questId))
.willReturn(Optional.of(participant));
given(users.getById(userId)).willReturn(user);

// when
participants.increaseLinkedCount(userId, questId);

// then
assertThat(user.getState()).isEqualTo(MODERATOR);
}
}

0 comments on commit 364eb2d

Please sign in to comment.