Skip to content

Commit

Permalink
feat: 메뉴 등록 기능 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
djdongjae committed Jun 11, 2024
1 parent 12d214d commit fc7b614
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.skhu.tastyinventory_be.controller.inventory;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.skhu.tastyinventory_be.common.dto.BaseResponse;
Expand Down Expand Up @@ -27,7 +28,7 @@ public class InventoryController {
public BaseResponse<?> createInventory(
@RequestParam("inventoryName") String name,
@RequestParam("inventoryUnit") Unit unit,
@RequestParam("inventoryImage") MultipartFile image
@RequestPart("inventoryImage") MultipartFile image
) {
inventoryService.createInventory(name, unit, image);
return BaseResponse.success(SuccessCode.INVENTORY_CREATE_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.skhu.tastyinventory_be.controller.menu;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.skhu.tastyinventory_be.common.dto.BaseResponse;
import net.skhu.tastyinventory_be.controller.menu.dto.request.MenuRequestDto;
import net.skhu.tastyinventory_be.exception.SuccessCode;
import net.skhu.tastyinventory_be.service.MenuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/menu")
@RestController
public class MenuController {
private final MenuService menuService;

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public BaseResponse<?> createMenu(
@RequestPart("image") MultipartFile image,
@RequestPart("data") MenuRequestDto requestDto
) {
menuService.createMenu(image, requestDto);
return BaseResponse.success(SuccessCode.MENU_CREATE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.skhu.tastyinventory_be.controller.menu.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class MenuRequestDto {
@NotBlank(message = "이름을 입력하세요.")
private String name;

private List<RelatedInventoryRequestDto> relatedInventory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.skhu.tastyinventory_be.controller.menu.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class RelatedInventoryRequestDto {
@NotNull(message = "재고 아이디를 입력하세요.")
private Long inventoryId;

@NotNull(message = "재고 사용량을 입력하세요.")
private Long inventoryUsage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum SuccessCode {
INVENTORY_CREATE_SUCCESS(HttpStatus.CREATED, "재고 생성을 완료하였습니다"),
EMPLOYEE_CREATE_SUCCESS(HttpStatus.CREATED, "직원 생성이 완료되었습니다"),
SALARY_CREATE_SUCCESS(HttpStatus.CREATED, "급여 생성이 완료되었습니다"),
MENU_CREATE_SUCCESS(HttpStatus.CREATED, "메뉴 생성이 완료되었습니다"),

/**
* 204 NO_CONTENT
Expand Down
64 changes: 47 additions & 17 deletions src/main/java/net/skhu/tastyinventory_be/service/MenuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import lombok.RequiredArgsConstructor;
import net.skhu.tastyinventory_be.controller.inventory.dto.response.InventoryResponseDto;
import net.skhu.tastyinventory_be.controller.menu.dto.request.MenuRequestDto;
import net.skhu.tastyinventory_be.controller.menu.dto.response.MenuResponseDto;
import net.skhu.tastyinventory_be.domain.inventory.InventoryRepository;
import net.skhu.tastyinventory_be.domain.menu.Menu;
import net.skhu.tastyinventory_be.domain.menu.MenuRepository;
import net.skhu.tastyinventory_be.domain.recipe.Recipe;
import net.skhu.tastyinventory_be.domain.recipe.RecipeRepository;
import net.skhu.tastyinventory_be.exception.ErrorCode;
import net.skhu.tastyinventory_be.exception.model.NotFoundException;
import net.skhu.tastyinventory_be.external.client.aws.S3Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,39 +26,64 @@
public class MenuService {
private final MenuRepository menuRepository;
private final RecipeRepository recipeRepository;
private final InventoryRepository inventoryRepository;
private final S3Service s3Service;

public List<MenuResponseDto> findAll() {
@Transactional
public void createMenu(MultipartFile image, MenuRequestDto requestDto) {
String imageUrl = s3Service.uploadImage(image, "menu");

Menu menu = Menu.builder().imageUrl(imageUrl).name(requestDto.getName()).build();

menuRepository.save(menu);

List<Recipe> recipes = requestDto.getRelatedInventory().stream()
.map(
r -> Recipe.builder()
.usage(r.getInventoryUsage())
.menu(menu)
.inventory(inventoryRepository.findById(
r.getInventoryId()).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_INVENTORY_EXCEPTION,
ErrorCode.NOT_FOUND_INVENTORY_EXCEPTION.getMessage()))
).build()).toList();


recipes.stream().map(recipeRepository::save).close();
}

public List<MenuResponseDto> findAllMenu() {
List<Menu> menuList = menuRepository.findAll();
List<MenuResponseDto> result = new ArrayList<>();
for (Menu menu : menuList) {
result.add(MenuResponseDto.of(
menu.getName(),
menu.getImageUrl(),
recipeRepository.findAllByMenu(menu)
.stream()
.map(recipe -> InventoryResponseDto.from(recipe.getInventory()))
.collect(Collectors.toList())
));
result.add(
MenuResponseDto.of(
menu.getName(),
menu.getImageUrl(),
recipeRepository.findAllByMenu(menu).stream()
.map(
recipe -> InventoryResponseDto.from(
recipe.getInventory()))
.collect(Collectors.toList())));
}
return result;
}

public MenuResponseDto findOne(Long menuId) {
public MenuResponseDto findOneMenu(Long menuId) {
Menu menu = menuRepository.findById(menuId).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_MENU_EXCEPTION,
ErrorCode.NOT_FOUND_MENU_EXCEPTION.getMessage()
)
);
));

return MenuResponseDto.of(
menu.getName(),
menu.getImageUrl(),
recipeRepository.findAllByMenu(menu)
.stream()
.map(recipe -> InventoryResponseDto.from(recipe.getInventory()))
.collect(Collectors.toList())
);
recipeRepository.findAllByMenu(menu).stream().map(
recipe -> InventoryResponseDto.from(
recipe.getInventory()))
.collect(Collectors.toList()));
}

}

0 comments on commit fc7b614

Please sign in to comment.