Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 상점 메뉴 조회 API 구현 #112

Merged
merged 17 commits into from
Dec 24, 2023
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
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

중간테이블이군요...
ManyToMany 관계라 어쩔수 없으려나요


@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`")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option의 Column 이름을 `option` 으로 한 이유가 있나요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JPA Buddy 활용한 것 뿐인데, 버그가 있었떤 것 같네요..

앞으로 JPA Buddy 이용해 코드 생성하더라도, 한 번씩 살펴보겠습니다~

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
Loading