Skip to content

Commit

Permalink
feat: 상점 메뉴 조회 API 구현 (#112)
Browse files Browse the repository at this point in the history
(cherry picked from commit 448cbf9)
  • Loading branch information
Invidam authored and Choi-JJunho committed May 9, 2024
1 parent 8fe1cf8 commit f4b0de7
Show file tree
Hide file tree
Showing 10 changed files with 643 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package in.koreatech.koin.controller.shop;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.dto.shop.ShopMenuResponse;
import in.koreatech.koin.service.shop.ShopService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class ShopController {

private final ShopService shopService;

@GetMapping("/shops/{shopId}/menus/{menuId}")
public ResponseEntity<ShopMenuResponse> findMenu(@PathVariable Long shopId, @PathVariable Long menuId) {
ShopMenuResponse shopMenu = shopService.findMenu(menuId);
return ResponseEntity.ok(shopMenu);
}
}
84 changes: 84 additions & 0 deletions src/main/java/in/koreatech/koin/domain/shop/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package in.koreatech.koin.domain.shop;

import java.util.ArrayList;
import java.util.List;

import in.koreatech.koin.domain.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "shop_menus")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Menu extends BaseEntity {

private static final int SINGLE_OPTION_COUNT = 1;

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "shop_id", nullable = false)
private Long shopId;

@Size(max = 255)
@NotNull
@Column(name = "name", nullable = false)
private String name;

@Size(max = 255)
@Column(name = "description")
private String description;

@NotNull
@Column(name = "is_hidden", nullable = false)
private Boolean isHidden = false;

@NotNull
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false;

@OneToMany(mappedBy = "menu", orphanRemoval = true, cascade = CascadeType.ALL)
private List<MenuCategoryMap> menuCategoryMaps = new ArrayList<>();

@OneToMany(mappedBy = "menu", orphanRemoval = true, cascade = CascadeType.ALL)
private List<MenuOption> menuOptions = new ArrayList<>();

@OneToMany(mappedBy = "menu", orphanRemoval = true, cascade = CascadeType.ALL)
private List<MenuImage> menuImages = new ArrayList<>();

@Builder
private Menu(Long shopId, String name, String description) {
this.shopId = shopId;
this.name = name;
this.description = description;
}

public boolean hasMultipleOption() {
return menuOptions.size() > SINGLE_OPTION_COUNT;
}

@Override
public String toString() {
return "Menu{" +
"id=" + id +
", shopId=" + shopId +
", name='" + name + '\'' +
'}';
}
}
53 changes: 53 additions & 0 deletions src/main/java/in/koreatech/koin/domain/shop/MenuCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package in.koreatech.koin.domain.shop;

import java.util.ArrayList;
import java.util.List;

import in.koreatech.koin.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "shop_menu_categories")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public final class MenuCategory extends BaseEntity {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "shop_id", nullable = false)
private Long shopId;

@Size(max = 255)
@NotNull
@Column(name = "name", nullable = false)
private String name;

@NotNull
@Column(name = "is_deleted", nullable = false)
private Boolean isDeleted = false;

@OneToMany(mappedBy = "menuCategory")
private List<MenuCategoryMap> menuCategoryMaps = new ArrayList<>();

@Builder
private MenuCategory(Long shopId, String name) {
this.shopId = shopId;
this.name = name;
}
}
70 changes: 70 additions & 0 deletions src/main/java/in/koreatech/koin/domain/shop/MenuCategoryMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package in.koreatech.koin.domain.shop;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "shop_menu_category_map")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MenuCategoryMap {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_menu_id")
private Menu menu;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "shop_menu_category_id")
private MenuCategory menuCategory;

public static MenuCategoryMap create() {
return new MenuCategoryMap();
}

public void map(Menu menu, MenuCategory menuCategory) {
setMenu(menu);
setMenuCategory(menuCategory);
}

private void setMenu(Menu menu) {
if (menu.equals(this.menu)) {
return;
}

if (this.menu != null) {
this.menu.getMenuCategoryMaps().remove(this);
}

this.menu = menu;
menu.getMenuCategoryMaps().add(this);
}

private void setMenuCategory(MenuCategory menuCategory) {
if (menuCategory.equals(this.menuCategory)) {
return;
}

if (this.menuCategory != null) {
this.menuCategory.getMenuCategoryMaps().remove(this);
}

this.menuCategory = menuCategory;
menuCategory.getMenuCategoryMaps().add(this);
}
}
55 changes: 55 additions & 0 deletions src/main/java/in/koreatech/koin/domain/shop/MenuImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package in.koreatech.koin.domain.shop;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "shop_menu_images")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MenuImage {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_menu_id")
private Menu menu;

@Size(max = 255)
@NotNull
@Column(name = "image_url", nullable = false)
private String imageUrl;

@Builder
private MenuImage(String imageUrl) {
this.imageUrl = imageUrl;
}

public void setMenu(Menu menu) {
if (menu.equals(this.menu)) {
return;
}

if (this.menu != null) {
this.menu.getMenuImages().remove(this);
}
this.menu = menu;
this.menu.getMenuImages().add(this);
}
}
60 changes: 60 additions & 0 deletions src/main/java/in/koreatech/koin/domain/shop/MenuOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package in.koreatech.koin.domain.shop;

import in.koreatech.koin.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "shop_menu_details")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MenuOption extends BaseEntity {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_menu_id")
private Menu menu;

@Size(max = 255)
@Column(name = "`option`")
private String option;

@NotNull
@Column(name = "price", nullable = false)
private Integer price;

@Builder
private MenuOption(String option, Integer price) {
this.option = option;
this.price = price;
}

public void setMenu(Menu menu) {
if (menu.equals(this.menu)) {
return;
}

if (this.menu != null) {
this.menu.getMenuOptions().remove(this);
}
this.menu = menu;
this.menu.getMenuOptions().add(this);
}
}
Loading

0 comments on commit f4b0de7

Please sign in to comment.