-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookController.java
More file actions
46 lines (38 loc) · 1.32 KB
/
BookController.java
File metadata and controls
46 lines (38 loc) · 1.32 KB
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
package com.example.librarymanagement.controller;
import com.example.librarymanagement.model.Book;
import com.example.librarymanagement.service.BookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
@ApiOperation("Get all books")
public List<Book> getAllBooks() {
return bookService.getAllBooks();
}
@GetMapping("/{id}")
@ApiOperation("Get a book by ID")
public Book getBookById(@PathVariable Long id) {
return bookService.getBookById(id).orElse(null);
}
@PostMapping
@ApiOperation("Add a new book")
public Book addBook(@RequestBody Book book) {
return bookService.addBook(book);
}
@PutMapping("/{id}")
@ApiOperation("Update a book")
public Book updateBook(@PathVariable Long id, @RequestBody Book updatedBook) {
return bookService.updateBook(id, updatedBook);
}
@DeleteMapping("/{id}")
@ApiOperation("Delete a book by ID")
public void deleteBook(@PathVariable Long id) {
bookService.deleteBook(id);
}
}