diff --git a/src/main/java/com/example/demo/controller/BookLendController.java b/src/main/java/com/example/demo/controller/BookLendController.java index 6683951..9d1b1dc 100644 --- a/src/main/java/com/example/demo/controller/BookLendController.java +++ b/src/main/java/com/example/demo/controller/BookLendController.java @@ -1,24 +1,62 @@ package com.example.demo.controller; +import java.util.List; + 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 com.example.demo.entity.Employee; import com.example.demo.repository.EmployeeRepository; +import com.example.demo.service.BookServiceImpl; +import com.example.demo.service.EmployeeService; + +import lombok.RequiredArgsConstructor; @Controller +@RequiredArgsConstructor public class BookLendController { + + @Autowired + private final BookServiceImpl bookServiceImpl; @Autowired private EmployeeRepository employeeRepository; + + @Autowired + private EmployeeService employeeService; - @PostMapping("/bookLend") - public String bookLend(Model model) { +// @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 empList = employeeService.findAll(); + + model.addAttribute("bookLend", employeeService.findAll()); + +// model.addAttribute("employeeList", employeeRepository.findByEmployeeId(employee.getEmployeeId())); +// model.addAttribute("bookLend", employee); + return "bookLend"; } - + // 社員IDを取得してプルダウンに表示させる @PostMapping("/bookLend/employeeId") public String selectEmployeeId(Employee employee, Model model) { diff --git a/src/main/java/com/example/demo/service/BookLendService.java b/src/main/java/com/example/demo/service/BookLendService.java index 962345d..a416702 100644 --- a/src/main/java/com/example/demo/service/BookLendService.java +++ b/src/main/java/com/example/demo/service/BookLendService.java @@ -15,7 +15,7 @@ public class BookLendService { private BookLendRepository bookLendRepository; public List findAll() { - return bookLendRepository.findBybookStatus(1); + return bookLendRepository.findAll(); } } diff --git a/src/main/java/com/example/demo/service/BookServiceImpl.java b/src/main/java/com/example/demo/service/BookServiceImpl.java index da7268a..0b6c00d 100644 --- a/src/main/java/com/example/demo/service/BookServiceImpl.java +++ b/src/main/java/com/example/demo/service/BookServiceImpl.java @@ -1,14 +1,20 @@ package com.example.demo.service; import java.util.List; +import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; import com.example.demo.entity.Book; import com.example.demo.repository.BookRepository; +import lombok.RequiredArgsConstructor; + @Service +@RequiredArgsConstructor public class BookServiceImpl { @Autowired @@ -17,9 +23,19 @@ public class BookServiceImpl { public List findAll() { return bookRepository.findAll(); } - - public List findBooks(String keyword){ + + public List findBooks(String keyword) { return bookRepository.searchBooks(keyword); } + public Book findById(int bookId) { + Optional book = bookRepository.findById(bookId); + if (book.isEmpty()) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "図書情報が見つかりません"); + } + + return book.get(); + } + } diff --git a/src/main/resources/templates/book.html b/src/main/resources/templates/book.html index 38231ea..dc8d5d0 100644 --- a/src/main/resources/templates/book.html +++ b/src/main/resources/templates/book.html @@ -80,6 +80,7 @@ + @@ -106,8 +107,8 @@ -
- + +