3030
3131@ RestController
3232@ Tag (name = "Books" )
33- @ OpenAPIDefinition (
34- info = @ Info (
35- title = "java.samples.spring.boot" ,
36- version = "1.0" ,
37- description = "🧪 Proof of Concept for a RESTful Web Service made with JDK 21 (LTS) and Spring Boot 3" ,
38- contact = @ Contact (
39- name = "GitHub" ,
40- url = "https://github.com/nanotaboada/java.samples.spring.boot"
41- ),
42- license = @ License (
43- name = "MIT License" ,
44- url = "https://opensource.org/licenses/MIT"
45- )
46- )
47- )
33+ @ OpenAPIDefinition (info = @ Info (title = "java.samples.spring.boot" , version = "1.0" , description = "🧪 Proof of Concept for a RESTful Web Service made with JDK 21 (LTS) and Spring Boot 3" , contact = @ Contact (name = "GitHub" , url = "https://github.com/nanotaboada/java.samples.spring.boot" ), license = @ License (name = "MIT License" , url = "https://opensource.org/licenses/MIT" )))
4834public class BooksController {
4935
50- private final BooksService service ;
36+ private final BooksService booksService ;
5137
52- public BooksController (BooksService service ) {
53- this .service = service ;
38+ public BooksController (BooksService booksService ) {
39+ this .booksService = booksService ;
5440 }
5541
56- /* --------------------------------------------------------------------------------------------
42+ /*
43+ * -------------------------------------------------------------------------
5744 * HTTP POST
58- * ----------------------------------------------------------------------------------------- */
45+ * -------------------------------------------------------------------------
46+ */
5947
6048 @ PostMapping ("/books" )
6149 @ Operation (summary = "Creates a new book" )
6250 @ ApiResponses (value = {
63- @ ApiResponse (responseCode = "201" , description = "Created" , content = @ Content ),
64- @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
65- @ ApiResponse (responseCode = "409" , description = "Conflict" , content = @ Content )
51+ @ ApiResponse (responseCode = "201" , description = "Created" , content = @ Content ),
52+ @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
53+ @ ApiResponse (responseCode = "409" , description = "Conflict" , content = @ Content )
6654 })
6755 public ResponseEntity <String > post (@ RequestBody BookDTO bookDTO ) {
68- if (service .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
56+ if (booksService .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
6957 return new ResponseEntity <>(HttpStatus .CONFLICT );
7058 } else {
71- if (service .create (bookDTO )) {
59+ if (booksService .create (bookDTO )) {
7260 URI location = MvcUriComponentsBuilder
73- .fromMethodName (BooksController .class , "getByIsbn" , bookDTO .getIsbn ())
74- .build ()
75- .toUri ();
61+ .fromMethodName (BooksController .class , "getByIsbn" , bookDTO .getIsbn ())
62+ .build ()
63+ .toUri ();
7664 HttpHeaders httpHeaders = new HttpHeaders ();
7765 httpHeaders .setLocation (location );
7866 return new ResponseEntity <>(httpHeaders , HttpStatus .CREATED );
@@ -82,19 +70,20 @@ public ResponseEntity<String> post(@RequestBody BookDTO bookDTO) {
8270 }
8371 }
8472
85- /* --------------------------------------------------------------------------------------------
73+ /*
74+ * -------------------------------------------------------------------------
8675 * HTTP GET
87- * ----------------------------------------------------------------------------------------- */
76+ * -------------------------------------------------------------------------
77+ */
8878
8979 @ GetMapping ("/books/{isbn}" )
9080 @ Operation (summary = "Retrieves a book by its ID" )
9181 @ ApiResponses (value = {
92- @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "application/json" , schema =
93- @ Schema (implementation = BookDTO .class ))),
94- @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
82+ @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = BookDTO .class ))),
83+ @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
9584 })
9685 public ResponseEntity <BookDTO > getByIsbn (@ PathVariable String isbn ) {
97- BookDTO bookDTO = service .retrieveByIsbn (isbn );
86+ BookDTO bookDTO = booksService .retrieveByIsbn (isbn );
9887 if (bookDTO != null ) {
9988 return new ResponseEntity <>(bookDTO , HttpStatus .OK );
10089 } else {
@@ -105,28 +94,29 @@ public ResponseEntity<BookDTO> getByIsbn(@PathVariable String isbn) {
10594 @ GetMapping ("/books" )
10695 @ Operation (summary = "Retrieves all books" )
10796 @ ApiResponses (value = {
108- @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "application/json" , schema =
109- @ Schema (implementation = BookDTO [].class )))
97+ @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = BookDTO [].class )))
11098 })
11199 public ResponseEntity <List <BookDTO >> getAll () {
112- List <BookDTO > books = service .retrieveAll ();
100+ List <BookDTO > books = booksService .retrieveAll ();
113101 return new ResponseEntity <>(books , HttpStatus .OK );
114102 }
115103
116- /* --------------------------------------------------------------------------------------------
104+ /*
105+ * -------------------------------------------------------------------------
117106 * HTTP PUT
118- * ----------------------------------------------------------------------------------------- */
107+ * -------------------------------------------------------------------------
108+ */
119109
120110 @ PutMapping ("/books" )
121111 @ Operation (summary = "Updates (entirely) a book by its ID" )
122112 @ ApiResponses (value = {
123- @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
124- @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
125- @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
113+ @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
114+ @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
115+ @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
126116 })
127117 public ResponseEntity <String > put (@ RequestBody BookDTO bookDTO ) {
128- if (service .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
129- if (service .update (bookDTO )) {
118+ if (booksService .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
119+ if (booksService .update (bookDTO )) {
130120 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
131121 } else {
132122 return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
@@ -136,20 +126,22 @@ public ResponseEntity<String> put(@RequestBody BookDTO bookDTO) {
136126 }
137127 }
138128
139- /* --------------------------------------------------------------------------------------------
129+ /*
130+ * -------------------------------------------------------------------------
140131 * HTTP DELETE
141- * ----------------------------------------------------------------------------------------- */
132+ * -------------------------------------------------------------------------
133+ */
142134
143135 @ DeleteMapping ("/books/{isbn}" )
144136 @ Operation (summary = "Deletes a book by its ID" )
145137 @ ApiResponses (value = {
146- @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
147- @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
148- @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
138+ @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
139+ @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
140+ @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
149141 })
150142 public ResponseEntity <String > delete (@ PathVariable String isbn ) {
151- if (service .retrieveByIsbn (isbn ) != null ) {
152- if (service .delete (isbn )) {
143+ if (booksService .retrieveByIsbn (isbn ) != null ) {
144+ if (booksService .delete (isbn )) {
153145 return new ResponseEntity <>(HttpStatus .NO_CONTENT );
154146 } else {
155147 return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
0 commit comments