-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat : 퀘스트 목표 변경 기능을 구현한다 #25 #59
Merged
+216
−52
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55acdcd
Refact : 스케줄러 리팩토링 (#25)
chaen-ing 4720f49
Refact : 연속출석 날짜 계산 로직 수정 (#25)
chaen-ing 2eba957
Docs : 설명 추가 (#25)
chaen-ing 00f07a6
Feat : 사용자 목표 추가 (#25)
chaen-ing 4638629
Feat : 퀘스트 목표 조회 구현 (#25)
chaen-ing d0ca395
Debug : 주석 변경 (#25)
chaen-ing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.ripple.BE.user.domain; | ||
|
||
import com.ripple.BE.global.entity.BaseEntity; | ||
import com.ripple.BE.user.dto.UserGoalDTO; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Table(name = "user_goals") | ||
@Getter | ||
@Builder | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class UserGoal extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "user_goal_id") | ||
private Long id; | ||
|
||
@OneToOne | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
private int quizGoal; | ||
|
||
private int conceptGoal; | ||
|
||
private int articleGoal; | ||
|
||
public void updateQuizGoal(UserGoalDTO userGoalDTO) { | ||
this.quizGoal = userGoalDTO.quizGoal(); | ||
this.conceptGoal = userGoalDTO.conceptGoal(); | ||
this.articleGoal = userGoalDTO.articleGoal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package com.ripple.BE.user.dto; | ||
|
||
public record QuestDTO(Long conceptProgress, Long quizProgress, Long articleProgress) { | ||
public record QuestDTO(int conceptProgress, int quizProgress, int articleProgress) { | ||
|
||
public static QuestDTO toQuestDTO( | ||
final Long conceptProgress, final Long quizProgress, final Long articleProgress) { | ||
final int conceptProgress, final int quizProgress, final int articleProgress) { | ||
return new QuestDTO(conceptProgress, quizProgress, articleProgress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ripple.BE.user.dto; | ||
|
||
import com.ripple.BE.user.dto.request.UserGoalRequest; | ||
|
||
public record UserGoalDTO(int conceptGoal, int quizGoal, int articleGoal) { | ||
|
||
public static UserGoalDTO toUserGoalDTO(UserGoalRequest userGoalRequest) { | ||
return new UserGoalDTO( | ||
userGoalRequest.conceptGoal(), userGoalRequest.quizGoal(), userGoalRequest.articleGoal()); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ripple/BE/user/dto/request/UserGoalRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ripple.BE.user.dto.request; | ||
|
||
import jakarta.validation.constraints.Min; | ||
|
||
public record UserGoalRequest( | ||
@Min(value = 1, message = "1 이상의 값을 입력해주세요.") int conceptGoal, | ||
@Min(value = 1, message = "1 이상의 값을 입력해주세요.") int quizGoal, | ||
@Min(value = 1, message = "1 이상의 값을 입력해주세요.") int articleGoal) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/ripple/BE/user/dto/response/UserGoalResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ripple.BE.user.dto.response; | ||
|
||
import com.ripple.BE.user.dto.UserGoalDTO; | ||
|
||
public record UserGoalResponse(int conceptGoal, int quizGoal, int articleGoal) { | ||
public static UserGoalResponse toUserGoalResponse(UserGoalDTO userGoalDTO) { | ||
return new UserGoalResponse( | ||
userGoalDTO.conceptGoal(), userGoalDTO.quizGoal(), userGoalDTO.articleGoal()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ripple/BE/user/repository/UserGoalRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ripple.BE.user.repository; | ||
|
||
import com.ripple.BE.user.domain.UserGoal; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserGoalRepository extends JpaRepository<UserGoal, Long> { | ||
Optional<UserGoal> findByUserId(Long userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,14 @@ | |
import com.ripple.BE.user.domain.AttendanceLog; | ||
import com.ripple.BE.user.domain.Quest; | ||
import com.ripple.BE.user.domain.User; | ||
import com.ripple.BE.user.domain.UserGoal; | ||
import com.ripple.BE.user.dto.AttendanceDTO; | ||
import com.ripple.BE.user.dto.QuestDTO; | ||
import com.ripple.BE.user.exception.UserException; | ||
import com.ripple.BE.user.repository.AttendanceLogRepository; | ||
import com.ripple.BE.user.repository.AttendanceRepository; | ||
import com.ripple.BE.user.repository.QuestRepository; | ||
import com.ripple.BE.user.repository.UserGoalRepository; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
@@ -30,43 +32,58 @@ public class AttendanceService { | |
private final QuestRepository questRepository; | ||
private final AttendanceRepository attendanceRepository; | ||
private final AttendanceLogRepository attendanceLogRepository; | ||
private final UserGoalRepository userGoalRepository; | ||
|
||
@Transactional | ||
public Long getCurrentStreak(Long id) { | ||
Attendance attendance = | ||
attendanceRepository | ||
.findByUserId(id) | ||
.orElseThrow(() -> new UserException(ATTENDANCE_NOT_FOUND)); | ||
|
||
if (attendance.getLastAttendedDate() != null | ||
&& attendance.getLastAttendedDate().isBefore(LocalDate.now().minusDays(1))) { | ||
attendance.updateCurrentStreak(1L); | ||
} | ||
|
||
return attendance.getCurrentStreak(); | ||
} | ||
|
||
public QuestDTO getTodayQuest(Long id) { | ||
Quest quest = | ||
questRepository.findByUserId(id).orElseThrow(() -> new UserException(QUEST_NOT_FOUND)); | ||
UserGoal userGoal = | ||
userGoalRepository | ||
.findByUserId(id) | ||
.orElseThrow(() -> new UserException(USER_GOAL_NOT_FOUND)); | ||
|
||
return QuestDTO.toQuestDTO( | ||
quest.isConceptCompleted() ? 100L : 0L, | ||
quest.isQuizCompleted() ? 100L : 0L, | ||
quest.getArticleCompletedCount() / 3 * 100); | ||
Math.min(100, quest.getConceptCompletedCount() * 100 / userGoal.getConceptGoal()), | ||
Math.min(100, quest.getQuizCompletedCount() * 100 / userGoal.getQuizGoal()), | ||
Math.min(100, quest.getArticleCompletedCount() * 100 / userGoal.getArticleGoal())); | ||
} | ||
|
||
@Transactional | ||
public void completeQuest(Long userId, String questType) { | ||
Quest quest = | ||
questRepository.findByUserId(userId).orElseThrow(() -> new UserException(QUEST_NOT_FOUND)); | ||
UserGoal userGoal = | ||
userGoalRepository | ||
.findByUserId(userId) | ||
.orElseThrow(() -> new UserException(USER_GOAL_NOT_FOUND)); | ||
|
||
// 퀘스트 타입에 따라 완료 처리 | ||
switch (questType.toUpperCase()) { | ||
case "QUIZ" -> quest.updateQuizCompleted(); | ||
case "CONCEPT" -> quest.updateConceptCompleted(); | ||
case "QUIZ" -> quest.updateQuizCompletedCount(); | ||
case "CONCEPT" -> quest.updateConceptCompletedCount(); | ||
case "ARTICLE" -> quest.updateArticleCompletedCount(); | ||
default -> throw new UserException(INVALID_QUEST_TYPE); | ||
} | ||
|
||
// 퀘스트 3개 완료 시 출석 완료 처리 | ||
if (quest.getArticleCompletedCount() >= 3 | ||
&& quest.isConceptCompleted() | ||
&& quest.isQuizCompleted()) { | ||
// 퀘스트 완료 시 출석 처리 | ||
if (quest.getArticleCompletedCount() >= userGoal.getArticleGoal() | ||
&& quest.getConceptCompletedCount() >= userGoal.getConceptGoal() | ||
&& quest.getQuizCompletedCount() >= userGoal.getQuizGoal()) { | ||
completeAttendance(userId, LocalDate.now()); | ||
} | ||
} | ||
|
@@ -108,6 +125,8 @@ public void createAttendance(User user) { | |
questRepository.save(Quest.builder().user(user).lastUpdatedDate(today).build()); | ||
attendanceLogRepository.save( | ||
AttendanceLog.builder().date(today).isAttended(false).attendance(attendance).build()); | ||
userGoalRepository.save( | ||
UserGoal.builder().user(user).quizGoal(1).articleGoal(3).conceptGoal(1).build()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이게 사용자 목표 기본 설정값인거가요? |
||
} | ||
|
||
@Transactional | ||
|
@@ -136,4 +155,29 @@ public AttendanceDTO getWeeklyAttendance(Long userId) { | |
.sunday(weeklyAttendance.get(6)) | ||
.build(); | ||
} | ||
|
||
@Transactional | ||
public void resetWeeklyAttendanceLog() { | ||
attendanceLogRepository.deleteAll(); | ||
} | ||
|
||
@Transactional | ||
public void resetAllQuests() { | ||
questRepository.findAll().forEach(Quest::resetQuests); | ||
} | ||
|
||
@Transactional | ||
public void recordAttendanceLog() { | ||
attendanceRepository | ||
.findAll() | ||
.forEach( | ||
attendance -> { | ||
attendanceLogRepository.save( | ||
AttendanceLog.builder() | ||
.attendance(attendance) | ||
.date(LocalDate.now()) | ||
.isAttended(false) | ||
.build()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 세심하네요