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

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

View File

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