Skip to content

Commit ab09a14

Browse files
Added New Functionality on Delete Book By Id And Update the existing Book instead of creating new Book Object Added Dropdown
1 parent c11ec3f commit ab09a14

12 files changed

Lines changed: 189 additions & 49 deletions

File tree

src/main/java/com/bookstore/bookstore/Controller/BookController.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.springframework.web.bind.annotation.ModelAttribute;
1111
import org.springframework.web.bind.annotation.PathVariable;
1212
import org.springframework.web.bind.annotation.PostMapping;
13-
import org.springframework.web.bind.annotation.RequestParam;
14-
1513
import com.bookstore.bookstore.Model.Book;
1614
import com.bookstore.bookstore.service.BookService;
1715

@@ -63,22 +61,32 @@ public String deleteBookForm(Model model) {
6361
// }
6462

6563
@PostMapping("/delete")
66-
public String deleteBookByTitle(@RequestParam String title) {
67-
bookService.deleteBookByTitle(title);
64+
public String deleteBookById(Book book) {
65+
bookService.deleteBookById(book);
6866
return "redirect:/";
6967
}
7068

7169
// For Updatting the Book attributes(Price,stocks,etc.)
72-
@GetMapping("/update")
73-
public String updateBookForm(Model model) {
74-
model.addAttribute("book", new Book());
70+
@GetMapping("/update/{id}")
71+
public String updateBookForm(@PathVariable Long id, Model model) {
72+
Optional<Book> existingBook = bookService.findById(id);
73+
if (existingBook.isEmpty()) {
74+
return "redirect:/select-book"; // book not found
75+
}
76+
model.addAttribute("book", existingBook.get());
7577
return "updatebook";
7678
}
7779

7880
@PostMapping("/update")
79-
public String updateBookByTitle(@ModelAttribute Book book) {
80-
bookService.updateBookByTitle(book);
81-
return "redirect:/";
81+
public String updateBookById(Book book) {
82+
bookService.updateBookById(book);
83+
return "redirect:/admin";
84+
}
85+
86+
@GetMapping("/select-book")
87+
public String selectBookPage(Model model) {
88+
model.addAttribute("books", bookService.getAllBooks());
89+
return "selectbook";
8290
}
8391

8492
@GetMapping("/add-to-cart/{id}")
@@ -90,7 +98,7 @@ public String addToCart(@PathVariable Long id, HttpSession session) {
9098
}
9199

92100
// Get book from DB
93-
Optional<Book> optionalBook = bookService.getBookById(id);
101+
Optional<Book> optionalBook = bookService.findById(id);
94102
if (optionalBook.isPresent()) {
95103
cart.add(optionalBook.get());
96104
session.setAttribute("cart", cart);
@@ -119,4 +127,15 @@ public String viewCart(HttpSession session, Model model) {
119127
return "cart";
120128
}
121129

130+
@GetMapping("/booklist")
131+
public String getListBook(Model model) {
132+
List<Book> books = bookService.getAllBooks();
133+
System.out.println("📚 Total books fetched: " + books.size());
134+
for (Book b : books) {
135+
System.out.println("📷 Book: " + b.getTitle() + ", Image: " + b.getImageUrl());
136+
}
137+
model.addAttribute("books", books);
138+
return "booklist";
139+
}
140+
122141
}

src/main/java/com/bookstore/bookstore/Model/Book.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public class Book {
2828
private String imageUrl;
2929

3030
private int stock;
31+
3132
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.bookstore.bookstore.repository;
22

3-
import java.util.List;
4-
import java.util.Optional;
5-
63
import org.springframework.data.jpa.repository.JpaRepository;
74
// Make sure the Book class exists at this location; if not, update the import to the correct package:
85
import com.bookstore.bookstore.Model.Book;
96

107
public interface BookRepository extends JpaRepository<Book, Long> {
11-
Optional<Book> findByTitle(String title); // if title is unique
12-
13-
// OR if you allow multiple same titles
14-
List<Book> findAllByTitle(String title);
8+
159

1610
}

src/main/java/com/bookstore/bookstore/service/BookService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public interface BookService {
1010

1111
void saveBook(Book book); // ✅ TO Add New Book
1212

13-
// void deleteBookById(Long id); // ✅ To Delete a Book by Id
13+
void deleteBookById(Book book); // ✅ To Delete a Book by Id
1414

15-
void deleteBookByTitle(String title); //to delete By title
15+
void updateBookById(Book book); // ✅To update
1616

17-
void updateBookByTitle(Book book);
18-
Optional<Book> getBookById(Long id);
17+
Optional<Book> findById(Long id); // ✅ TO get THe list of book by Id
1918

19+
2020
}

src/main/java/com/bookstore/bookstore/service/BookServiceImpl.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,16 @@ public void saveBook(Book book) {
2424
bookRepository.save(book);
2525
}
2626

27-
// @Override
28-
// public void deleteBookById(Long id){
29-
// bookRepository.deleteById(id);
30-
// }
27+
3128

3229
@Override
33-
public void deleteBookByTitle(String title) {
34-
Optional<Book> bookOptional = bookRepository.findByTitle(title);
35-
if (bookOptional.isPresent()) {
36-
bookRepository.delete(bookOptional.get());
37-
} else {
38-
System.out.println("❌ Book not found with title: " + title);
39-
// throw exception or show error message if needed
40-
}
30+
public void deleteBookById(Book book) {
31+
bookRepository.delete(book);
4132
}
4233

4334
@Override
44-
public void updateBookByTitle(Book book) {
45-
Book existingBook = bookRepository.findByTitle(book.getTitle())
35+
public void updateBookById(Book book) {
36+
Book existingBook = bookRepository.findById(book.getId())
4637
.orElseThrow(() -> new RuntimeException("Book not found"));
4738

4839
// Update fields only if user provided
@@ -62,9 +53,12 @@ public void updateBookByTitle(Book book) {
6253
bookRepository.save(existingBook);
6354
}
6455

56+
57+
6558
@Override
66-
public Optional<Book> getBookById(Long id) {
67-
return bookRepository.findById(id);
59+
public Optional<Book> findById(Long id) {
60+
Optional<Book> book= bookRepository.findById(id);
61+
return book;
6862
}
6963

7064
}

src/main/resources/templates/addbook.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h2>Add New Book</h2>
4646
<label>Image URL</label>
4747
<input type="text" class="form-control" th:field="*{imageUrl}">
4848
</div>
49-
<button type="submit" class="btn btn-success">Add Book</button>
49+
<button href="admin.html" type="submit" class="btn btn-success">Add Book</button>
5050
</form>
5151

5252
</body>

src/main/resources/templates/admin.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<!DOCTYPE html>
22
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
3+
34
<head>
45
<title>Admin Panel - Online Bookstore</title>
5-
6+
67
<!-- Bootstrap CSS -->
78
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
8-
9+
910
<!-- Optional Font (Google) -->
1011
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
11-
12+
1213
<style>
1314
body {
1415
font-family: 'Poppins', sans-serif;
@@ -72,8 +73,12 @@ <h1 class="admin-title">Admin Panel</h1>
7273
<a href="/delete" class="btn btn-outline-danger w-100">
7374
<span class="emoji">🗑️</span> Delete A Book
7475
</a>
75-
<a href="/update" class="btn btn-outline-primary w-100">
76-
<span class="emoji">🔄</span> Update A Book
76+
<a href="/select-book" class="btn btn-outline-primary mb-3">🔄 Update A Book</a>
77+
78+
79+
</a>
80+
<a href="/booklist" class="btn btn-outline-primary w-100">
81+
<span class="emoji">🔄</span>List of Books
7782
</a>
7883
</div>
7984
</div>
@@ -83,4 +88,4 @@ <h1 class="admin-title">Admin Panel</h1>
8388
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
8489
</body>
8590

86-
</html>
91+
</html>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
3+
4+
<head>
5+
<title>List of Books For HTML</title>
6+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
7+
<style>
8+
.admin-title {
9+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
10+
font-size: 2.5rem;
11+
font-weight: 700;
12+
color: #2c3e50;
13+
text-align: center;
14+
padding: 20px;
15+
margin-bottom: 30px;
16+
background: linear-gradient(135deg, #00b894, #0984e3);
17+
color: white;
18+
border-radius: 12px;
19+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
20+
letter-spacing: 1px;
21+
text-transform: uppercase;
22+
}
23+
</style>
24+
</head>
25+
26+
<body>
27+
<h1 class="admin-title">Admin Panel</h1>
28+
<div class="container">
29+
30+
<table class="table table-bordered table-striped">
31+
<thead>
32+
<tr>
33+
<th>ID</th>
34+
<th>Title</th>
35+
<th>Author</th>
36+
<th>Price</th>
37+
<th>Stock</th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
<tr th:each="book:${books}">
42+
<td th:text="${book.id}"></td>
43+
<td th:text="${book.title}"></td>
44+
<td th:text="${book.author}"></td>
45+
<td th:text="${book.price}"></td>
46+
<td th:text="${book.stock}"></td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
</div>
51+
</body>
52+
53+
</html>

src/main/resources/templates/deletebook.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ <h2>Delete A Book</h2>
3535
</form> -->
3636

3737
<form th:action="@{/delete}" method="post">
38-
<input type="text" name="title" class="form-control" placeholder="Enter Book Title" required>
39-
<button type="submit" class="btn btn-danger mt-2">Delete by Title</button>
38+
<input type="number" name="Id" class="form-control" placeholder="Enter Book Id" required>
39+
<button type="submit" class="btn btn-danger mt-2">Delete by ID</button>
4040
</form>
4141

4242

src/main/resources/templates/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
<div class="row">
3535
<div th:each="book : ${books}" class="col-md-4 mb-4">
36+
3637
<div class="card h-100 shadow-sm">
3738
<img th:src="${book.imageUrl} ?: '/images/default.jpg'" class="card-img-top img-fluid"
3839
alt="Book Image" style="height: 200px; object-fit: cover;" />
@@ -51,8 +52,7 @@ <h5 th:text="${book.title}" class="card-title"></h5>
5152

5253
</div>
5354
</div>
54-
<!-- Visible only to ADMIN -->
55-
55+
5656

5757
<!-- Visible to both USER & ADMIN -->
5858
<div sec:authorize="hasAnyRole('USER', 'ADMIN')">

0 commit comments

Comments
 (0)