-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book_Controller.java
64 lines (48 loc) · 1.38 KB
/
Book_Controller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.restful_demo;
//imports
@RestController
@RequestMapping("/api/books")
public class BookController {
private final List<Book> books = new ArrayList<Book>();
@GetMapping
public List<Book> getAllBooks() {
return books;
}
@GetMapping("/{id}")
public Book getBookById(@PathVariable Long id) {
return findBookById(id);
}
@PostMapping
public Book addBook(@RequestBody Book book) {
book.setId(generateBookId());
books.add(book);
return book;
}
@PutMapping("/{id}")
public Book updateBook(@PathVariable Long id, @RequestBody Book updatedBook) {
Book existingBook = findBookById(id);
if (existingBook != null) {
existingBook.setTitle(updatedBook.getTitle());
existingBook.setAuthor(updatedBook.getAuthor());
existingBook.setPublicationYear(updatedBook.getPublicationYear());
}
return existingBook;
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable Long id) {
Book bookToRemove = findBookById(id);
if (bookToRemove != null) {
books.remove(bookToRemove);
}
}
// Helper method to find a book by ID
private Book findBookById(Long id) {
for(Person p:al) {
if(p.getName().equalsIgnoreCase(name)) {
return p;
}else {
return null;
}
}
}
}