Skip to content

Commit

Permalink
[refactor] add path variable validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Nov 12, 2024
1 parent 7b89dc7 commit bc4b04e
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.hankki.hankkiserver.api.menu.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.dto.HankkiResponse;
import org.hankki.hankkiserver.api.menu.service.MenuCommandService;
Expand All @@ -11,32 +13,41 @@
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.controller.request.MenuPostRequest;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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;

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

private final MenuCommandService menuCommandService;

@DeleteMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id) {
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") @Min(value = 1L) final long storeId,
@PathVariable("id") @Min(value = 1L) final long id) {
menuCommandService.deleteMenu(MenuDeleteCommand.of(storeId, id));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}

@PatchMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id,
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") @Min(value = 1L) final long storeId,
@PathVariable("id") @Min(value = 1L) final long id,
@Valid @RequestBody final MenuPostRequest request) {
menuCommandService.modifyMenu(MenuPatchCommand.of(storeId, id, request.name(), request.price()));
return HankkiResponse.success(CommonSuccessCode.OK);
}

@PostMapping("{storeId}/menus/bulk")
public HankkiResponse<MenusPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final List<MenuPostRequest> request) {
public HankkiResponse<MenusPostResponse> createMenu(@PathVariable @Min(value = 1L) final long storeId,
@Valid @RequestBody final List<MenuPostRequest> request) {
List<MenuPostCommand> command = request.stream()
.map(r -> MenuPostCommand.of(r.name(), r.price()))
.toList();
Expand Down

0 comments on commit bc4b04e

Please sign in to comment.