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 73c9046 commit e5b572c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BaseResponse<List<InventoryResponseDto>> findAllByNameContaining(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE,
path = "/{id}")
@ResponseStatus(HttpStatus.CREATED)
@ResponseStatus(HttpStatus.OK)
public BaseResponse<?> updateInventory(
@RequestParam("inventoryName") String name,
@RequestParam("inventoryUnit") Unit unit,
Expand All @@ -64,7 +64,7 @@ public BaseResponse<?> updateInventory(
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseStatus(HttpStatus.OK)
public BaseResponse<?> deleteInventory(@PathVariable Long id) {
inventoryService.deleteInventory(id);
return BaseResponse.success(SuccessCode.INVENTORY_DELETE_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ public BaseResponse<MenuDetailResponseDto> findOne(@PathVariable Long id) {
final MenuDetailResponseDto data = menuService.findOneMenu(id);
return BaseResponse.success(SuccessCode.GET_SUCCESS, data);
}

@PatchMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE,
path = "/{id}"
)
@ResponseStatus(HttpStatus.OK)
public BaseResponse<?> updateMenu(
@RequestPart("image") MultipartFile image,
@RequestPart("data") MenuRequestDto requestDto,
@PathVariable Long id
) {
menuService.updateMenu(id, image, requestDto);
return BaseResponse.success(SuccessCode.MENU_PATCH_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public Menu(String name, String imageUrl) {
this.name = name;
this.imageUrl = imageUrl;
}

public void update(String name, String imageUrl) {
this.name = name;
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface RecipeRepository extends JpaRepository<Recipe, Long> {
List<Recipe> findAllByMenu(Menu menu);
void deleteAllByMenu(Menu menu);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public enum SuccessCode {
LOGIN_SUCCESS(HttpStatus.OK, "로그인에 성공했습니다."),
LOGOUT_SUCCESS(HttpStatus.OK, "로그아웃에 성공했습니다."),
INVENTORY_GET_SUCCESS(HttpStatus.OK, "재고 조회에 성공했습니다."),
INVENTORY_DELETE_SUCCESS(HttpStatus.OK, "재고 삭제에 성공했습니다."),
INVENTORY_PATCH_SUCCESS(HttpStatus.OK, "재고 수정이 완료되었습니다."),
MENU_PATCH_SUCCESS(HttpStatus.OK, "메뉴 수정이 완료되었습니다."),

/**
* 201 CREATED
Expand All @@ -36,8 +39,6 @@ public enum SuccessCode {
SALARY_DELETE_SUCCESS(HttpStatus.NO_CONTENT, "급여 삭제에 성공했습니다"),
EMPLOYEE_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "직원 정보 수정이 완료되었습니다"),
SALARY_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "급여 정보 수정이 완료되었습니다"),
INVENTORY_DELETE_SUCCESS(HttpStatus.NO_CONTENT, "재고 삭제에 성공했습니다."),
INVENTORY_PATCH_SUCCESS(HttpStatus.NO_CONTENT, "재고 수정이 완료되었습니다.")
;


Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/skhu/tastyinventory_be/service/MenuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,34 @@ public MenuDetailResponseDto findOneMenu(Long menuId) {
return MenuDetailResponseDto.of(menu.getId(), menu.getName(), menu.getImageUrl(), relatedInventories);
}

@Transactional
public void updateMenu(Long id, MultipartFile image, MenuRequestDto requestDto) {
Menu menu = menuRepository.findById(id).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_MENU_EXCEPTION,
ErrorCode.NOT_FOUND_MENU_EXCEPTION.getMessage()
)
);

recipeRepository.deleteAllByMenu(menu);

s3Service.deleteFile(menu.getImageUrl());
String imageUrl = s3Service.uploadImage(image, "menu");

menu.update(requestDto.getName(), imageUrl);

requestDto.getRelatedInventories().stream()
.map(
r -> Recipe.builder()
.usages(r.getInventoryUsage())
.menu(menu)
.inventory(inventoryRepository.findById(
r.getInventoryId()).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_INVENTORY_EXCEPTION,
ErrorCode.NOT_FOUND_INVENTORY_EXCEPTION.getMessage()))
).build()
).forEach(recipeRepository::save);
}

}

0 comments on commit e5b572c

Please sign in to comment.