This commit is contained in:
s-tajihi
2026-09-29 09:56:22 +09:00
4 changed files with 63 additions and 8 deletions

View File

@@ -1,21 +1,59 @@
package com.example.demo.controller; package com.example.demo.controller;
import java.util.List;
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.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import com.example.demo.entity.Employee; import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository; import com.example.demo.repository.EmployeeRepository;
import com.example.demo.service.BookServiceImpl;
import com.example.demo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequiredArgsConstructor
public class BookLendController { public class BookLendController {
@Autowired
private final BookServiceImpl bookServiceImpl;
@Autowired @Autowired
private EmployeeRepository employeeRepository; private EmployeeRepository employeeRepository;
@PostMapping("/bookLend") @Autowired
public String bookLend(Model model) { private EmployeeService employeeService;
// @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/{id}")
public String bookLend(@PathVariable int id,Employee employee, Model model) {
// 図書ID取得
model.addAttribute("book", bookServiceImpl.findById(id));
// 社員ID取得(プルダウン用)
List<Employee> empList = employeeService.findAll();
model.addAttribute("bookLend", employeeService.findAll());
// model.addAttribute("employeeList", employeeRepository.findByEmployeeId(employee.getEmployeeId()));
// model.addAttribute("bookLend", employee);
return "bookLend"; return "bookLend";
} }

View File

@@ -15,7 +15,7 @@ public class BookLendService {
private BookLendRepository bookLendRepository; private BookLendRepository bookLendRepository;
public List<BookLend> findAll() { public List<BookLend> findAll() {
return bookLendRepository.findBybookStatus(1); return bookLendRepository.findAll();
} }
} }

View File

@@ -1,14 +1,20 @@
package com.example.demo.service; package com.example.demo.service;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import com.example.demo.entity.Book; import com.example.demo.entity.Book;
import com.example.demo.repository.BookRepository; import com.example.demo.repository.BookRepository;
import lombok.RequiredArgsConstructor;
@Service @Service
@RequiredArgsConstructor
public class BookServiceImpl { public class BookServiceImpl {
@Autowired @Autowired
@@ -22,4 +28,14 @@ public class BookServiceImpl {
return bookRepository.searchBooks(keyword); return bookRepository.searchBooks(keyword);
} }
public Book findById(int bookId) {
Optional<Book> book = bookRepository.findById(bookId);
if (book.isEmpty()) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "図書情報が見つかりません");
}
return book.get();
}
} }

View File

@@ -80,6 +80,7 @@
<tbody> <tbody>
<tr th:each="book : ${book}" th:if="${book.bookManageId != null}"> <tr th:each="book : ${book}" th:if="${book.bookManageId != null}">
<td th:hidden="*{book.bookId}"></td>
<td th:text="*{book.bookManageId}"></td> <td th:text="*{book.bookManageId}"></td>
<td th:text="*{book.isbn}"></td> <td th:text="*{book.isbn}"></td>
<td th:text="*{book.title}"></td> <td th:text="*{book.title}"></td>
@@ -106,8 +107,8 @@
<td th:if="${session.loginEmployee.adminFlag == 1}"> <td th:if="${session.loginEmployee.adminFlag == 1}">
<form method="post" action="/bookLend"> <form method="post" th:action="@{/bookLend/{id}(id=${book.bookId})}">
<input type="hidden" name="bookManageId" th:value="*{book.bookManageId}"> <input type="hidden" name="id" th:value="*{book.bookId}">
<button type="submit" <button type="submit"
th:disabled="*{book.bookLends != null && book.bookLends.bookStatus == 1}"> th:disabled="*{book.bookLends != null && book.bookLends.bookStatus == 1}">
貸出 貸出