[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;
import java.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
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 lombok.RequiredArgsConstructor;
@@ -14,25 +17,36 @@ import lombok.RequiredArgsConstructor;
@Controller
@RequiredArgsConstructor
public class BookLendController {
@Autowired
private final BookServiceImpl bookServiceImpl;
@Autowired
private EmployeeRepository employeeRepository;
private final BookServiceImpl bookServiceImpl;
@Autowired
private final BookLendService bookLendService;
@PostMapping("/bookLend")
public String bookLend(Model model) {
return "bookLend";
}
@PostMapping("/bookLend/{id}")
public String bookLend(@PathVariable int id, Model model) {
System.out.println(id);
model.addAttribute("book", bookServiceImpl.findById(id));
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;
import java.sql.Date;
import java.time.LocalDate;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
@@ -35,11 +35,11 @@ public class BookLend {
// 貸出日
@Column(name = "book_lend_date", nullable = false)
private Date bookLendDate;
private LocalDate bookLendDate;
// 返却日
@Column(name = "book_return_date", nullable = false)
private Date bookReturnDate;
private LocalDate bookReturnDate;
// 貸出状態
@Column(name = "book_status", nullable = false)

View File

@@ -1,7 +1,27 @@
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 {
private Book book;
@NotBlank(message = "社員IDの入力は必須です")
private Employee employeeBooklend;
private Date bookLendDate;
@NotNull(message = "返却日の入力は必須です")
private Date bookReturnDate;
private Integer bookStatus;
}

View File

@@ -1,21 +1,55 @@
package com.example.demo.service;
import java.time.LocalDate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.Book;
import com.example.demo.entity.BookLend;
import com.example.demo.entity.Employee;
import com.example.demo.repository.BookLendRepository;
import com.example.demo.repository.BookRepository;
import com.example.demo.repository.EmployeeRepository;
@Service
public class BookLendService {
@Autowired
private BookLendRepository bookLendRepository;
@Autowired
private BookRepository bookRepository;
@Autowired
private EmployeeRepository employeeRepository;
public List<BookLend> 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>
<form id="lendForm" action="bookLendConfirm" method="post">
<form th:action="@{/bookLend/confirm}" method="POST"
id="lendForm">
<div class="card-body">
<table class="info-table">
@@ -34,8 +35,8 @@
</thead>
<tbody>
<tr th:each="book : ${book}">
<td th:hidden="*{book.bookId}"></td>
<tr>
<!-- <td th:hidden="*{book.bookId}"></td>-->
<td th:text="*{book.bookManageId}"></td>
<td th:text="*{book.title}"></td>
<td th:text="*{book.author}"></td>
@@ -59,7 +60,7 @@
社員ID
</label>
<input type="text" id="employeeId" placeholder="社員IDを入力してください">
<input type="text" name="employeeId" placeholder="社員IDを入力してください">
</select>
</div>
@@ -79,7 +80,7 @@
</label>
<p th:text="${#calendars.format(#calendars.createNowForTimeZone('Asia/Tokyo'),
'yyyy年MM月dd日')}">
'yyyy年MM月dd日')}">
</p>
</div>
@@ -111,9 +112,11 @@
<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="貸出する">
<!-- <button type="button" class="btn btn-primary" onclick="submitLend()">-->
<input type="reset" name="reset" value="入力内容をクリア">