diff --git a/src/main/java/com/example/demo/controller/BookInsertController.java b/src/main/java/com/example/demo/controller/BookInsertController.java index 9d3faf8..2b1829e 100644 --- a/src/main/java/com/example/demo/controller/BookInsertController.java +++ b/src/main/java/com/example/demo/controller/BookInsertController.java @@ -17,93 +17,80 @@ import com.example.demo.service.BookInsertService; @Controller public class BookInsertController { - -@Autowired -private BookInsertService bookInsertService; -@Autowired -private BookCategoryRepository categoryRepository; + @Autowired + private BookInsertService bookInsertService; -@Autowired -private SubCategoryRepository subCategoryRepository; + @Autowired + private BookCategoryRepository categoryRepository; -@Autowired -private StorageLocationRepository storageLocationRepository; + @Autowired + private SubCategoryRepository subCategoryRepository; -@Autowired -private BookInsertRepository bookInsertRepository; + @Autowired + private StorageLocationRepository storageLocationRepository; - @GetMapping("/bookInsert") - public String bookInsert(Model model) { - model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); - model.addAttribute("bookInsert",new BookInsert()); - model.addAttribute("categoryList",categoryRepository.findAll()); - model.addAttribute("subCategoryList",subCategoryRepository.findAll()); - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert",new BookInsert()); - return "bookInsert"; - } - - // 初期表示: 空のformを渡す(th:fieldの初期値が空になる) - - // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ - @PostMapping("/bookInsertConfirm") - public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { - if(result.hasErrors()) { - model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); - model.addAttribute("categoryList",categoryRepository.findAll()); - if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); - //そのカテゴリに属するサブカテゴリを持ってくる - } else{ - model.addAttribute("subCategoryList",subCategoryRepository.findAll()); - } - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert", bookInsert); - return "bookInsert"; - - } - bookInsertService.save(bookInsert); - model.addAttribute("bookInsert",bookInsert); - return "bookInsertConfirm"; - } - - //確認画面から「もどる」ボタンを押したら入力画面に戻る - @PostMapping("/bookInsert") - public String bookInsertBack(BookInsert bookInsert,Model model) { - model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); - model.addAttribute("categoryList",categoryRepository.findAll()); - if(bookInsert.getBookCategoryId() !=null) { - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); - } else{ - model.addAttribute("subCategoryList",subCategoryRepository.findAll()); - } - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert",bookInsert); - return "bookInsert"; - } - - @PostMapping("/bookInsert/category") - public String changeCategory(BookInsert bookInsert,Model model) { - //カテゴリプルダウンに表示するデータを取得 - model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); - model.addAttribute("categoryList",categoryRepository.findAll()); - model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); - model.addAttribute("locationList",storageLocationRepository.findAll()); - model.addAttribute("bookInsert", bookInsert); + @Autowired + private BookInsertRepository bookInsertRepository; + + @GetMapping("/bookInsert") + public String bookInsert(Model model) { + model.addAttribute("nextBookManageId", bookInsertService.createBookManageId()); + model.addAttribute("bookInsert", new BookInsert()); + model.addAttribute("categoryList", categoryRepository.findAll()); + model.addAttribute("subCategoryList", subCategoryRepository.findAll()); + model.addAttribute("locationList", storageLocationRepository.findAll()); + model.addAttribute("bookInsert", new BookInsert()); + return "bookInsert"; + } + + // 初期表示: 空のformを渡す(th:fieldの初期値が空になる) + + // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ + //入力内容確認してOKであれば確認画面へ + @PostMapping("/bookInsertConfirm") + public String bookInsertConfirm(@Validated BookInsert bookInsert, BindingResult result, Model model) { + if (result.hasErrors()) { + model.addAttribute("nextBookManageId", bookInsertService.createBookManageId()); + model.addAttribute("categoryList", categoryRepository.findAll()); + if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら + model.addAttribute("subCategoryList", + subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + //そのカテゴリに属するサブカテゴリを持ってくる + } else { + model.addAttribute("subCategoryList", subCategoryRepository.findAll()); + } + model.addAttribute("locationList",storageLocationRepository.findAll()); + model.addAttribute("bookInsert", bookInsert); + return "bookInsert"; + + } + bookInsertService.save(bookInsert); + model.addAttribute("bookInsert", bookInsert); + return "bookInsertConfirm"; + } + + @PostMapping("/bookInsert/category") + public String changeCategory(BookInsert bookInsert, Model model) { + //カテゴリプルダウンに表示するデータを取得 + model.addAttribute("nextBookManageId", bookInsertService.createBookManageId()); + model.addAttribute("categoryList", categoryRepository.findAll()); + if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら + model.addAttribute("subCategoryList", + subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); + //そのカテゴリに属するサブカテゴリを持ってくる + } else { + model.addAttribute("subCategoryList", subCategoryRepository.findAll()); + } + model.addAttribute("locationList", storageLocationRepository.findAll()); + model.addAttribute("bookInsert", bookInsert); + + return "bookInsert"; + } - return "bookInsert"; - } - } - - - //画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる - //subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); - //「カテゴリIDが1のサブカテゴリを取ってくる」 - //model.addAttribute("subCategoryList",取得結果) - - - - +//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる +//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); +//「カテゴリIDが1のサブカテゴリを取ってくる」 +//model.addAttribute("subCategoryList",取得結果)