Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package umc7th.bulk.dailyMeal.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import umc7th.bulk.dailyMeal.entity.DailyMeal;

import java.util.Optional;

public interface DailyMealRepository extends JpaRepository<DailyMeal, Long> {

@Query("""
SELECT dm FROM DailyMeal dm
JOIN FETCH dm.mealPlan mp
JOIN FETCH mp.user u
WHERE dm.id = :dailyMealId
""")
Optional<DailyMeal> findByIdWithMealPlanAndUser(@Param("dailyMealId") Long dailyMealId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DailyMeal createDailyMeal(MealPlan mealPlan, MealPlanRequestDTO.DailyMeal
@Override
public DailyMealResponseDTO.DailyMealGetResponseDTO getDailyMeal(Long userId, Long dailyMealId) {

DailyMeal dailyMeal = dailyMealRepository.findById(dailyMealId).orElseThrow(() ->
DailyMeal dailyMeal = dailyMealRepository.findByIdWithMealPlanAndUser(dailyMealId).orElseThrow(() ->
new DailyMealException(DailyMealErrorCode.NOT_FOUND));

if (!dailyMeal.getMealPlan().getUser().getId().equals(userId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MealQueryServiceImpl implements MealQueryService{
public MealResponseDTO.MealPreviewDTO getMealItems(Long userId, Long dailyMealId, MealType type, Long cursorId, int pagSize) {

// 하루 식단 확인
DailyMeal dailyMeal = dailyMealRepository.findById(dailyMealId).orElseThrow(
DailyMeal dailyMeal = dailyMealRepository.findByIdWithMealPlanAndUser(dailyMealId).orElseThrow(
() -> new DailyMealException(DailyMealErrorCode.NOT_FOUND));

if (!dailyMeal.getMealPlan().getUser().getId().equals(userId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public class MealPlanQueryServiceImpl implements MealPlanQueryService {
@Override
public MealPlan getMealPlan(Long userId, Long mealPlanId) {

User user = userRepository.findById(userId).orElseThrow(() -> new UserException(UserErrorCode.USER_NOT_FOUND));
MealPlan mealPlan = mealPlanRepository.findById(mealPlanId).orElseThrow(() -> new MealPlanException(MealPlanErrorCode.MEAL_PLAN_NOT_FOUND));
if (!mealPlan.getUser().getId().equals(user.getId()) ) {
if (!mealPlan.getUser().getId().equals(userId) ) {
throw new CustomException(GeneralErrorCode.FORBIDDEN_403);
}
return mealPlan;
Expand Down