diff --git a/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java new file mode 100644 index 00000000..8dd3b518 --- /dev/null +++ b/springboot_validation/src/main/java/hello/itemservice/web/validation/ValidationItemControllerV2.java @@ -0,0 +1,93 @@ +package hello.itemservice.web.validation; + +import hello.itemservice.domain.item.Item; +import hello.itemservice.domain.item.ItemRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.util.StringUtils; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Controller +@RequestMapping("/validation/v2/items") +@RequiredArgsConstructor +public class ValidationItemControllerV2 { + + private final ItemRepository itemRepository; + + @GetMapping + public String items(Model model) { + List items = itemRepository.findAll(); + model.addAttribute("items", items); + return "validation/v2/items"; + } + + @GetMapping("/{itemId}") + public String item(@PathVariable long itemId, Model model) { + Item item = itemRepository.findById(itemId); + model.addAttribute("item", item); + return "validation/v2/item"; + } + + @GetMapping("/add") + public String addForm(Model model) { + model.addAttribute("item", new Item()); + return "validation/v2/addForm"; + } + + @PostMapping("/add") + public String addItemV1(@ModelAttribute Item item, BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) { + + // 검증 로직 + if (!StringUtils.hasText(item.getItemName())) { + bindingResult.addError(new FieldError("item", "itemName", "상품 이름은 필수 입니다.")); + } + if (item.getPrice() == null || item.getPrice() < 1000 || item.getPrice() > 1000000) { + bindingResult.addError(new FieldError("item", "price", "가격은 1,000 ~ 1,000,000 까지 허용합니다.")); + } + if (item.getQuantity() == null || item.getQuantity() >= 9999) { + bindingResult.addError(new FieldError("item", "quantity", "수량은 최대 9,999 까지 허용됩니다.")); + + } + if (item.getPrice() != null && item.getQuantity() != null ) { + int resultPrice = item.getPrice() * item.getQuantity(); + if (resultPrice < 10000) { + bindingResult.addError(new ObjectError("item", "가격 * 수량의 합은 10,000원 이상 이어야 합니다. 현재 값 = " + resultPrice)); + } + } + + // 검증 실패시 + if (bindingResult.hasErrors()) { + return "validation/v2/addForm"; + } + + // 검증 성공시 + Item savedItem = itemRepository.save(item); + redirectAttributes.addAttribute("itemId", savedItem.getId()); + redirectAttributes.addAttribute("status", true); + return "redirect:/validation/v2/items/{itemId}"; + } + + @GetMapping("/{itemId}/edit") + public String editForm(@PathVariable Long itemId, Model model) { + Item item = itemRepository.findById(itemId); + model.addAttribute("item", item); + return "validation/v2/editForm"; + } + + @PostMapping("/{itemId}/edit") + public String edit(@PathVariable Long itemId, @ModelAttribute Item item) { + itemRepository.update(itemId, item); + return "redirect:/validation/v2/items/{itemId}"; + } + +} + diff --git a/springboot_validation/src/main/resources/templates/validation/v2/addForm.html b/springboot_validation/src/main/resources/templates/validation/v2/addForm.html new file mode 100644 index 00000000..aeba7535 --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v2/addForm.html @@ -0,0 +1,73 @@ + + + + + + + + + +
+ +
+

상품 등록

+
+ +
+ +
+

글로벌 오류 메시지

+
+
+ + +
+ 상품명 오류 +
+
+
+ + +
+ 가격 오류 +
+
+
+ + +
+ 수량 오류 +
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v2/editForm.html b/springboot_validation/src/main/resources/templates/validation/v2/editForm.html new file mode 100644 index 00000000..85ab748e --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v2/editForm.html @@ -0,0 +1,57 @@ + + + + + + + + + +
+ +
+

상품 수정

+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v2/item.html b/springboot_validation/src/main/resources/templates/validation/v2/item.html new file mode 100644 index 00000000..3a2de3c4 --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v2/item.html @@ -0,0 +1,60 @@ + + + + + + + + + +
+ +
+

상품 상세

+
+ + +

+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+ + \ No newline at end of file diff --git a/springboot_validation/src/main/resources/templates/validation/v2/items.html b/springboot_validation/src/main/resources/templates/validation/v2/items.html new file mode 100644 index 00000000..62becbb4 --- /dev/null +++ b/springboot_validation/src/main/resources/templates/validation/v2/items.html @@ -0,0 +1,49 @@ + + + + + + + + +
+
+

상품 목록

+
+ +
+
+ +
+
+ +
+
+ + + + + + + + + + + + + + + + + +
ID상품명가격수량
회원id상품명1000010
+
+ +
+ + + \ No newline at end of file