Skip to content

Commit

Permalink
Merge branch 'develop' into fix/#60
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Jul 19, 2023
2 parents e021777 + b9ade28 commit 8c2b6ce
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public ArticleSummaryResponse findArticlesByWeekAndMemberId(Long memberId, short

}

@Transactional
public ArticleDetailResponse findArticleDetail(Long memberId, Long articleId) {
Article article = ArticleServiceUtils.findArticleById(articleRepository, articleId);
Member member = MemberServiceUtils.findMemberById(memberRepository, memberId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.chiwawa.lionheart.api.service.challenge;

import static com.chiwawa.lionheart.common.constant.message.AttendanceErrorMessage.*;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chiwawa.lionheart.api.service.member.MemberServiceUtils;
import com.chiwawa.lionheart.common.dto.WeekAndDay;
import com.chiwawa.lionheart.common.exception.ErrorCode;
import com.chiwawa.lionheart.common.exception.model.NotFoundException;
import com.chiwawa.lionheart.common.util.DateUtils;
import com.chiwawa.lionheart.common.util.MessageUtils;
import com.chiwawa.lionheart.domain.domain.article.Article;
import com.chiwawa.lionheart.domain.domain.challenge.Attendance;
import com.chiwawa.lionheart.domain.domain.challenge.repository.AttendanceRepository;
Expand All @@ -33,6 +29,10 @@ public class ChallengeService {
private final static int ZERO = 0;

public void checkAttendance(Article article, Member member) {
if (isNotAttended(member)) {
return;
}

WeekAndDay weekAndDay = MemberServiceUtils.findMemberWeekAndDay(member);
boolean isTodayArticle = weekAndDay.equals(WeekAndDay.of(article.getWeek(), article.getDay()));

Expand All @@ -45,7 +45,7 @@ public void checkAttendance(Article article, Member member) {
}

private boolean validateLastAttendanceIsNotToday(Member member) {
Attendance latestMemberAttendance = findMemberLastAttendance(member);
Attendance latestMemberAttendance = findMemberLastAttendance(member).get();
int dayDifference = DateUtils.getDayDifference(LocalDateTime.now(), latestMemberAttendance.getCreatedAt());

if (dayDifference == ZERO) {
Expand All @@ -54,13 +54,17 @@ private boolean validateLastAttendanceIsNotToday(Member member) {
return false;
}

private Attendance findMemberLastAttendance(Member member) {
List<Attendance> memberAttendances = attendanceRepository.findAttendancesByMember(member);
private boolean isNotAttended(Member member) {
Optional<Attendance> firstMemberAttendance = findMemberLastAttendance(member);
if (firstMemberAttendance.isEmpty()) {
return true;
}

return DateUtils.getDayDifference(LocalDateTime.now(), firstMemberAttendance.get().getCreatedAt()) != ZERO;
}

return memberAttendances.stream().max(Comparator.comparing(Attendance::getCreatedAt))
.orElseThrow(() ->
new NotFoundException(
MessageUtils.generate(NOT_EXIST_MEMBER_ATTENDANCE_DATA_ERROR_MESSAGE, member.getId()),
ErrorCode.NOT_FOUND_MEMBER_ATTENDANCE_DATA_EXCEPTION));
private Optional<Attendance> findMemberLastAttendance(Member member) {
List<Attendance> memberAttendances = attendanceRepository.findAttendancesByMember(member);
return memberAttendances.stream().max(Comparator.comparing(Attendance::getCreatedAt));
}
}
2 changes: 1 addition & 1 deletion lionheart-api/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring:

sql:
init:
mode: always
mode: never
schema-locations: classpath:sql/schema.sql
data-locations: classpath:sql/data.sql

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public enum ErrorCode {
NOT_FOUND_CHALLENGE_EXCEPTION("N004", "존재하지 않는 챌린지입니다."),
NOT_FOUND_ARTICLE_EXCEPTION("N005", "삭제되었거나 존재하지 않는 아티클입니다."),
NOT_FOUND_ARTICLE_IN_WEEK_AND_DAY_EXCEPTION("N006", "해당 주차 일차에 해당하는 아티클이 존재하지 않습니다."),
NOT_FOUND_MEMBER_ATTENDANCE_DATA_EXCEPTION("N007", "해당 유저의 출석체크 정보가 존재하지 않습니다"),

// Conflict Exception
CONFLICT_EXCEPTION("C001", "이미 존재합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Article extends BaseEntity {
private String title;

@Column(name = "CATEGORY", length = 30)
@Enumerated(value = EnumType.STRING)
private Category category;

@Column(name = "MAIN_IMAGE_URL", nullable = false, length = 300)
Expand All @@ -59,10 +60,10 @@ public class Article extends BaseEntity {
private String author;

@Column(name = "WEEK")
private short week;
private Short week;

@Column(name = "DAY")
private short day;
private Short day;

@Column(name = "REQUIRED_TIME", nullable = false)
private short requiredTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public class Attendance extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MEMBER_ID", nullable = false)
private Member member;

public static Attendance newInstance(Member member) {
return Attendance.builder()
.member(member)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@
public interface ChallengeRepositoryCustom {

Optional<Challenge> findChallengeByMember(Member member);

void checkAttendance(Member member);
}

0 comments on commit 8c2b6ce

Please sign in to comment.