Skip to content

Commit

Permalink
Merge pull request #20 from GDG-Hackathon-77ia/feat/attendence
Browse files Browse the repository at this point in the history
Feat: add attendence type per consecutiveDays util 5 and add diary writing point logic
  • Loading branch information
GitJIHO authored Nov 13, 2024
2 parents 283ff6e + faa9064 commit 287c2cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/gdg/kkia/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.gdg.kkia.diary.repository.DiaryRepository;
import com.gdg.kkia.member.entity.Member;
import com.gdg.kkia.member.repository.MemberRepository;
import com.gdg.kkia.point.service.PointLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,6 +23,7 @@ public class DiaryService {

private final DiaryRepository diaryRepository;
private final MemberRepository memberRepository;
private final PointLogService pointLogService;

@Transactional
public void writeDiary(Long memberId, List<DiaryWriteRequest> diaryWriteRequests) {
Expand All @@ -32,6 +34,8 @@ public void writeDiary(Long memberId, List<DiaryWriteRequest> diaryWriteRequests
Diary diary = new Diary(diaryWriteRequest.type(), diaryWriteRequest.content(), member);
diaryRepository.save(diary);
}

pointLogService.earnDiaryWritePointPerDay(member);
}

@Transactional(readOnly = true)
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/gdg/kkia/point/entity/PointLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ public void convertToReceivedDate() {
}

public enum Type {
ATTENDANCE,
DAILYRESPONSE,
ATTENDANCE_DAY_1,
ATTENDANCE_DAY_2,
ATTENDANCE_DAY_3,
ATTENDANCE_DAY_4,
ATTENDANCE_DAY_5_OR_MORE,
DAILY_RESPONSE,
DIARY,
PET_GROWTH

PET_GROWTH,
CHAT_BOT
}

public enum Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface PointLogRepository extends JpaRepository<PointLog, Long> {

List<PointLog> findByMemberOrderByReceivedDateDesc(Member member);

boolean existsByReceivedDateAndMemberAndType(LocalDate receivedDate, Member member, PointLog.Type type);
boolean existsByReceivedDateAndMemberAndTypeIn(LocalDate receivedDate, Member member, List<PointLog.Type> type);

}
45 changes: 39 additions & 6 deletions src/main/java/com/gdg/kkia/point/service/PointLogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -27,7 +28,15 @@ public class PointLogService {
private final static int ATTENDANCE_BONUS_4_DAYS = 150;
private final static int ATTENDANCE_BONUS_5_DAYS = 200;
private final static int DIARY_WRITE_POINT = 300;
private final static int USING_CHAT_BOT_POINT = 200;
private final static int DAILY_QUESTION_ANSWER_POINT = 200;
private final static List<PointLog.Type> ATTENDANCE_LISTS = Arrays.asList(
PointLog.Type.ATTENDANCE_DAY_1,
PointLog.Type.ATTENDANCE_DAY_2,
PointLog.Type.ATTENDANCE_DAY_3,
PointLog.Type.ATTENDANCE_DAY_4,
PointLog.Type.ATTENDANCE_DAY_5_OR_MORE
);


private final PointLogRepository pointLogRepository;
Expand Down Expand Up @@ -60,12 +69,14 @@ public PointResponse getMemberPoint(Long memberId) {
public void earnAttendancePointPerDay(Member member) {
LocalDate today = LocalDate.now();

if (!pointLogRepository.existsByReceivedDateAndMemberAndType(today, member, PointLog.Type.ATTENDANCE)) {
if (!pointLogRepository.existsByReceivedDateAndMemberAndTypeIn(today, member, ATTENDANCE_LISTS)) {
int consecutiveDays = calculateConsecutiveAttendanceDays(member);

int totalPoints = ATTENDANCE_BASE_POINT + calculateBonusPoints(consecutiveDays);

PointLog newPointLog = new PointLog(PointLog.Type.ATTENDANCE, PointLog.Status.EARNED, member, totalPoints);
PointLog.Type attendanceType = getAttendanceType(consecutiveDays);

PointLog newPointLog = new PointLog(attendanceType, PointLog.Status.EARNED, member, totalPoints);
pointLogRepository.save(newPointLog);

member.earnPoint(totalPoints);
Expand All @@ -78,7 +89,7 @@ private int calculateConsecutiveAttendanceDays(Member member) {
int consecutiveDays = 0;
for (int i = 1; i <= 5; i++) {
LocalDate dateToCheck = today.minusDays(i);
if (pointLogRepository.existsByReceivedDateAndMemberAndType(today, member, PointLog.Type.ATTENDANCE)) {
if (pointLogRepository.existsByReceivedDateAndMemberAndTypeIn(dateToCheck, member, ATTENDANCE_LISTS)) {
consecutiveDays++;
} else {
break;
Expand All @@ -97,24 +108,46 @@ private int calculateBonusPoints(int consecutiveDays) {
};
}

private PointLog.Type getAttendanceType(int consecutiveDays) {
return switch (consecutiveDays) {
case 2 -> PointLog.Type.ATTENDANCE_DAY_2;
case 3 -> PointLog.Type.ATTENDANCE_DAY_3;
case 4 -> PointLog.Type.ATTENDANCE_DAY_4;
case 5 -> PointLog.Type.ATTENDANCE_DAY_5_OR_MORE;
default -> PointLog.Type.ATTENDANCE_DAY_1;
};
}

@Transactional
public void earnDiaryWritePointPerDay(Member member) {
LocalDate today = LocalDate.now();

if (!pointLogRepository.existsByReceivedDateAndMemberAndType(today, member, PointLog.Type.DIARY)) {
if (!pointLogRepository.existsByReceivedDateAndMemberAndTypeIn(today, member, List.of(PointLog.Type.DIARY))) {
PointLog newPointLog = new PointLog(PointLog.Type.DIARY, PointLog.Status.EARNED, member, DIARY_WRITE_POINT);
pointLogRepository.save(newPointLog);

member.earnPoint(DIARY_WRITE_POINT);
}
}

@Transactional
public void earnUsingChatBotPointPerDay(Member member) {
LocalDate today = LocalDate.now();

if (!pointLogRepository.existsByReceivedDateAndMemberAndTypeIn(today, member, List.of(PointLog.Type.CHAT_BOT))) {
PointLog newPointLog = new PointLog(PointLog.Type.CHAT_BOT, PointLog.Status.EARNED, member, USING_CHAT_BOT_POINT);
pointLogRepository.save(newPointLog);

member.earnPoint(USING_CHAT_BOT_POINT);
}
}

@Transactional
public void earnResponseDailyQuestionPointPerDay(Member member) {
LocalDate today = LocalDate.now();

if (!pointLogRepository.existsByReceivedDateAndMemberAndType(today, member, PointLog.Type.DAILYRESPONSE)) {
PointLog newPointLog = new PointLog(PointLog.Type.DAILYRESPONSE, PointLog.Status.EARNED, member, DAILY_QUESTION_ANSWER_POINT);
if (!pointLogRepository.existsByReceivedDateAndMemberAndTypeIn(today, member, List.of(PointLog.Type.DAILY_RESPONSE))) {
PointLog newPointLog = new PointLog(PointLog.Type.DAILY_RESPONSE, PointLog.Status.EARNED, member, DAILY_QUESTION_ANSWER_POINT);
pointLogRepository.save(newPointLog);

member.earnPoint(DAILY_QUESTION_ANSWER_POINT);
Expand Down

0 comments on commit 287c2cc

Please sign in to comment.