[EDIT]:図書貸出機能実装

This commit is contained in:
h-seya
2026-09-29 16:10:02 +09:00
parent 3a7b165e76
commit 6ca6d6399b
5 changed files with 89 additions and 18 deletions

View File

@@ -1,12 +1,15 @@
package com.example.demo.controller; package com.example.demo.controller;
import java.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.repository.EmployeeRepository; import com.example.demo.service.BookLendService;
import com.example.demo.service.BookServiceImpl; import com.example.demo.service.BookServiceImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -19,7 +22,7 @@ public class BookLendController {
private final BookServiceImpl bookServiceImpl; private final BookServiceImpl bookServiceImpl;
@Autowired @Autowired
private EmployeeRepository employeeRepository; private final BookLendService bookLendService;
@PostMapping("/bookLend") @PostMapping("/bookLend")
public String bookLend(Model model) { public String bookLend(Model model) {
@@ -28,11 +31,22 @@ public class BookLendController {
@PostMapping("/bookLend/{id}") @PostMapping("/bookLend/{id}")
public String bookLend(@PathVariable int id, Model model) { public String bookLend(@PathVariable int id, Model model) {
System.out.println(id);
model.addAttribute("book", bookServiceImpl.findById(id)); model.addAttribute("book", bookServiceImpl.findById(id));
return "bookLend"; return "bookLend";
} }
// 貸出処理
@PostMapping("/bookLend/confirm")
public String bookLendConfirm(
@RequestParam(name = "bookId") int bookId,
@RequestParam(name = "bookManageId") String bookManageId,
@RequestParam(name = "employeeId") int employeeId,
@RequestParam(name = "returnDate") LocalDate bookReturnDate,
Model model) {
bookLendService.save(bookId,bookManageId,employeeId,bookReturnDate);
return "redirect:/book";
}
} }

View File

@@ -1,6 +1,6 @@
package com.example.demo.entity; package com.example.demo.entity;
import java.sql.Date; import java.time.LocalDate;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
@@ -35,11 +35,11 @@ public class BookLend {
// 貸出日 // 貸出日
@Column(name = "book_lend_date", nullable = false) @Column(name = "book_lend_date", nullable = false)
private Date bookLendDate; private LocalDate bookLendDate;
// 返却日 // 返却日
@Column(name = "book_return_date", nullable = false) @Column(name = "book_return_date", nullable = false)
private Date bookReturnDate; private LocalDate bookReturnDate;
// 貸出状態 // 貸出状態
@Column(name = "book_status", nullable = false) @Column(name = "book_status", nullable = false)

View File

@@ -1,7 +1,27 @@
package com.example.demo.form; package com.example.demo.form;
import java.sql.Date;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import com.example.demo.entity.Book;
import com.example.demo.entity.Employee;
// 図書の貸出処理に使用予定 // 図書の貸出処理に使用予定
public class BookLendForm { public class BookLendForm {
private Book book;
@NotBlank(message = "社員IDの入力は必須です")
private Employee employeeBooklend;
private Date bookLendDate;
@NotNull(message = "返却日の入力は必須です")
private Date bookReturnDate;
private Integer bookStatus;
} }

View File

@@ -1,12 +1,17 @@
package com.example.demo.service; package com.example.demo.service;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.demo.entity.Book;
import com.example.demo.entity.BookLend; import com.example.demo.entity.BookLend;
import com.example.demo.entity.Employee;
import com.example.demo.repository.BookLendRepository; import com.example.demo.repository.BookLendRepository;
import com.example.demo.repository.BookRepository;
import com.example.demo.repository.EmployeeRepository;
@Service @Service
public class BookLendService { public class BookLendService {
@@ -14,8 +19,37 @@ public class BookLendService {
@Autowired @Autowired
private BookLendRepository bookLendRepository; private BookLendRepository bookLendRepository;
@Autowired
private BookRepository bookRepository;
@Autowired
private EmployeeRepository employeeRepository;
public List<BookLend> findAll() { public List<BookLend> findAll() {
return bookLendRepository.findAll(); return bookLendRepository.findAll();
} }
public void save(int bookId, String bookManageId, int employeeId,
LocalDate bookReturnDate) {
BookLend bookLend = new BookLend();
Book book = bookRepository.findById(bookId)
.orElseThrow(
() -> new IllegalArgumentException("対象の本が見つかりません"));
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(
() -> new IllegalArgumentException("対象のが見つかりません"));
bookLend.setBook(book);
bookLend.setEmployeeBooklend(employee);
bookLend.setBookLendDate(LocalDate.now());
bookLend.setBookReturnDate(bookReturnDate);
bookLend.setBookStatus(1);
bookLendRepository.save(bookLend);
}
} }

View File

@@ -20,7 +20,8 @@
図書情報 図書情報
</h2> </h2>
<form id="lendForm" action="bookLendConfirm" method="post"> <form th:action="@{/bookLend/confirm}" method="POST"
id="lendForm">
<div class="card-body"> <div class="card-body">
<table class="info-table"> <table class="info-table">
@@ -34,8 +35,8 @@
</thead> </thead>
<tbody> <tbody>
<tr th:each="book : ${book}"> <tr>
<td th:hidden="*{book.bookId}"></td> <!-- <td th:hidden="*{book.bookId}"></td>-->
<td th:text="*{book.bookManageId}"></td> <td th:text="*{book.bookManageId}"></td>
<td th:text="*{book.title}"></td> <td th:text="*{book.title}"></td>
<td th:text="*{book.author}"></td> <td th:text="*{book.author}"></td>
@@ -59,7 +60,7 @@
社員ID 社員ID
</label> </label>
<input type="text" id="employeeId" placeholder="社員IDを入力してください"> <input type="text" name="employeeId" placeholder="社員IDを入力してください">
</select> </select>
</div> </div>
@@ -79,7 +80,7 @@
</label> </label>
<p th:text="${#calendars.format(#calendars.createNowForTimeZone('Asia/Tokyo'), <p th:text="${#calendars.format(#calendars.createNowForTimeZone('Asia/Tokyo'),
'yyyy年MM月dd日')}"> 'yyyy年MM月dd日')}">
</p> </p>
</div> </div>
@@ -112,8 +113,10 @@
貸出処理を行うと、対象の図書のステータスが「貸出中」に変更されます。 貸出処理を行うと、対象の図書のステータスが「貸出中」に変更されます。
</p> </p>
<input type="hidden" name="bookManageId" th:value="${book.bookManageId}">
<input type="hidden" name="bookId" th:value="${book.bookId}">
<input type="submit" name="submit" onclick="submitLend()" value="貸出する"> <input type="submit" name="submit" onclick="submitLend()" value="貸出する">
<!-- <button type="button" class="btn btn-primary" onclick="submitLend()">-->
<input type="reset" name="reset" value="入力内容をクリア"> <input type="reset" name="reset" value="入力内容をクリア">