Skip to content

Commit

Permalink
[#83] refactor : Restaurant Entity를 Enum 클래스로 변경한다
Browse files Browse the repository at this point in the history
  • Loading branch information
packdev937 committed Jan 18, 2024
1 parent 8bdb950 commit 2cd2361
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 113 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ssu/eatssu/domain/menu/entity/Meal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.domain.restaurant.entity.TemporalRestaurant;

import java.util.*;

Expand All @@ -27,8 +28,7 @@ public class Meal {
@Enumerated(EnumType.STRING)
private TimePart timePart;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id")
@Embedded
private Restaurant restaurant;

@OneToMany(mappedBy = "meal", cascade = CascadeType.ALL)
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/ssu/eatssu/domain/menu/entity/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import lombok.*;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.domain.restaurant.entity.TemporalRestaurant;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,8 +30,7 @@ public class Menu {
@OneToMany(mappedBy = "menu", cascade = CascadeType.ALL)
private List<MealMenu> mealMenus = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id")
@Embedded
private Restaurant restaurant;

@Embedded
Expand All @@ -42,14 +42,19 @@ private Menu(String name, Restaurant restaurant, Integer price) {
this.price = price;
}

private Menu(String name, Integer price) {
this.name = name;
this.price = price;
}

/**
* 변동 메뉴를 생성합니다.
* todo: 변동메뉴 식당이 아니라 고정 메뉴 식당으로 잘못 들어온다면 어떻게 처리?
*/
public static Menu createChangeMenu(String name, Restaurant restaurant) {
public static Menu createVariable(String name, Restaurant restaurant) {
int price = 0;
if (MenuType.isChanged(restaurant.getRestaurantName())) {
price = restaurant.getRestaurantName().getPrice();
if (restaurant.isVariable()) {
price = restaurant.getPrice();
}
return new Menu(name, restaurant, price);
}
Expand All @@ -58,7 +63,7 @@ public static Menu createChangeMenu(String name, Restaurant restaurant) {
* 고정 메뉴를 생성합니다.
* todo: 고정메뉴 식당이 아니라 변동 메뉴 식당으로 잘못 들어온다면 어떻게 처리?
*/
public static Menu createFixedMenu(String name, Restaurant restaurant, Integer price) {
public static Menu createFixed(String name, Restaurant restaurant, Integer price) {
return new Menu(name, restaurant, price);
}

Expand Down
16 changes: 4 additions & 12 deletions src/main/java/ssu/eatssu/domain/menu/entity/MenuType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import java.util.Arrays;
import java.util.List;
import ssu.eatssu.domain.restaurant.entity.RestaurantName;
import ssu.eatssu.domain.restaurant.entity.Restaurant;

import static ssu.eatssu.domain.restaurant.entity.RestaurantName.*;
import static ssu.eatssu.domain.restaurant.entity.Restaurant.*;


@Getter
Expand All @@ -16,21 +16,13 @@ public enum MenuType {
VARIABLE("변동 메뉴", Arrays.asList(DODAM, DORMITORY, HAKSIK));

private final String description;
private final List<RestaurantName> restaurants;
private final List<Restaurant> restaurants;

MenuType(String description, List<RestaurantName> restaurants) {
MenuType(String description, List<Restaurant> restaurants) {
this.description = description;
this.restaurants = restaurants;
}

public static boolean isFixed(RestaurantName restaurant) {
return FIXED.getRestaurants().contains(restaurant);
}

public static boolean isChanged(RestaurantName restaurant) {
return VARIABLE.getRestaurants().contains(restaurant);
}

@JsonCreator
public static MenuType from(String description) {
return Arrays.stream(MenuType.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import ssu.eatssu.domain.menu.dto.MenuRequest.CreateMealRequest;
import ssu.eatssu.domain.menu.dto.MenuResponse.MealInformationResponse;
import ssu.eatssu.domain.menu.service.MenuService;
import ssu.eatssu.domain.restaurant.entity.RestaurantName;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.global.handler.response.BaseException;
import ssu.eatssu.global.handler.response.BaseResponse;

Expand All @@ -52,10 +52,10 @@ public class MealController {
@PostMapping("")
public BaseResponse<?> createMeal(
@Parameter(description = "날짜(yyyyMMdd)") @RequestParam("date") @DateTimeFormat(pattern = "yyyyMMdd") Date date,
@Parameter(description = "식당이름") @RequestParam("restaurant") RestaurantName restaurantName,
@Parameter(description = "식당이름") @RequestParam("restaurant") Restaurant restaurantName,
@Parameter(description = "시간대") @RequestParam("time") TimePart timePart,
@RequestBody CreateMealRequest createMealRequest) {
if (RestaurantName.isFixed(restaurantName)) {
if (restaurantName.isFixed()) {
throw new BaseException(NOT_SUPPORT_RESTAURANT);
}

Expand All @@ -76,9 +76,9 @@ public BaseResponse<?> createMeal(
@GetMapping("")
public BaseResponse<List<MealInformationResponse>> getMeal(
@Parameter(description = "날짜(yyyyMMdd)") @RequestParam("date") @DateTimeFormat(pattern = "yyyyMMdd") Date date,
@Parameter(description = "식당 이름") @RequestParam("restaurant") RestaurantName restaurantName,
@Parameter(description = "식당 이름") @RequestParam("restaurant") Restaurant restaurantName,
@Parameter(description = "시간대") @RequestParam("time") TimePart timePart) {
if (RestaurantName.isFixed(restaurantName)) {
if (restaurantName.isFixed()) {
throw new BaseException(NOT_SUPPORT_RESTAURANT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.*;
import ssu.eatssu.domain.menu.dto.MenuResponse.MenuInformationResponse;
import ssu.eatssu.domain.menu.dto.MenuResponse.MenusInformationResponse;
import ssu.eatssu.domain.restaurant.entity.RestaurantName;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.global.handler.response.BaseException;
import ssu.eatssu.domain.menu.service.MenuService;

Expand All @@ -39,8 +39,8 @@ public class MenuController {
})
@GetMapping("")
public BaseResponse<List<MenuInformationResponse>> getMenus(
@RequestParam("restaurant") RestaurantName restaurantName) {
if (RestaurantName.isVariable(restaurantName)) {
@RequestParam("restaurant") Restaurant restaurantName) {
if (restaurantName.isVariable()) {
throw new BaseException(NOT_SUPPORT_RESTAURANT);
}
return BaseResponse.success(menuService.findMenusByRestaurant(restaurantName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import org.springframework.data.jpa.repository.JpaRepository;
import ssu.eatssu.domain.menu.entity.Meal;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.domain.restaurant.entity.TemporalRestaurant;
import ssu.eatssu.domain.menu.entity.TimePart;

import java.util.Date;
import java.util.List;

public interface MealRepository extends JpaRepository<Meal, Long> {
List<Meal> findAllByDateAndTimePartAndRestaurant(Date date, TimePart timePart, Restaurant restaurant);

List<Meal> findAllByDateAndTimePartAndRestaurant(Date date, TimePart timePart,
Restaurant restaurant);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import ssu.eatssu.domain.menu.entity.Menu;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.domain.restaurant.entity.TemporalRestaurant;

import java.util.List;
import java.util.Optional;
Expand Down
24 changes: 7 additions & 17 deletions src/main/java/ssu/eatssu/domain/menu/service/MenuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ssu.eatssu.domain.menu.entity.MealMenu;
import ssu.eatssu.domain.menu.entity.Menu;
import ssu.eatssu.domain.restaurant.entity.Restaurant;
import ssu.eatssu.domain.restaurant.entity.RestaurantName;
import ssu.eatssu.domain.restaurant.entity.TemporalRestaurant;
import ssu.eatssu.domain.menu.entity.TimePart;
import ssu.eatssu.domain.menu.repository.MealMenuRepository;
import ssu.eatssu.domain.menu.repository.MealRepository;
Expand All @@ -35,10 +35,8 @@ public class MenuService {
private final MenuRepository menuRepository;
private final MealRepository mealRepository;
private final MealMenuRepository mealMenuRepository;
private final RestaurantRepository restaurantRepository;

public List<MenuInformationResponse> findMenusByRestaurant(RestaurantName restaurantName) {
Restaurant restaurant = getRestaurant(restaurantName);
public List<MenuInformationResponse> findMenusByRestaurant(Restaurant restaurant) {
List<Menu> menus = menuRepository.findAllByRestaurant(restaurant);

return menus.stream()
Expand All @@ -47,19 +45,17 @@ public List<MenuInformationResponse> findMenusByRestaurant(RestaurantName restau
}

public List<MealInformationResponse> findSpecificMeals(Date date,
RestaurantName restaurantName,
Restaurant restaurant,
TimePart timePart) {
Restaurant restaurant = getRestaurant(restaurantName);
List<Meal> meals = getMeals(date, timePart, restaurant);

return meals.stream()
.map(meal -> MealInformationResponse.from(meal))
.toList();
}

public void createMeal(Date date, RestaurantName restaurantName, TimePart timePart,
public void createMeal(Date date, Restaurant restaurant, TimePart timePart,
CreateMealRequest request) {
Restaurant restaurant = getRestaurant(restaurantName);
List<Meal> meals = getMeals(date, timePart, restaurant);

if (MenuValidator.validateExistedMeal(meals,
Expand Down Expand Up @@ -95,10 +91,10 @@ private void addMenu(Meal meal, CreateMealRequest request) {

private void checkAndCreateMenu(String menuName, Restaurant restaurant) {
if (!menuRepository.existsByNameAndRestaurant(menuName, restaurant)) {
menuRepository.save(Menu.createChangeMenu(menuName, restaurant));
menuRepository.save(Menu.createVariable(menuName, restaurant));
}

menuRepository.save(Menu.createChangeMenu(menuName, restaurant));
menuRepository.save(Menu.createVariable(menuName, restaurant));
}

public MenusInformationResponse findMenusInMeal(Long mealId) {
Expand All @@ -119,7 +115,7 @@ public void deleteMeal(Long mealId) {
cleanupGarbageMenu(menus);
}

public void cleanupGarbageMenu(List<Menu> menuList) {
private void cleanupGarbageMenu(List<Menu> menuList) {
for (Menu menu : menuList) {
if (menu.getMealMenus().isEmpty()) {
menuRepository.delete(menu);
Expand All @@ -144,10 +140,4 @@ private List<Meal> getMeals(Date date, TimePart timePart, Restaurant restaurant)
timePart, restaurant);
return meals;
}

private Restaurant getRestaurant(RestaurantName restaurantName) {
Restaurant restaurant = restaurantRepository.findByRestaurantName(restaurantName)
.orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_FOUND_RESTAURANT));
return restaurant;
}
}
3 changes: 0 additions & 3 deletions src/main/java/ssu/eatssu/domain/menu/util/MenuValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import ssu.eatssu.domain.menu.entity.TimePart;
import ssu.eatssu.domain.menu.dto.MenuRequest.CreateMealRequest;
import ssu.eatssu.domain.menu.entity.Meal;
import ssu.eatssu.domain.restaurant.entity.Restaurant;

@RequiredArgsConstructor
public class MenuValidator {
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/ssu/eatssu/domain/restaurant/entity/Restaurant.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
package ssu.eatssu.domain.restaurant.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import ssu.eatssu.domain.menu.entity.Meal;
import ssu.eatssu.domain.menu.entity.Menu;
import ssu.eatssu.global.handler.response.BaseException;
import ssu.eatssu.global.handler.response.BaseResponseStatus;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Restaurant {
public enum Restaurant {
DODAM(RestaurantType.VARIABLE, "도담 식당", 6000),
DORMITORY(RestaurantType.VARIABLE, "기숙사 식당", 5000),
FOOD_COURT(RestaurantType.FIXED, "푸드 코트", null),
SNACK_CORNER(RestaurantType.FIXED, "스낵 코너", null),
HAKSIK(RestaurantType.VARIABLE, "학생 식당", 5000);

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "restaurant_id")
private Long id;
private RestaurantType restaurantType;
private String restaurantName;
private Integer price;

@Enumerated(EnumType.STRING)
private RestaurantName restaurantName;
@JsonCreator
public static Restaurant from(String restaurantName) {
return Arrays.stream(Restaurant.values())
.filter(r -> r.getRestaurantName().equals(restaurantName))
.findAny()
.orElseThrow(() -> new BaseException(BaseResponseStatus.NOT_FOUND_RESTAURANT));
}

@OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL)
private List<Menu> menus;
Restaurant(RestaurantType restaurantType, String description, Integer price) {
this.restaurantType = restaurantType;
this.restaurantName = description;
this.price = price;
}

@OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL)
private List<Meal> meals;
public boolean isFixed() {
return this.restaurantType == RestaurantType.FIXED;
}

public boolean isVariable() {
return this.restaurantType == RestaurantType.VARIABLE;
}
}

This file was deleted.

Loading

0 comments on commit 2cd2361

Please sign in to comment.