diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index 6ed1d7b..e3d5b2f 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -5,11 +5,14 @@ import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import com.example.demo.entity.BookInsert; import com.example.demo.repository.BookCategoryRepository; +import com.example.demo.repository.StorageLocationRepository; import com.example.demo.repository.SubCategoryRepository; import com.example.demo.service.BookInsertService; @@ -21,13 +24,18 @@ private BookInsertService bookInsertService; @Autowired private BookCategoryRepository categoryRepository; + @Autowired private SubCategoryRepository subCategoryRepository; +@Autowired +private StorageLocationRepository storageLocationRepository; + @GetMapping("/bookInsert") public String bookInsert(Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findAll()); + model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert",new BookInsert()); return "bookInsert"; } @@ -36,17 +44,37 @@ private SubCategoryRepository subCategoryRepository; // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ @PostMapping("/bookInsertConfirm") - public String bookInsertConfirm(BookInsert bookInsert,Model model) { + public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { + if(result.hasErrors()) { + model.addAttribute("categoryList",categoryRepository.findAll()); + if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + //そのカテゴリに属するサブカテゴリを持ってくる + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert", bookInsert); + return "bookInsert"; + + } + System.out.println(bookInsert); bookInsertService.save(bookInsert); model.addAttribute("bookInsert",bookInsert); return "bookInsertConfirm"; } + //確認画面から「もどる」ボタンを押したら入力画面に戻る @PostMapping("/bookInsert") public String bookInsertBack(BookInsert bookInsert,Model model) { model.addAttribute("categoryList",categoryRepository.findAll()); - model.addAttribute("subCategoryList",new ArrayList<>()); - model.addAttribute("bookInsert",bookInsert); + if(bookInsert.getBookCategoryId() != null) { + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + } else{ + model.addAttribute("subCategoryList",new ArrayList<>()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert",bookInsert); return "bookInsert"; } @@ -54,18 +82,22 @@ private SubCategoryRepository subCategoryRepository; public String changeCategory(BookInsert bookInsert,Model model) { //カテゴリプルダウンに表示するデータを取得 model.addAttribute("categoryList",categoryRepository.findAll()); - - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", bookInsert); return "bookInsert"; } + } - //画面で選択したカテゴリbookInsert.getBookCategoryId()) + //画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる + //subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); + //「カテゴリIDが1のサブカテゴリを取ってくる」 + //model.addAttribute("subCategoryList",取得結果) diff --git a/src/main/java/com/example/demo/entity/BookInsert.java b/src/main/java/com/example/demo/entity/BookInsert.java index ffe074a..be1fd47 100644 --- a/src/main/java/com/example/demo/entity/BookInsert.java +++ b/src/main/java/com/example/demo/entity/BookInsert.java @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Transient; +import jakarta.validation.constraints.Pattern; import lombok.Data; @@ -27,6 +28,8 @@ public class BookInsert { @Column(name = "title") private String title; + @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$", + message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください") @Column(name = "isbn") private String isbn; @@ -40,7 +43,7 @@ public class BookInsert { private String publisher; @Column(name = "publication_date") - private String publicationDate; + private LocalDate publicationDate; @Column(name = "location_id") private Integer location; @@ -66,7 +69,7 @@ public class BookInsert { @Column(name = "book_approval") private Integer bookApproval; - @Transient + @Transient //DB保存無効 private Integer bookCategoryId; diff --git a/src/main/java/com/example/demo/repository/StorageLocationRepository.java b/src/main/java/com/example/demo/repository/StorageLocationRepository.java new file mode 100644 index 0000000..861a62a --- /dev/null +++ b/src/main/java/com/example/demo/repository/StorageLocationRepository.java @@ -0,0 +1,9 @@ +package com.example.demo.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.example.demo.entity.StorageLocation; + +public interface StorageLocationRepository extends JpaRepository { + +} \ No newline at end of file diff --git a/src/main/resources/templates/bookInsert.html b/src/main/resources/templates/bookInsert.html index 467e577..be4be18 100644 --- a/src/main/resources/templates/bookInsert.html +++ b/src/main/resources/templates/bookInsert.html @@ -8,17 +8,12 @@

新規図書登録入力

- -


- +

新しく図書を登録します。必要事項を入力し、「登録」ボタンを押してください

+ ※は必須項目です +

-
- -




-
+


-
- +
+ +
+


-
+


+
- + +

-
+


- -
- + +
+

-
- -

- -
- -

- -
- +

+ +
+ +

+ +
+ +


@@ -103,7 +99,7 @@ --> - +
diff --git a/src/main/resources/templates/bookInsertConfirm.html b/src/main/resources/templates/bookInsertConfirm.html index 9a53f47..882221c 100644 --- a/src/main/resources/templates/bookInsertConfirm.html +++ b/src/main/resources/templates/bookInsertConfirm.html @@ -17,7 +17,7 @@ - +