Skip to content

Commit

Permalink
Merge pull request #427 from JNU-econovation/fix/#425
Browse files Browse the repository at this point in the history
[Be/fix] 특정 날짜의 식사 정보 조회하는 API 오류 수정
  • Loading branch information
hwangdaesun authored Jul 22, 2024
2 parents 89778c2 + a6982b5 commit 764e34f
Show file tree
Hide file tree
Showing 27 changed files with 271 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.gaebaljip.exceed.adapter.in.nutritionist.request.GetAllAnalysisRequest;
import com.gaebaljip.exceed.application.port.in.meal.GetCurrentMealQuery;
import com.gaebaljip.exceed.application.port.in.meal.GetSpecificMealQuery;
import com.gaebaljip.exceed.application.port.in.meal.ValidateBeforeSignUpUsecase;
import com.gaebaljip.exceed.application.port.in.member.GetMaintainMealUsecase;
import com.gaebaljip.exceed.application.port.in.member.GetTargetMealUsecase;
import com.gaebaljip.exceed.application.service.nutritionist.GetAllCalorieAnalysisService;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class GetMealController {
private final GetCurrentMealQuery getCurrentMealQuery;
private final GetSpecificMealQuery getSpecificMealQuery;
private final GetAllCalorieAnalysisService getAllCalorieAnalysisService;
private final ValidateBeforeSignUpUsecase validateDateBeforeSignUpUsecase;

/** 오늘 먹은 식사 정보(단,탄,지 및 칼로리) 조회 */
@Operation(summary = "오늘 먹은 식사 정보 조회", description = "오늘 먹은 식사 정보(단,탄,지 및 칼로리)를 조회한다.")
Expand All @@ -68,6 +70,7 @@ public ApiResponse<ApiResponse.CustomBody<GetMealFoodResponse>> getMealFood(
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
@Parameter(hidden = true) @AuthenticationMemberId Long memberId) {
LocalDateTime localDateTime = date.atStartOfDay();
validateDateBeforeSignUpUsecase.execute(memberId, localDateTime);
MaintainMealDTO maintainMealDTO = getMaintainMealUsecase.execute(memberId, localDateTime);
TargetMealDTO targetMealDTO = getTargetMealUsecase.execute(memberId, localDateTime);
SpecificMealDTO specificMealDTO = getSpecificMealQuery.execute(memberId, localDateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public HistoryEntity command(HistoryEntity historyEntity) {
}

@Override
public HistoryEntity findByMemberIdAndDate(Long memberId, LocalDateTime date) {
return historyRepository.findByMemberIdAndDate(memberId, date);
public HistoryEntity findMostRecentFutureMember(Long memberId, LocalDateTime date) {
return historyRepository.findMostRecentFutureMember(memberId, date);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public MemberEntity findCheckedMemberByEmail(String email) {
}

@Override
public Optional<MemberEntity> findByIdAndDate(Long memberId, LocalDateTime date) {
return memberRepository.findByIdAndDate(memberId, date);
public Optional<MemberEntity> findMemberBeforeDate(Long memberId, LocalDateTime date) {
return memberRepository.findMemberBeforeDate(memberId, date);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

@Repository
public interface CustomHistoryRepository {
HistoryEntity findByMemberIdAndDate(Long memberId, LocalDateTime date);
HistoryEntity findMostRecentFutureMember(Long memberId, LocalDateTime date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ public class CustomHistoryRepositoryImpl implements CustomHistoryRepository {
private final JPAQueryFactory queryFactory;

@Override
public HistoryEntity findByMemberIdAndDate(Long memberId, LocalDateTime date) {
public HistoryEntity findMostRecentFutureMember(Long memberId, LocalDateTime date) {
return queryFactory
.selectFrom(historyEntity)
.where(historyEntity.id.eq(memberId).and(checkDate(historyEntity, date)))
.orderBy(historyEntity.id.desc())
.where(
historyEntity
.memberEntity
.id
.eq(memberId)
.and(checkFutureDate(historyEntity, date)))
.orderBy(historyEntity.id.asc())
.fetchFirst();
}

private BooleanExpression checkDate(QHistoryEntity historyEntity, LocalDateTime date) {
return historyEntity.createdDate.before(date);
private BooleanExpression checkFutureDate(QHistoryEntity historyEntity, LocalDateTime date) {
return historyEntity.createdDate.after(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

@Repository
public interface CustomMemberRepository {
Optional<MemberEntity> findByIdAndDate(Long memberId, LocalDateTime date);
Optional<MemberEntity> findMemberBeforeDate(Long memberId, LocalDateTime date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CustomMemberRepositoryImpl implements CustomMemberRepository {
private final JPAQueryFactory queryFactory;

@Override
public Optional<MemberEntity> findByIdAndDate(Long memberId, LocalDateTime date) {
public Optional<MemberEntity> findMemberBeforeDate(Long memberId, LocalDateTime date) {
MemberEntity result =
queryFactory
.selectFrom(memberEntity)
Expand All @@ -31,6 +31,6 @@ public Optional<MemberEntity> findByIdAndDate(Long memberId, LocalDateTime date)
}

private BooleanExpression checkDate(QMemberEntity memberEntity, LocalDateTime date) {
return memberEntity.createdDate.eq(date);
return memberEntity.updatedDate.before(date);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gaebaljip.exceed.application.domain.member;

import java.time.LocalDateTime;

import javax.persistence.*;

import org.hibernate.annotations.ColumnDefault;
Expand Down Expand Up @@ -98,4 +100,8 @@ public boolean checkOnBoarding() {
&& this.getGender() != null
&& this.getTargetWeight() != null;
}

public boolean checkIfBeforeSignUp(LocalDateTime checkDateTime) {
return checkDateTime.isBefore(this.getCreatedDate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gaebaljip.exceed.application.port.in.meal;

import java.time.LocalDateTime;

import com.gaebaljip.exceed.common.annotation.UseCase;

@UseCase
public interface ValidateBeforeSignUpUsecase {
void execute(Long memberId, LocalDateTime dateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface HistoryPort {
HistoryEntity command(HistoryEntity historyEntity);

HistoryEntity findByMemberIdAndDate(Long memberId, LocalDateTime date);
HistoryEntity findMostRecentFutureMember(Long memberId, LocalDateTime date);

List<HistoryEntity> findByMemberEntity(MemberEntity memberEntity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface MemberPort {

MemberEntity findCheckedMemberByEmail(String email);

Optional<MemberEntity> findByIdAndDate(Long memberId, LocalDateTime date);
Optional<MemberEntity> findMemberBeforeDate(Long memberId, LocalDateTime date);

Boolean existsByEmail(String email);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.gaebaljip.exceed.application.service.meal;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.gaebaljip.exceed.adapter.out.jpa.member.MemberPersistenceAdapter;
import com.gaebaljip.exceed.application.domain.member.MemberEntity;
import com.gaebaljip.exceed.application.port.in.meal.ValidateBeforeSignUpUsecase;
import com.gaebaljip.exceed.common.exception.meal.InValidDateFoundException;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ValidateBeforeSignupService implements ValidateBeforeSignUpUsecase {

private final MemberPersistenceAdapter memberPersistenceAdapter;

@Override
public void execute(Long memberId, LocalDateTime dateTime) {
MemberEntity memberEntity = memberPersistenceAdapter.query(memberId);
if (memberEntity.checkIfBeforeSignUp(dateTime)) {
throw InValidDateFoundException.EXECPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public MaintainMealDTO execute(Long memberId) {
@Override
@Transactional(readOnly = true)
public MaintainMealDTO execute(Long memberId, LocalDateTime date) {
Optional<MemberEntity> memberEntity = memberPort.findByIdAndDate(memberId, date);
Optional<MemberEntity> memberEntity = memberPort.findMemberBeforeDate(memberId, date);
if (memberEntity.isPresent()) {
Member member = memberConverter.toModel(memberEntity.get());
return toMaintainMeal(member);
} else {
HistoryEntity lastestHistoryEntity = historyPort.findByMemberIdAndDate(memberId, date);
HistoryEntity lastestHistoryEntity =
historyPort.findMostRecentFutureMember(memberId, date);
Member member = memberConverter.toModel(lastestHistoryEntity);
return toMaintainMeal(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public TargetMealDTO execute(Long memberId) {

@Override
public TargetMealDTO execute(Long memberId, LocalDateTime date) {
Optional<MemberEntity> memberEntity = memberPort.findByIdAndDate(memberId, date);
Optional<MemberEntity> memberEntity = memberPort.findMemberBeforeDate(memberId, date);
if (memberEntity.isPresent()) {
Member member = memberConverter.toModel(memberEntity.get());
return toTargetMeal(member);
} else {
HistoryEntity lastestHistoryEntity = historyPort.findByMemberIdAndDate(memberId, date);
HistoryEntity lastestHistoryEntity =
historyPort.findMostRecentFutureMember(memberId, date);
Member member = memberConverter.toModel(lastestHistoryEntity);
return toTargetMeal(member);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gaebaljip.exceed.common.docs.meal;

import com.gaebaljip.exceed.common.exception.EatCeedException;
import com.gaebaljip.exceed.common.exception.meal.InsufficientMealsException;
import com.gaebaljip.exceed.common.exception.meal.InvalidMultipleException;
import com.gaebaljip.exceed.common.exception.meal.NotSameDateException;
import com.gaebaljip.exceed.common.exception.member.InvalidGenderException;
Expand All @@ -12,9 +11,6 @@

@ExceptionDoc
public class GetMealExceptionDocs implements SwaggerExampleExceptions {
@ExplainError("Daily Meal에 최소 1끼도 제공 되지 않았을 때 ")
public EatCeedException Daily_Meal_최소_1끼도_제공_되지_않았습니다 = InsufficientMealsException.EXECPTION;

@ExplainError("0인분 이하거나 100인분 초과일 경우 ")
public EatCeedException _0인분_이하거나_100인분_초과일_경우 = InvalidMultipleException.EXECPTION;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gaebaljip.exceed.common.docs.meal;

import com.gaebaljip.exceed.common.exception.EatCeedException;
import com.gaebaljip.exceed.common.exception.meal.InsufficientMealsException;
import com.gaebaljip.exceed.common.exception.meal.InValidDateFoundException;
import com.gaebaljip.exceed.common.exception.meal.NotSameDateException;
import com.gaebaljip.exceed.common.exception.member.InvalidGenderException;
import com.gaebaljip.exceed.common.exception.member.MemberNotFoundException;
Expand All @@ -11,9 +11,6 @@

@ExceptionDoc
public class GetMealFoodExceptionDocs implements SwaggerExampleExceptions {
@ExplainError("Daily Meal에 최소 1끼도 제공 되지 않았을 때 ")
public EatCeedException Daily_Meal_최소_1끼도_제공_되지_않았습니다 = InsufficientMealsException.EXECPTION;

@ExplainError("식사들의 날짜가 다를 경우")
public EatCeedException 식사들의_날짜가_다를_경우 = NotSameDateException.EXECPTION;

Expand All @@ -22,4 +19,7 @@ public class GetMealFoodExceptionDocs implements SwaggerExampleExceptions {

@ExplainError("회원이 존재하지 않을 때")
public EatCeedException 회원이_없을_때 = MemberNotFoundException.EXECPTION;

@ExplainError("켈린더 상세 조회시 회원가입 전의 기록을 보려고 할 때")
public EatCeedException 회원_전의_기록_열람시 = InValidDateFoundException.EXECPTION;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gaebaljip.exceed.common.exception.meal;

import com.gaebaljip.exceed.common.exception.EatCeedException;

import lombok.Getter;

@Getter
public class InValidDateFoundException extends EatCeedException {
public static EatCeedException EXECPTION = new InValidDateFoundException();

public InValidDateFoundException() {
super(MealError.INVALID_DATE_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum MealError implements BaseError {
400,
"6002",
"'multiple'은 null이 아니고 'g'는 null이어야 하거나, 'multiple'은 null이고 'g'는 null이 아니어야 합니다."),
INVALID_G(400, "6002", "g은 1 이상이어야합니다.");
INVALID_G(400, "6002", "g은 1 이상이어야합니다."),
INVALID_DATE_FOUND(400, "6321", "회원가입 이전의 기록은 없습니다.");

private final Integer status;
private final String code;
Expand Down
24 changes: 13 additions & 11 deletions BE/exceed/src/main/resources/db/testData.sql
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@

INSERT INTO MEMBER_TB (MEMBER_PK, CREATED_DATE, UPDATED_DATE, MEMBER_ACTIVITY, MEMBER_AGE, MEMBER_ETC, MEMBER_GENDER,
MEMBER_HEIGHT, MEMBER_WEIGHT, MEMBER_TARGET_WEIGHT, MEMBER_EMAIL, MEMBER_PASSWORD, MEMBER_ROLE, MEMBER_CHECKED)
VALUES (1, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 175.0, 70.0, 68.0, 'abcd123!@gmail.com',
VALUES (1, '2023-12-01 08:00:00', '2023-12-07 09:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 175.0, 70.0, 78.0, 'abcd123!@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.0Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(2, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 178.0, 72.0, 69.0, 'abcd234@@gmail.com',
(2, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 178.0, 72.0, 79.0, 'abcd234@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.2Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(3, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 165.0, 60.0, 58.0, 'abcd345@@gmail.com',
(3, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 165.0, 60.0, 78.0, 'abcd345@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.3Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(4, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 180.0, 75.0, 72.0, 'abcd456@@gmail.com',
(4, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 180.0, 75.0, 82.0, 'abcd456@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.4Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(5, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 170.0, 68.0, 65.0, 'abcd567@@gmail.com',
(5, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 170.0, 68.0, 75.0, 'abcd567@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.5Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(6, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 177.0, 71.0, 67.0, 'abcd678@@gmail.com',
(6, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 177.0, 71.0, 77.0, 'abcd678@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.6Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(7, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 169.0, 69.0, 64.0, 'abcd789@@gmail.com',
(7, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 169.0, 69.0, 74.0, 'abcd789@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.7Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(8, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 181.0, 74.0, 70.0, 'abcd890@@gmail.com',
(8, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 181.0, 74.0, 90.0, 'abcd890@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.8Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(9, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 168.0, 65.0, 62.0, 'abcd901@@gmail.com',
(9, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 168.0, 65.0, 72.0, 'abcd901@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.9Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true),
(10, '2023-12-01 08:00:00', '2023-12-01 08:00:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 172.0, 66.0, 63.0, 'abcd012@@gmail.com',
(10, '2023-12-01 08:00:00', '2023-12-01 08:10:00', 'NOT_ACTIVE', 30, '비고 없음', 1, 172.0, 66.0, 73.0, 'abcd012@@gmail.com',
'$2a$10$pljAKl0Ad3LnjQyQei.Yz.0Cfcn3Zv/xeBMDwUHDaUrfG8Wm57c56', 'MEMBER', true);


INSERT INTO HISTORY_TB(HISTORY_PK, CREATED_DATE, UPDATED_DATE, HISTORY_ACTIVITY,HISTORY_AGE,HISTORY_GENDER,HISTORY_HEIGHT, HISTORY_WEIGHT, MEMBER_FK)

VALUES(1, '2023-11-20 08:00:00', '2023-11-20 08:00:00', 'NOT_ACTIVE', 30, 1, 175.0, 65.8, 1);
VALUES(1, '2023-12-03 11:00:00', '2023-12-03 11:00:00', 'NOT_ACTIVE', 30, 1, 175.0, 71.0, 1),
(2, '2023-12-05 09:00:00', '2023-12-05 09:00:00', 'NOT_ACTIVE', 30, 1, 175.0, 72.0, 1),
(3, '2023-12-07 09:00:00', '2023-12-07 09:00:00', 'NOT_ACTIVE', 30, 1, 175.0, 73.0, 1);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.gaebaljip.exceed.application.port.in.meal.GetCurrentMealQuery;
import com.gaebaljip.exceed.application.port.in.meal.GetSpecificMealQuery;
import com.gaebaljip.exceed.application.port.in.meal.ValidateBeforeSignUpUsecase;
import com.gaebaljip.exceed.application.port.in.member.GetMaintainMealUsecase;
import com.gaebaljip.exceed.application.port.in.member.GetTargetMealUsecase;
import com.gaebaljip.exceed.application.service.nutritionist.GetAllCalorieAnalysisService;
Expand All @@ -36,6 +37,8 @@ class GetMealControllerTest extends ControllerTest {

@MockBean private GetAllCalorieAnalysisService getAllCalorieAnalysisService;

@MockBean private ValidateBeforeSignUpUsecase validateDateBeforeSignUpUsecase;

@Test
@WithMockUser
void when_getMeal_expected_success() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gaebaljip.exceed.adapter.out.jpa.member.custom;

import static org.junit.jupiter.api.Assertions.*;

import java.time.LocalDateTime;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.gaebaljip.exceed.application.domain.member.HistoryEntity;
import com.gaebaljip.exceed.common.DatabaseTest;

class CustomHistoryRepositoryImplTest extends DatabaseTest {

@Autowired private CustomHistoryRepositoryImpl customHistoryRepository;

@Test
@DisplayName(
"회원 1L : 2023-12-03 11:00:00, 2023-12-05 09:00:00, 그리고 2023-12-07 09:00:00에 회원 수정"
+ "2023-12-04의 회원 정보를 열람할 경우 2023-12-05 09:00:00 에 저장된 회원 정보를 조회해야한다.")
void when_findMostRecentFutureMemberWeight_expected_72() {
LocalDateTime dateTime = LocalDateTime.of(2023, 12, 04, 12, 00);
HistoryEntity historyEntity =
customHistoryRepository.findMostRecentFutureMember(1L, dateTime);
assertEquals(historyEntity.getWeight(), 72.0);
}
}
Loading

0 comments on commit 764e34f

Please sign in to comment.