Skip to content

Commit a5f7bbe

Browse files
committed
spring boot completed. still pretty confused though.
1 parent b50a774 commit a5f7bbe

File tree

1 file changed

+20
-7
lines changed
  • 11-spring-boot-and-mvc/spring-boot-and-mvc/src/main/java/org/example/Controllers

1 file changed

+20
-7
lines changed

11-spring-boot-and-mvc/spring-boot-and-mvc/src/main/java/org/example/Controllers/BookController.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,28 @@ public Book getBookById(@PathVariable("id") int id) {
2626
}
2727

2828
@PostMapping
29-
public void addBook(Book book) {
30-
throw new UnsupportedOperationException("Not implemented yet");
29+
public void addBook(@RequestBody Book book) {
30+
books.add(book);
3131
}
32-
33-
public void updateBook(int id, Book book) {
34-
throw new UnsupportedOperationException("Not implemented yet");
32+
@PutMapping("/{id}")
33+
public void updateBook(@PathVariable int id, @RequestBody Book book) {
34+
for (int i = 0; i < books.size(); i++) {
35+
if (books.get(i).getId() == id) {
36+
books.set(i, book); // Update the book in the list
37+
return;
38+
}
39+
}
3540
}
3641

37-
public void deleteBook(int id) {
38-
throw new UnsupportedOperationException("Not implemented yet");
42+
43+
@DeleteMapping("/{id}")
44+
public void deleteBook(@PathVariable int id) {
45+
Iterator<Book> iter = books.iterator();
46+
while (iter.hasNext()) {
47+
Book book = iter.next();
48+
if (book.getId() == id) {
49+
iter.remove();
50+
}
51+
}
3952
}
4053
}

0 commit comments

Comments
 (0)