Skip to content

Commit

Permalink
fix: 리뷰 요청사항에 따른 수정 (service 분리, PutMapping 변경)
Browse files Browse the repository at this point in the history
  • Loading branch information
juno-junho committed Sep 19, 2023
1 parent f220a27 commit 180d8d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.ray.pominowner.menu.controller.dto.MenuRequest;
import com.ray.pominowner.menu.domain.MenuImage;
import com.ray.pominowner.menu.service.MenuImageService;
import com.ray.pominowner.menu.service.MenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -24,17 +26,19 @@ public class MenuController {

private final MenuService menuService;

private final MenuImageService menuImageService;

@PostMapping(value = "/stores/{storeId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> registerMenu(@RequestPart MenuRequest request, @RequestPart MultipartFile image, @PathVariable Long storeId) {
MenuImage menuImage = menuService.createImage(image);
MenuImage menuImage = menuImageService.saveImage(image);
Long menuId = menuService.registerMenu(request.generateMenuEntity(menuImage));

final String url = "/api/v1/menus/%d/stores/%d".formatted(menuId, storeId);

return ResponseEntity.created(URI.create(url)).build();
}

@PostMapping(value = "/{menuId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PutMapping(value = "/{menuId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void updateMenu(@RequestPart MenuRequest request, @RequestPart MultipartFile image, @PathVariable Long menuId) {
MenuImage menuImage = menuService.createImage(image);
menuService.updateMenu(request.generateMenuEntity(menuImage), menuId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ray.pominowner.menu.repository.MenuImageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Component
Expand All @@ -19,6 +20,7 @@ public class MenuImageService {

private final MenuImageRepository menuImageRepository;

@Transactional
public MenuImage saveImage(MultipartFile image) {
final String rootPath = imagePathProvider.getMenuImageRootPath();
FileInfo fileInfo = fileSaver.retrieveFileInfoAfterSavingFile(image, rootPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ray.pominowner.menu.service.MenuImageService;
import com.ray.pominowner.menu.service.MenuService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -13,6 +14,8 @@
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.nio.charset.StandardCharsets;
import java.util.Map;
Expand All @@ -39,6 +42,9 @@ class MenuControllerTest {
@MockBean
private MenuService menuService;

@MockBean
private MenuImageService menuImageService;

private MockMultipartFile image;

private MockMultipartFile menuRequest;
Expand Down Expand Up @@ -89,7 +95,14 @@ void successUpdateMenu() throws Exception {
doNothing().when(menuService).updateMenu(any(), any());


mvc.perform(multipart("/api/v1/menus/1")
// when, then
MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart("/api/v1/menus/1");
builder.with(request -> {
request.setMethod("PUT");
return request;
});

mvc.perform(builder
.file(menuRequest)
.file(image)
.contentType(MediaType.MULTIPART_FORM_DATA)
Expand Down

0 comments on commit 180d8d7

Please sign in to comment.