This commit is contained in:
s-tajihi
2026-09-29 09:56:38 +09:00
7 changed files with 40 additions and 50 deletions

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.BookInsert; import com.example.demo.entity.BookInsert;
import com.example.demo.repository.BookCategoryRepository; import com.example.demo.repository.BookCategoryRepository;
import com.example.demo.repository.BookInsertRepository;
import com.example.demo.repository.StorageLocationRepository; import com.example.demo.repository.StorageLocationRepository;
import com.example.demo.repository.SubCategoryRepository; import com.example.demo.repository.SubCategoryRepository;
import com.example.demo.service.BookInsertService; import com.example.demo.service.BookInsertService;
@@ -29,8 +30,13 @@ private SubCategoryRepository subCategoryRepository;
@Autowired @Autowired
private StorageLocationRepository storageLocationRepository; private StorageLocationRepository storageLocationRepository;
@Autowired
private BookInsertRepository bookInsertRepository;
@GetMapping("/bookInsert") @GetMapping("/bookInsert")
public String bookInsert(Model model) { public String bookInsert(Model model) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
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());
@@ -44,6 +50,7 @@ private StorageLocationRepository storageLocationRepository;
@PostMapping("/bookInsertConfirm") @PostMapping("/bookInsertConfirm")
public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) { public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) {
if(result.hasErrors()) { if(result.hasErrors()) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList",categoryRepository.findAll());
if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
@@ -56,7 +63,6 @@ private StorageLocationRepository storageLocationRepository;
return "bookInsert"; return "bookInsert";
} }
System.out.println(bookInsert);
bookInsertService.save(bookInsert); bookInsertService.save(bookInsert);
model.addAttribute("bookInsert",bookInsert); model.addAttribute("bookInsert",bookInsert);
return "bookInsertConfirm"; return "bookInsertConfirm";
@@ -65,8 +71,9 @@ private StorageLocationRepository storageLocationRepository;
//確認画面から「もどる」ボタンを押したら入力画面に戻る //確認画面から「もどる」ボタンを押したら入力画面に戻る
@PostMapping("/bookInsert") @PostMapping("/bookInsert")
public String bookInsertBack(BookInsert bookInsert,Model model) { public String bookInsertBack(BookInsert bookInsert,Model model) {
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll()); model.addAttribute("categoryList",categoryRepository.findAll());
if(bookInsert.getBookCategoryId() != null) { if(bookInsert.getBookCategoryId() !=null) {
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
} else{ } else{
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("subCategoryList",subCategoryRepository.findAll());
@@ -79,6 +86,7 @@ private StorageLocationRepository storageLocationRepository;
@PostMapping("/bookInsert/category") @PostMapping("/bookInsert/category")
public String changeCategory(BookInsert bookInsert,Model model) { public String changeCategory(BookInsert bookInsert,Model model) {
//カテゴリプルダウンに表示するデータを取得 //カテゴリプルダウンに表示するデータを取得
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
model.addAttribute("categoryList",categoryRepository.findAll()); 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("locationList",storageLocationRepository.findAll());
@@ -87,10 +95,9 @@ private StorageLocationRepository storageLocationRepository;
return "bookInsert"; return "bookInsert";
} }
}
}
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる //画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる
//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); //subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1)));

View File

@@ -28,8 +28,7 @@ public class BookInsert {
@Column(name = "title") @Column(name = "title")
private String title; private String title;
@Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$", @Pattern(regexp = "^(?=(?:.*-){4})[0-9-]{17}$",message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください")
message = "ISBNは半角数字(13文字)とハイフン(4つ)を含めて入力してください")
@Column(name = "isbn") @Column(name = "isbn")
private String isbn; private String isbn;
@@ -69,7 +68,7 @@ public class BookInsert {
@Column(name = "book_approval") @Column(name = "book_approval")
private Integer bookApproval; private Integer bookApproval;
@Transient //DB保存無効 @Transient
private Integer bookCategoryId; private Integer bookCategoryId;

View File

@@ -3,9 +3,13 @@ package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.entity.BookInsert; import com.example.demo.entity.BookInsert;
@Repository
public interface BookInsertRepository extends JpaRepository<BookInsert,Integer> { public interface BookInsertRepository extends JpaRepository<BookInsert,Integer> {
} BookInsert findFirstByOrderByBookIdDesc();//bookIDが一番大きいデータを取得
}

View File

@@ -8,4 +8,5 @@ public interface BookInsertService {
String regist(BookInsert book); String regist(BookInsert book);
List<BookInsert> findAll(); List<BookInsert> findAll();
void save(BookInsert bookInsert); void save(BookInsert bookInsert);
} String createBookManageId();
}

View File

@@ -15,6 +15,14 @@ import com.example.demo.repository.BookInsertRepository;
@Autowired @Autowired
private BookInsertRepository bookInsertRepository; private BookInsertRepository bookInsertRepository;
@Override
public String createBookManageId() { //次に表示する図書管理IDを作る
BookInsert book =bookInsertRepository.findFirstByOrderByBookIdDesc();
if(book ==null){
return "BK-1";}
return "BK-"+(book.getBookId()+1); //いまのIDの次のIDを返す
}
@Override @Override
public String regist(BookInsert book) { public String regist(BookInsert book) {
// DTO → エンティティへ詰め替えて保存 // DTO → エンティティへ詰め替えて保存
@@ -33,15 +41,14 @@ import com.example.demo.repository.BookInsertRepository;
bookInsert.setIsDeleted(0);//削除フラグ設定 bookInsert.setIsDeleted(0);//削除フラグ設定
bookInsert.setBookApproval(0);//承認フラグ(承認されていないため0) bookInsert.setBookApproval(0);//承認フラグ(承認されていないため0)
bookInsertRepository.save(bookInsert); bookInsertRepository.save(bookInsert);
//BK〇〇〇〇形式で管理IDを作る //BK-〇形式で管理IDを作る
String manageId="BK-" + bookInsert.getBookId();//BookId取得→BK形式に変換 String manageId="BK-"+bookInsert.getBookId();//BookId取得→BK形式に変換
bookInsert.setBookManageId(manageId); bookInsert.setBookManageId(manageId);
bookInsertRepository.save(bookInsert);//データベースに保存 bookInsertRepository.save(bookInsert);//データベースに保存
} }
} }
// ※BookService インタフェースにも List<Review> findAll(); を追加すること // ※BookService インタフェースにも List<Review> findAll(); を追加すること

View File

@@ -17,9 +17,7 @@
<label for="bookmanageId">図書管理ID</label><br> <label for="bookmanageId">図書管理ID</label><br>
<input type="text" <input type="text" id="bookManageId" th:value="${nextBookManageId}" readonly>
id="bookManageId"
placeholder="登録時に自動採番されます" readonly>
<div style="font-size:12px; color:rgb(128, 128, 128);"> <div style="font-size:12px; color:rgb(128, 128, 128);">
※例:BK-1、BK-2... ※例:BK-1、BK-2...
</div> </div>
@@ -109,11 +107,9 @@
<br><br> <br><br>
</label>--> </label>-->
<input type="submit" value="登録" onclick="return confirm('この内容で登録しますか?');">
<input type="submit" value="登録" class="btn">
</form> </form>
</body> </body>
</html> </html>

View File

@@ -10,30 +10,6 @@
<h1>登録内容確認</h1> <h1>登録内容確認</h1>
<!-- もどる: 入力中の値を持って入力画面へ -->
<form method="post" th:action="@{/bookInsert}" >
<input type="hidden" name="bookId" th:value="${bookInsert.bookId}">
<input type="hidden" name="bookManageId" th:value="${bookInsert.bookManageId}">
<input type="hidden" name="title" th:value="${bookInsert.title}">
<input type="hidden" name="isbn" th:value="${bookInsert.isbn}">
<input type="hidden" name="author" th:value="${bookInsert.author}">
<input type="hidden" name="bookCategoryId" th:value="${bookInsert.bookCategoryId}">
<input type="hidden" name="subcategory" th:value="${bookInsert.subcategory}">
<input type="hidden" name="publisher" th:value="${bookInsert.publisher}">
<input type="hidden" name="publicationDate" th:value="${bookInsert.publicationDate}">
<input type="hidden" name="location" th:value="${bookInsert.location}">
<input type="hidden" name="condition" th:value="${bookInsert.condition}">
<input type="hidden" name="donor" th:value="${bookInsert.donor}">
<input type="hidden" name="description" th:value="${bookInsert.description}">
<button type="submit">もどる</button>
</form>
</form>
<!-- 完了 -->
<form action="/book" method="GET">
<input type="submit" value="完了" class="btn">
</form>
<div id="registerModal" class="modal-overlay">
<div class="modal"> <div class="modal">
<div class="modal-message"> <div class="modal-message">