Skip to content

Commit

Permalink
[#42] Feat: 식단 등록 개발 완
Browse files Browse the repository at this point in the history
  • Loading branch information
SY2on committed Jan 26, 2024
1 parent 224b76e commit bb1813a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import ssu.eatssu.domain.admin.dto.MealInfo;
import ssu.eatssu.domain.admin.dto.MenuBoards;
import ssu.eatssu.domain.admin.dto.RegisterMealRequest;
import ssu.eatssu.domain.admin.service.ManageMealService;
import ssu.eatssu.domain.menu.entity.TimePart;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.global.handler.response.BaseResponse;

import java.util.Date;
Expand All @@ -30,4 +30,16 @@ public BaseResponse<MenuBoards> mealPage(@RequestParam @DateTimeFormat(pattern =
MenuBoards menuBoards = manageMealService.getMenuBoards(date, timePart);
return BaseResponse.success(menuBoards);
}

@ResponseBody
@PostMapping("")
public BaseResponse register(@RequestParam Restaurant restaurant,
@RequestParam @DateTimeFormat(pattern = "yyyyMMdd") Date date,
@RequestParam TimePart timePart,
@RequestBody RegisterMealRequest request,
Model model) {
MealInfo mealInfo = new MealInfo(restaurant, date, timePart);
manageMealService.register(mealInfo, request);
return BaseResponse.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ssu.eatssu.domain.admin.dto;

import java.util.List;

public record RegisterMealRequest(List<String> menuNames) {
public RegisterMealRequest {
menuNames.sort(String::compareTo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import org.springframework.stereotype.Repository;
import ssu.eatssu.domain.admin.dto.BriefMenu;
import ssu.eatssu.domain.admin.dto.MealInfo;
import ssu.eatssu.domain.menu.entity.QMeal;
import ssu.eatssu.domain.menu.entity.QMealMenu;
import ssu.eatssu.domain.menu.entity.QMenu;
import ssu.eatssu.domain.menu.entity.TimePart;
import ssu.eatssu.domain.menu.entity.*;
import ssu.eatssu.domain.restaurant.entity.Restaurant;

import java.util.Date;
Expand All @@ -24,11 +21,33 @@ public class LoadMealRepository {
private final QMeal meal = QMeal.meal;
private final QMealMenu mealMenu = QMealMenu.mealMenu;

public List<Long> existsMeal(MealInfo query) {
return queryFactory.select(meal.id)
.from(meal)
.where(
mealRestaurantEq(query.restaurant()),
mealDateEq(query.date()),
mealTimePartEq(query.timePart())
)
.fetch();
}

public List<String> findMenuNamesByMealId(Long mealId) {
return queryFactory
.select(menu.name)
.from(mealMenu)
.join(mealMenu.meal, meal)
.on(mealIdEq(mealId))
.join(mealMenu.menu, menu)
.orderBy(menu.name.asc())
.fetch();
}

public List<Long> getAllMealIdsByInfo(MealInfo query) {
return queryFactory.select(meal.id)
.from(meal)
.where(
restaurantEq(query.restaurant()),
mealRestaurantEq(query.restaurant()),
mealDateEq(query.date()),
mealTimePartEq(query.timePart())
)
Expand All @@ -49,6 +68,15 @@ public List<BriefMenu> findBriefMenusByMealId(Long mealId) {
.fetch();
}

public Menu findMenu(String menuName, Restaurant restaurant) {
return queryFactory.selectFrom(menu)
.where(
menuNameEq(menuName),
menuRestaurantEq(restaurant)
)
.fetchFirst();
}

private BooleanExpression mealIdEq(Long mealId) {
return meal.id.eq(mealId);
}
Expand All @@ -61,8 +89,17 @@ private BooleanExpression mealDateEq(Date date) {
return meal.date.eq(date);
}

private BooleanExpression restaurantEq(Restaurant restaurant) {
private BooleanExpression mealRestaurantEq(Restaurant restaurant) {
return meal.restaurant.eq(restaurant);
}

private BooleanExpression menuRestaurantEq(Restaurant restaurant) {
return menu.restaurant.eq(restaurant);
}

private BooleanExpression menuNameEq(String menuName) {
return menu.name.eq(menuName);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ssu.eatssu.domain.admin.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import ssu.eatssu.domain.menu.entity.MealMenu;

public interface ManageMealMenuRepository extends JpaRepository<MealMenu, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ssu.eatssu.domain.admin.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import ssu.eatssu.domain.menu.entity.Meal;

public interface ManageMealRepository extends JpaRepository<Meal, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ssu.eatssu.domain.admin.dto.*;
import ssu.eatssu.domain.admin.persistence.LoadMealRepository;
import ssu.eatssu.domain.admin.persistence.MenuRatingRepository;
import ssu.eatssu.domain.admin.persistence.*;
import ssu.eatssu.domain.menu.entity.Meal;
import ssu.eatssu.domain.menu.entity.MealMenu;
import ssu.eatssu.domain.menu.entity.Menu;
import ssu.eatssu.domain.menu.entity.TimePart;
import ssu.eatssu.domain.restaurant.entity.RestaurantType;
import ssu.eatssu.global.handler.response.BaseException;
import ssu.eatssu.global.handler.response.BaseResponseStatus;

import java.util.Date;
import java.util.List;
Expand All @@ -17,6 +21,9 @@ public class ManageMealService {

private final LoadMealRepository loadMealRepository;
private final MenuRatingRepository menuRatingRepository;
private final ManageMealRepository manageMealRepository;
private final ManageMealMenuRepository manageMealMenuRepository;
private final ManageMenuRepository manageMenuRepository;

public MenuBoards getMenuBoards(Date date, TimePart timePart) {
MenuBoards menuBoards = new MenuBoards();
Expand Down Expand Up @@ -58,4 +65,47 @@ private MenuSection getMenuSection(Long mealId) {
return menuSection;
}

public void register(MealInfo mealInfo, RegisterMealRequest request) {
// 중복 체크
duplicateCheck(mealInfo, request);

// 식단 등록
Meal meal = new Meal(mealInfo.date(), mealInfo.timePart(), mealInfo.restaurant());
manageMealRepository.save(meal);

//식단에 메뉴 추가
addMenusToMeal(meal, request.menuNames());

}

private void addMenusToMeal(Meal meal, List<String> menuNames) {
menuNames.forEach(menuName -> {
Menu menu = loadMealRepository.findMenu(menuName, meal.getRestaurant());
if (menu == null) {
menu = Menu.createVariable(menuName, meal.getRestaurant());
manageMenuRepository.save(menu);
}
manageMealMenuRepository.save(MealMenu.builder().meal(meal).menu(menu).build());
});
}

private void duplicateCheck(MealInfo mealInfo, RegisterMealRequest request) {
List<Long> mealIds = loadMealRepository.existsMeal(mealInfo);
mealIds.forEach(mealId -> {
if (hasMenus(mealId, request.menuNames())) {
throw new BaseException(BaseResponseStatus.CONFLICT);
}
});
}

private boolean hasMenus(Long mealId, List<String> menuNames) {
List<String> menuNamesInMeal = loadMealRepository.findMenuNamesByMealId(mealId);

/**
* menuNamesInMeal 과 menuNames 는 오름차순 정렬된 상태
* @see LoadMealRepository#findMenuNamesByMealId(Long)
* @see RegisterMealRequest#RegisterMealRequest
*/
return menuNamesInMeal.equals(menuNames);
}
}

0 comments on commit bb1813a

Please sign in to comment.