-
Notifications
You must be signed in to change notification settings - Fork 24
/
BookResource.java
executable file
·78 lines (65 loc) · 3.01 KB
/
BookResource.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package example.controller;
import example.dto.BookDto;
import example.service.BookService;
import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
import javax.validation.Valid;
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class BookResource {
private final BookService bookService;
@Autowired
public BookResource(BookService bookService) {
this.bookService = bookService;
}
@Operation(summary = "Gets the list of available books")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found the books",
content = { @Content(mediaType = "application/json",
schema = @Schema(implementation = List.class)) }),
})
@GetMapping(path = "/books", produces = {"application/json"})
public ResponseEntity<List<BookDto>> list() {
log.info("GET /api/v1/books");
return ResponseEntity.ok(bookService.list());
}
@Operation(summary = "Get a book by its id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found the book",
content = { @Content(mediaType = "application/json",
schema = @Schema(implementation = BookDto.class)) }),
@ApiResponse(responseCode = "400", description = "Invalid id supplied",
content = @Content),
@ApiResponse(responseCode = "404", description = "Book not found",
content = @Content) })
@GetMapping(path = "/books/{id}", produces = {"application/json"})
public ResponseEntity<BookDto> get(@PathVariable("id") Long bookId) {
log.info("GET /api/v1/books/"+bookId);
return ResponseEntity.ok(bookService.get(bookId));
}
@Operation(summary = "Creates a new book")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Created the book"),
@ApiResponse(responseCode = "400", description = "Bad request",
content = @Content),
@ApiResponse(responseCode = "500", description = "Internal Server Error",
content = @Content) })
@PostMapping(path = "/books", consumes = {"application/json"})
public ResponseEntity<Void> create(@Valid @RequestBody BookDto bookDto) {
log.info("POST /api/v1/books : "+bookDto);
Long bookId = bookService.create(bookDto);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(bookId).toUri();
return ResponseEntity.created(location).build();
}
}