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

[POM-94] feat: image 관련 mappedSuperclass로 분리, menu 도메인 엔티티 validation 및 전체적 구조 설정 #43

Merged
merged 8 commits into from
Sep 18, 2023
23 changes: 23 additions & 0 deletions src/main/java/com/ray/pominowner/global/domain/BaseFileEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ray.pominowner.global.domain;

import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.util.Assert;

@Getter
@MappedSuperclass
public abstract class BaseFileEntity extends BaseTimeEntity {

protected String path;

protected String uploadName;

protected String fileName;

protected void validateImage(String path, String uploadName, String fileName) {
Assert.hasText(path, "경로는 빈 값일 수 없습니다.");
Assert.hasText(uploadName, "파일 이름은 빈 값일 수 없습니다.");
Assert.hasText(fileName, "파일 이름은 빈 값일 수 없습니다.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ray.pominowner.menu.controller;

import com.ray.pominowner.menu.controller.dto.MenuInfoRequest;
import com.ray.pominowner.menu.domain.MenuImage;
import com.ray.pominowner.menu.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/menu/")
public class MenuController {

private final MenuService menuService;

@PostMapping("{storeId}")
public ResponseEntity<Void> registerMenu(@RequestBody MenuInfoRequest request, @PathVariable Long storeId) {
MenuImage image = menuService.createImage(request.image());
Long menuId = menuService.registerMenu(request.generateMenuEntityWithImage(image));

return ResponseEntity.created(URI.create("/api/v1/menu/" + storeId + "/" + menuId)).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ray.pominowner.menu.controller.dto;

import com.ray.pominowner.menu.domain.Menu;
import com.ray.pominowner.menu.domain.MenuImage;
import org.springframework.web.multipart.MultipartFile;

public record MenuInfoRequest(String name, MultipartFile image, String info, int price, Long storeId) {

public Menu generateMenuEntityWithImage(MenuImage image) {

return Menu.builder()
.name(name)
.info(info)
.price(price)
.menuImageId(image.getId())
.storeId(storeId)
.build();
}

}
37 changes: 33 additions & 4 deletions src/main/java/com/ray/pominowner/menu/domain/Menu.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.ray.pominowner.menu.domain;

import com.ray.pominowner.global.domain.BaseTimeEntity;
import com.ray.pominowner.global.util.ExceptionMessage;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import static com.ray.pominowner.global.util.Validator.validate;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Menu extends BaseTimeEntity {

@Id
Expand All @@ -16,14 +24,35 @@ public class Menu extends BaseTimeEntity {

private String name;

private String image;

private String info;

private Integer listOrder;
private Long menuImageId;

private Long storeId;

private int price;

private Long storeId;
// private Integer listOrder; // 추후 수정 예정

@Builder
public Menu(String name, String info, int price, Long menuImageId, Long storeId) {
validateMenu(name, info, price);

this.name = name;
this.menuImageId = menuImageId;
this.info = info;
this.price = price;
this.storeId = storeId;
}

private void validateMenu(String name, String info, int price) {
Assert.hasText(name, "메뉴 형식이 올바르지 않습니다.");
Assert.hasText(info, "메뉴 형식이 올바르지 않습니다.");
validate(price < 0, ExceptionMessage.valueOf("메뉴 형식이 올바르지 않습니다."));
}

public Long getId() {
return id;
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/ray/pominowner/menu/domain/MenuImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ray.pominowner.menu.domain;

import com.ray.pominowner.global.domain.BaseFileEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MenuImage extends BaseFileEntity {

@Id
@Column(name = "MENU_IMAGE_ID")
@GeneratedValue
private Long id;

@Builder
private MenuImage(String path, String uploadName, String fileName) {
validateImage(path, uploadName, fileName);

this.path = path;
this.uploadName = uploadName;
this.fileName = fileName;
}

public Long getId() {
return id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ray.pominowner.menu.repository;

import com.ray.pominowner.menu.domain.Menu;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MenuRepository extends JpaRepository<Menu, Long> {

}
26 changes: 26 additions & 0 deletions src/main/java/com/ray/pominowner/menu/service/MenuService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ray.pominowner.menu.service;

import com.ray.pominowner.menu.domain.Menu;
import com.ray.pominowner.menu.domain.MenuImage;
import com.ray.pominowner.menu.repository.MenuRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@RequiredArgsConstructor
public class MenuService {

private final MenuRepository menuRepository;

public Long registerMenu(Menu menu) {
// 구현 예정
return menuRepository.save(menu).getId();
}

public MenuImage createImage(MultipartFile image) {
// 구현 예정
return null;
}

}
20 changes: 4 additions & 16 deletions src/main/java/com/ray/pominowner/store/domain/StoreImage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ray.pominowner.store.domain;

import com.ray.pominowner.global.domain.BaseTimeEntity;
import com.ray.pominowner.global.domain.BaseFileEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -9,45 +9,33 @@
import jakarta.persistence.ManyToOne;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@NoArgsConstructor(access = PROTECTED)
public class StoreImage extends BaseTimeEntity {
public class StoreImage extends BaseFileEntity {

@Id
@Column(name = "STORE_IAMGE_ID")
@Column(name = "STORE_IMAGE_ID")
@GeneratedValue
private Long id;

private String path;

private String uploadName;

private String fileName;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "STORE_ID")
private Store store;

@Builder
private StoreImage(String path, String uploadName, String fileName, Store store) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 private 생성자는 어디에서 호출 되나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

호출 안되고 builder로만 사용하려고 막아놓은거에요

validateImage(path, uploadName, fileName);

this.path = path;
this.uploadName = uploadName;
this.fileName = fileName;
this.store = store;
}

private void validateImage(String path, String uploadName, String fileName) {
Assert.hasText(path, "경로는 빈 값일 수 없습니다.");
Assert.hasText(uploadName, "파일 이름은 빈 값일 수 없습니다.");
Assert.hasText(fileName, "파일 이름은 빈 값일 수 없습니다.");
}

public Store getStore() {
return store;
}
Expand Down