Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"editor.rulers": [80],
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"[java]": {
"editor.defaultFormatter": "redhat.java"
},
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic",
"sonarlint.connectedMode.project": {
Expand Down
2 changes: 1 addition & 1 deletion assets/images/structure.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/swagger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import ar.com.nanotaboada.java.samples.spring.boot.repositories.BooksRepository;

/**
* A configuration class that declares one or more Bean methods and also triggers auto-configuration
* and component scanning.
* */
* A configuration class that declares one or more Bean methods and also
* triggers auto-configuration and component scanning.
*/
@SpringBootApplication
@EnableCaching
public class Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,37 @@

@RestController
@Tag(name = "Books")
@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"
)
)
)
@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")))
public class BooksController {

private final BooksService service;
private final BooksService booksService;

public BooksController(BooksService service) {
this.service = service;
public BooksController(BooksService booksService) {
this.booksService = booksService;
}

/* --------------------------------------------------------------------------------------------
/*
* -------------------------------------------------------------------------
* HTTP POST
* ----------------------------------------------------------------------------------------- */
* -------------------------------------------------------------------------
*/

@PostMapping("/books")
@Operation(summary = "Creates a new book")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Created", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "409", description = "Conflict", content = @Content)
@ApiResponse(responseCode = "201", description = "Created", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "409", description = "Conflict", content = @Content)
})
public ResponseEntity<String> post(@RequestBody BookDTO bookDTO) {
if (service.retrieveByIsbn(bookDTO.getIsbn()) != null) {
if (booksService.retrieveByIsbn(bookDTO.getIsbn()) != null) {
return new ResponseEntity<>(HttpStatus.CONFLICT);
} else {
if (service.create(bookDTO)) {
if (booksService.create(bookDTO)) {
URI location = MvcUriComponentsBuilder
.fromMethodName(BooksController.class, "getByIsbn", bookDTO.getIsbn())
.build()
.toUri();
.fromMethodName(BooksController.class, "getByIsbn", bookDTO.getIsbn())
.build()
.toUri();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(location);
return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
Expand All @@ -82,19 +70,20 @@
}
}

/* --------------------------------------------------------------------------------------------
/*
* -------------------------------------------------------------------------
* HTTP GET
* ----------------------------------------------------------------------------------------- */
* -------------------------------------------------------------------------
*/

@GetMapping("/books/{isbn}")
@Operation(summary = "Retrieves a book by its ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema =
@Schema(implementation = BookDTO.class))),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BookDTO.class))),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
})
public ResponseEntity<BookDTO> getByIsbn(@PathVariable String isbn) {
BookDTO bookDTO = service.retrieveByIsbn(isbn);
BookDTO bookDTO = booksService.retrieveByIsbn(isbn);
if (bookDTO != null) {
return new ResponseEntity<>(bookDTO, HttpStatus.OK);
} else {
Expand All @@ -105,28 +94,29 @@
@GetMapping("/books")
@Operation(summary = "Retrieves all books")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema =
@Schema(implementation = BookDTO[].class)))
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BookDTO[].class)))
})
public ResponseEntity<List<BookDTO>> getAll() {
List<BookDTO> books = service.retrieveAll();
List<BookDTO> books = booksService.retrieveAll();
return new ResponseEntity<>(books, HttpStatus.OK);
}

/* --------------------------------------------------------------------------------------------
/*
* -------------------------------------------------------------------------
* HTTP PUT
* ----------------------------------------------------------------------------------------- */
* -------------------------------------------------------------------------
*/

@PutMapping("/books")
@Operation(summary = "Updates (entirely) a book by its ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "No Content", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
@ApiResponse(responseCode = "204", description = "No Content", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
})
public ResponseEntity<String> put(@RequestBody BookDTO bookDTO) {
if (service.retrieveByIsbn(bookDTO.getIsbn()) != null) {
if (service.update(bookDTO)) {
if (booksService.retrieveByIsbn(bookDTO.getIsbn()) != null) {

Check warning on line 118 in src/main/java/ar/com/nanotaboada/java/samples/spring/boot/controllers/BooksController.java

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 7 lines is too similar to src/main/java/ar/com/nanotaboada/java/samples/spring/boot/controllers/BooksController.java:136
if (booksService.update(bookDTO)) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Expand All @@ -136,20 +126,22 @@
}
}

/* --------------------------------------------------------------------------------------------
/*
* -------------------------------------------------------------------------
* HTTP DELETE
* ----------------------------------------------------------------------------------------- */
* -------------------------------------------------------------------------
*/

@DeleteMapping("/books/{isbn}")
@Operation(summary = "Deletes a book by its ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "No Content", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
@ApiResponse(responseCode = "204", description = "No Content", content = @Content),
@ApiResponse(responseCode = "400", description = "Bad Request", content = @Content),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
})
public ResponseEntity<String> delete(@PathVariable String isbn) {
if (service.retrieveByIsbn(isbn) != null) {
if (service.delete(isbn)) {
if (booksService.retrieveByIsbn(isbn) != null) {

Check warning on line 143 in src/main/java/ar/com/nanotaboada/java/samples/spring/boot/controllers/BooksController.java

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 7 lines is too similar to src/main/java/ar/com/nanotaboada/java/samples/spring/boot/controllers/BooksController.java:111
if (booksService.delete(isbn)) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
Expand Down
Loading
Loading