13 Commits

10 changed files with 210 additions and 141 deletions

View File

@@ -17,93 +17,80 @@ import com.example.demo.service.BookInsertService;
@Controller @Controller
public class BookInsertController { public class BookInsertController {
@Autowired
private BookInsertService bookInsertService;
@Autowired @Autowired
private BookCategoryRepository categoryRepository; private BookInsertService bookInsertService;
@Autowired @Autowired
private SubCategoryRepository subCategoryRepository; private BookCategoryRepository categoryRepository;
@Autowired @Autowired
private StorageLocationRepository storageLocationRepository; private SubCategoryRepository subCategoryRepository;
@Autowired @Autowired
private BookInsertRepository bookInsertRepository; private StorageLocationRepository storageLocationRepository;
@GetMapping("/bookInsert") @Autowired
public String bookInsert(Model model) { private BookInsertRepository bookInsertRepository;
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId());
model.addAttribute("bookInsert",new BookInsert()); @GetMapping("/bookInsert")
model.addAttribute("categoryList",categoryRepository.findAll()); public String bookInsert(Model model) {
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
model.addAttribute("locationList",storageLocationRepository.findAll()); model.addAttribute("bookInsert", new BookInsert());
model.addAttribute("bookInsert",new BookInsert()); model.addAttribute("categoryList", categoryRepository.findAll());
return "bookInsert"; model.addAttribute("subCategoryList", subCategoryRepository.findAll());
} model.addAttribute("locationList", storageLocationRepository.findAll());
model.addAttribute("bookInsert", new BookInsert());
// 初期表示: 空のformを渡す(th:fieldの初期値が空になる) return "bookInsert";
}
// 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ
@PostMapping("/bookInsertConfirm") // 初期表示: 空のformを渡す(th:fieldの初期値が空になる)
public String bookInsertConfirm(@Validated BookInsert bookInsert,BindingResult result,Model model) {
if(result.hasErrors()) { // 確認画面から戻る: POSTされた入力中の値がformに入ったまま入力画面へ
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); //入力内容確認してOKであれば確認画面へ
model.addAttribute("categoryList",categoryRepository.findAll()); @PostMapping("/bookInsertConfirm")
if(bookInsert.getBookCategoryId() != null) { //nullじゃなかったら public String bookInsertConfirm(@Validated BookInsert bookInsert, BindingResult result, Model model) {
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); if (result.hasErrors()) {
//そのカテゴリに属するサブカテゴリを持ってくる model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
} else{ model.addAttribute("categoryList", categoryRepository.findAll());
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら
} model.addAttribute("subCategoryList",
model.addAttribute("locationList",storageLocationRepository.findAll()); subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
model.addAttribute("bookInsert", bookInsert); //そのカテゴリに属するサブカテゴリを持ってくる
return "bookInsert"; } else {
model.addAttribute("subCategoryList", subCategoryRepository.findAll());
} }
bookInsertService.save(bookInsert); model.addAttribute("locationList",storageLocationRepository.findAll());
model.addAttribute("bookInsert",bookInsert); model.addAttribute("bookInsert", bookInsert);
return "bookInsertConfirm"; return "bookInsert";
}
}
//確認画面から「もどる」ボタンを押したら入力画面に戻る bookInsertService.save(bookInsert);
@PostMapping("/bookInsert") model.addAttribute("bookInsert", bookInsert);
public String bookInsertBack(BookInsert bookInsert,Model model) { return "bookInsertConfirm";
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); }
model.addAttribute("categoryList",categoryRepository.findAll());
if(bookInsert.getBookCategoryId() !=null) { @PostMapping("/bookInsert/category")
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); public String changeCategory(BookInsert bookInsert, Model model) {
} else{ //カテゴリプルダウンに表示するデータを取得
model.addAttribute("subCategoryList",subCategoryRepository.findAll()); model.addAttribute("nextBookManageId", bookInsertService.createBookManageId());
} model.addAttribute("categoryList", categoryRepository.findAll());
model.addAttribute("locationList",storageLocationRepository.findAll()); if (bookInsert.getBookCategoryId() != null) { //nullじゃなかったら
model.addAttribute("bookInsert",bookInsert); model.addAttribute("subCategoryList",
return "bookInsert"; subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId()));
} //そのカテゴリに属するサブカテゴリを持ってくる
} else {
@PostMapping("/bookInsert/category") model.addAttribute("subCategoryList", subCategoryRepository.findAll());
public String changeCategory(BookInsert bookInsert,Model model) { }
//カテゴリプルダウンに表示するデータを取得 model.addAttribute("locationList", storageLocationRepository.findAll());
model.addAttribute("nextBookManageId",bookInsertService.createBookManageId()); model.addAttribute("bookInsert", bookInsert);
model.addAttribute("categoryList",categoryRepository.findAll());
model.addAttribute("subCategoryList",subCategoryRepository.findByBookCategory_BookCategoryId(bookInsert.getBookCategoryId())); return "bookInsert";
model.addAttribute("locationList",storageLocationRepository.findAll()); }
model.addAttribute("bookInsert", bookInsert);
return "bookInsert";
}
} }
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる
//画面で選択したカテゴリbookInsert.getBookCategoryId()に選択した結果をいれる //subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1)));
//subCategoryRepository.findBookCategory_BookCategoryId(bookInsert.getBookCategoryId(1))); //「カテゴリIDが1のサブカテゴリを取ってくる」
//「カテゴリIDが1のサブカテゴリを取ってくる」 //model.addAttribute("subCategoryList",取得結果)
//model.addAttribute("subCategoryList",取得結果)

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;
@@ -14,25 +17,36 @@ import lombok.RequiredArgsConstructor;
@Controller @Controller
@RequiredArgsConstructor @RequiredArgsConstructor
public class BookLendController { public class BookLendController {
@Autowired
private final BookServiceImpl bookServiceImpl;
@Autowired @Autowired
private EmployeeRepository employeeRepository; private final BookServiceImpl bookServiceImpl;
@Autowired
private final BookLendService bookLendService;
@PostMapping("/bookLend") @PostMapping("/bookLend")
public String bookLend(Model model) { public String bookLend(Model model) {
return "bookLend"; return "bookLend";
} }
@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

@@ -74,7 +74,7 @@ public class LoginController {
AccountEntity accountEntity = loginService.login(loginForm); AccountEntity accountEntity = loginService.login(loginForm);
if (accountEntity == null) { if (accountEntity == null) {
model.addAttribute("loginError", model.addAttribute("loginError",
"ログインIDまたはパスワードが正しくありません"); "社員IDまたはパスワードが正しくありません");
return "login"; return "login";
} }

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,22 +1,33 @@
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 org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
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 {
@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.findBybookStatus(1);
} }
@@ -31,4 +42,27 @@ public class BookLendService {
bookLendRepository.save(booklend); bookLendRepository.save(booklend);
} }
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

@@ -12,6 +12,12 @@
図書貸出処理 図書貸出処理
</h1> </h1>
<div class="float-end">
<a th:href="@{/topmenu}">
トップメニューへ
</a>
</div>
<p> <p>
以下の図書を貸出します。貸出先の社員IDと返却予定日を入力してください。 以下の図書を貸出します。貸出先の社員IDと返却予定日を入力してください。
</p> </p>
@@ -20,7 +26,7 @@
図書情報 図書情報
</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 +40,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 +65,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 +85,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 +118,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="入力内容をクリア">

View File

@@ -10,53 +10,57 @@
<body> <body>
<h1>貸出中図書一覧</h1> <h1>貸出中図書一覧</h1>
<div class="float-end">
<table> <a th:href="@{/topmenu}">トップメニューへ</a>
</div>
<thead>
<tr>
<th>社員ID</th>
<th>社員名</th>
<th>管理ID</th>
<th>タイトル</th>
<th>返却予定日</th>
</tr>
</thead>
<tbody> <table>
<tr th:each="booklist : ${list}">
<td th:text="*{booklist.employeeBooklend.employeeId}"></td> <thead>
<td th:text="*{booklist.employeeBooklend.employeeName}"></td> <tr>
<td th:text="*{booklist.book.bookManageId}"></td> <th>社員ID</th>
<td th:text="*{booklist.book.title}"></td> <th>社員名</th>
<td th:text="*{booklist.bookReturnDate}"></td> <th>管理ID</th>
<th>タイトル</th>
<th>返却予定日</th>
</tr>
</thead>
<td> <tbody>
<form th:action="@{/return-book}" method="get"> <tr th:each="booklist : ${list}">
<button type="submit" name="lendId" th:value="${booklist.lendId}"
onclick="return MoveCheck();">返却
</button>
</form>
</td>
</tr> <td th:text="*{booklist.employeeBooklend.employeeId}"></td>
<td th:text="*{booklist.employeeBooklend.employeeName}"></td>
</tbody> <td th:text="*{booklist.book.bookManageId}"></td>
<td th:text="*{booklist.book.title}"></td>
</table> <td th:text="*{booklist.bookReturnDate}"></td>
<script> <td>
<form th:action="@{/return-book}" method="get">
<button type="submit" name="lendId" th:value="${booklist.lendId}"
onclick="return MoveCheck();">返却
</button>
</form>
</td>
function MoveCheck() { </tr>
return window.confirm("返却処理を実行します。よろしいですか?");
}
</script> </tbody>
</table>
<script>
function MoveCheck() {
return window.confirm("返却処理を実行します。よろしいですか?");
}
</script>
</body> </body>

View File

@@ -4,6 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>ログイン</title> <title>ログイン</title>
</head> </head>
<body> <body>
@@ -16,7 +17,7 @@
<form th:action="@{/login}" th:object="${loginForm}" method="post"> <form th:action="@{/login}" th:object="${loginForm}" method="post">
<div class="form-group"> <div class="form-group">
<label for="employeeId">社員ID</label> <label for="employeeId">社員ID</label>
<input id="employeeId" type="text" th:field="*{employeeId}"> <input id="employeeId" type="text" th:field="*{employeeId}"pattern="^[0-9]+$"title="数値以外は入力しないでください">
<p class="field-error" th:errors="*{employeeId}"></p> <p class="field-error" th:errors="*{employeeId}"></p>
</div> </div>

View File

@@ -97,6 +97,7 @@
</a> </a>
<hr> <hr>
</th>
<a class="card" href="/passwordChange"> <a class="card" href="/passwordChange">
<h3>パスワード変更</h3> <h3>パスワード変更</h3>
@@ -105,7 +106,7 @@
初期パスワードを<br> 変更します。 初期パスワードを<br> 変更します。
</p> </p>
</a> </a>
</th>
</body> </body>