Merge remote-tracking branch 'origin/y-watanabe'

This commit is contained in:
h-seya
2026-09-29 16:15:20 +09:00

View File

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