Skip to content

Commit

Permalink
feeat: create book resource
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Nov 11, 2024
1 parent 2d279da commit 5da6adf
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/main/java/expert/os/demos/books/api/BookResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@


@RequestScoped
@Path("/movies")
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Movies", description = "Operations related to movies")
@Tag(name = "Books", description = "Operations related to books")
public class BookResource {


Expand All @@ -37,68 +37,69 @@ public BookResource() {
this(null);
}


@GET
@Operation(summary = "Retrieve all movies", description = "Returns a paginated list of all movies.")
@APIResponse(responseCode = "200", description = "List of movies", content = @Content(mediaType = "application/json",
@Operation(summary = "Retrieve all books", description = "Returns a paginated list of all books.")
@APIResponse(responseCode = "200", description = "List of books", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = BookResponse.class)))
public Response getAllMovies(
public Response getAllBooks(
@Parameter(description = "Page number for pagination", example = "1")
@QueryParam("page") @DefaultValue("1") int page,
@Parameter(description = "Number of items per page", example = "10")
@QueryParam("size") @DefaultValue("10") int size) {
List<BookResponse> movies = bookService.getAllMovies(page, size);
return Response.ok(movies).build();
List<BookResponse> books = bookService.getAllBooks(page, size);
return Response.ok(books).build();
}

@GET
@Path("/{id}")
@Operation(summary = "Retrieve a movie by ID", description = "Returns a movie for the specified ID.")
@APIResponse(responseCode = "200", description = "Movie details", content = @Content(mediaType = "application/json",
@Operation(summary = "Retrieve a book by ID", description = "Returns a book for the specified ID.")
@APIResponse(responseCode = "200", description = "Book details", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = BookResponse.class)))
@APIResponse(responseCode = "404", description = "Movie not found")
public Response getMovieById(
@Parameter(description = "ID of the movie to retrieve", required = true)
@APIResponse(responseCode = "404", description = "Book not found")
public Response getBookById(
@Parameter(description = "ID of the book to retrieve", required = true)
@PathParam("id") String id) {
Optional<BookResponse> movieResponse = bookService.findById(id);
return movieResponse.map(Response::ok)
Optional<BookResponse> bookResponse = bookService.findById(id);
return bookResponse.map(Response::ok)
.orElse(Response.status(Response.Status.NOT_FOUND))
.build();
}

@POST
@Operation(summary = "Create a new movie", description = "Creates a new movie in the system.")
@APIResponse(responseCode = "201", description = "Movie created", content = @Content(mediaType = "application/json",
@Operation(summary = "Create a new book", description = "Creates a new book in the system.")
@APIResponse(responseCode = "201", description = "Book created", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = BookResponse.class)))
public Response createMovie(
@Parameter(description = "Movie data to be created", required = true)
public Response createBook(
@Parameter(description = "Book data to be created", required = true)
BookRequest bookRequest) {
BookResponse bookResponse = bookService.create(bookRequest);
return Response.status(Response.Status.CREATED).entity(bookResponse).build();
}

@PUT
@Path("/{id}")
@Operation(summary = "Update a movie", description = "Updates an existing movie by ID.")
@APIResponse(responseCode = "200", description = "Movie updated", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BookResponse.class)))
@APIResponse(responseCode = "404", description = "Movie not found")
public Response updateMovie(
@Parameter(description = "ID of the movie to update", required = true)
@Operation(summary = "Update a book", description = "Updates an existing book by ID.")
@APIResponse(responseCode = "200", description = "Book updated", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BookResponse.class)))
@APIResponse(responseCode = "404", description = "Book not found")
public Response updateBook(
@Parameter(description = "ID of the book to update", required = true)
@PathParam("id") String id,
@Parameter(description = "Updated movie data", required = true)
@Parameter(description = "Updated book data", required = true)
BookRequest bookRequest) {
Optional<BookResponse> updatedMovie = bookService.update(id, bookRequest);
return updatedMovie.map(Response::ok)
Optional<BookResponse> updatedBook = bookService.update(id, bookRequest);
return updatedBook.map(Response::ok)
.orElse(Response.status(Response.Status.NOT_FOUND))
.build();
}

@DELETE
@Path("/{id}")
@Operation(summary = "Delete a movie", description = "Deletes a movie by ID.")
@APIResponse(responseCode = "204", description = "Movie deleted")
@APIResponse(responseCode = "404", description = "Movie not found")
public Response deleteMovie(
@Parameter(description = "ID of the movie to delete", required = true)
@Operation(summary = "Delete a book", description = "Deletes a book by ID.")
@APIResponse(responseCode = "204", description = "Book deleted")
@APIResponse(responseCode = "404", description = "Book not found")
public Response deleteBook(
@Parameter(description = "ID of the book to delete", required = true)
@PathParam("id") String id) {
bookService.delete(id);
return Response.noContent().build();
Expand Down

0 comments on commit 5da6adf

Please sign in to comment.