11package ar .com .nanotaboada .java .samples .spring .boot .controllers ;
22
3+ import static org .springframework .http .HttpHeaders .LOCATION ;
4+
35import java .net .URI ;
46import java .util .List ;
57
6- import org .springframework .http .HttpHeaders ;
8+ import jakarta .validation .Valid ;
9+
10+ import org .hibernate .validator .constraints .ISBN ;
711import org .springframework .http .HttpStatus ;
812import org .springframework .http .ResponseEntity ;
913import org .springframework .web .bind .annotation .DeleteMapping ;
2529import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
2630import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
2731import io .swagger .v3 .oas .annotations .tags .Tag ;
32+
2833import ar .com .nanotaboada .java .samples .spring .boot .models .BookDTO ;
2934import ar .com .nanotaboada .java .samples .spring .boot .services .BooksService ;
3035
@@ -52,22 +57,18 @@ public BooksController(BooksService booksService) {
5257 @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
5358 @ ApiResponse (responseCode = "409" , description = "Conflict" , content = @ Content )
5459 })
55- public ResponseEntity <String > post (@ RequestBody BookDTO bookDTO ) {
56- if (booksService .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
57- return new ResponseEntity <>(HttpStatus .CONFLICT );
58- } else {
59- if (booksService .create (bookDTO )) {
60- URI location = MvcUriComponentsBuilder
61- .fromMethodName (BooksController .class , "getByIsbn" , bookDTO .getIsbn ())
62- .build ()
63- .toUri ();
64- HttpHeaders httpHeaders = new HttpHeaders ();
65- httpHeaders .setLocation (location );
66- return new ResponseEntity <>(httpHeaders , HttpStatus .CREATED );
67- } else {
68- return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
69- }
60+ public ResponseEntity <Void > post (@ RequestBody @ Valid BookDTO bookDTO ) {
61+ boolean created = booksService .create (bookDTO );
62+ if (!created ) {
63+ return ResponseEntity .status (HttpStatus .CONFLICT ).build ();
7064 }
65+ URI location = MvcUriComponentsBuilder
66+ .fromMethodCall (MvcUriComponentsBuilder .on (BooksController .class ).getByIsbn (bookDTO .getIsbn ()))
67+ .build ()
68+ .toUri ();
69+ return ResponseEntity .status (HttpStatus .CREATED )
70+ .header (LOCATION , location .toString ())
71+ .build ();
7172 }
7273
7374 /*
@@ -77,18 +78,16 @@ public ResponseEntity<String> post(@RequestBody BookDTO bookDTO) {
7778 */
7879
7980 @ GetMapping ("/books/{isbn}" )
80- @ Operation (summary = "Retrieves a book by its ID " )
81+ @ Operation (summary = "Retrieves a book by its ISBN " )
8182 @ ApiResponses (value = {
8283 @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = BookDTO .class ))),
8384 @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
8485 })
8586 public ResponseEntity <BookDTO > getByIsbn (@ PathVariable String isbn ) {
8687 BookDTO bookDTO = booksService .retrieveByIsbn (isbn );
87- if (bookDTO != null ) {
88- return new ResponseEntity <>(bookDTO , HttpStatus .OK );
89- } else {
90- return new ResponseEntity <>(HttpStatus .NOT_FOUND );
91- }
88+ return (bookDTO != null )
89+ ? ResponseEntity .status (HttpStatus .OK ).body (bookDTO )
90+ : ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
9291 }
9392
9493 @ GetMapping ("/books" )
@@ -98,7 +97,7 @@ public ResponseEntity<BookDTO> getByIsbn(@PathVariable String isbn) {
9897 })
9998 public ResponseEntity <List <BookDTO >> getAll () {
10099 List <BookDTO > books = booksService .retrieveAll ();
101- return new ResponseEntity <>( books , HttpStatus .OK );
100+ return ResponseEntity . status ( HttpStatus .OK ). body ( books );
102101 }
103102
104103 /*
@@ -108,22 +107,17 @@ public ResponseEntity<List<BookDTO>> getAll() {
108107 */
109108
110109 @ PutMapping ("/books" )
111- @ Operation (summary = "Updates (entirely) a book by its ID " )
110+ @ Operation (summary = "Updates (entirely) a book by its ISBN " )
112111 @ ApiResponses (value = {
113112 @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
114113 @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
115114 @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
116115 })
117- public ResponseEntity <String > put (@ RequestBody BookDTO bookDTO ) {
118- if (booksService .retrieveByIsbn (bookDTO .getIsbn ()) != null ) {
119- if (booksService .update (bookDTO )) {
120- return new ResponseEntity <>(HttpStatus .NO_CONTENT );
121- } else {
122- return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
123- }
124- } else {
125- return new ResponseEntity <>(HttpStatus .NOT_FOUND );
126- }
116+ public ResponseEntity <Void > put (@ RequestBody @ Valid BookDTO bookDTO ) {
117+ boolean updated = booksService .update (bookDTO );
118+ return (updated )
119+ ? ResponseEntity .status (HttpStatus .NO_CONTENT ).build ()
120+ : ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
127121 }
128122
129123 /*
@@ -133,21 +127,16 @@ public ResponseEntity<String> put(@RequestBody BookDTO bookDTO) {
133127 */
134128
135129 @ DeleteMapping ("/books/{isbn}" )
136- @ Operation (summary = "Deletes a book by its ID " )
130+ @ Operation (summary = "Deletes a book by its ISBN " )
137131 @ ApiResponses (value = {
138132 @ ApiResponse (responseCode = "204" , description = "No Content" , content = @ Content ),
139133 @ ApiResponse (responseCode = "400" , description = "Bad Request" , content = @ Content ),
140134 @ ApiResponse (responseCode = "404" , description = "Not Found" , content = @ Content )
141135 })
142- public ResponseEntity <String > delete (@ PathVariable String isbn ) {
143- if (booksService .retrieveByIsbn (isbn ) != null ) {
144- if (booksService .delete (isbn )) {
145- return new ResponseEntity <>(HttpStatus .NO_CONTENT );
146- } else {
147- return new ResponseEntity <>(HttpStatus .BAD_REQUEST );
148- }
149- } else {
150- return new ResponseEntity <>(HttpStatus .NOT_FOUND );
151- }
136+ public ResponseEntity <Void > delete (@ PathVariable @ ISBN String isbn ) {
137+ boolean deleted = booksService .delete (isbn );
138+ return (deleted )
139+ ? ResponseEntity .status (HttpStatus .NO_CONTENT ).build ()
140+ : ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
152141 }
153142}
0 commit comments