Skip to content

Commit

Permalink
feat: create bookservice
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 539afec commit 4026c42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package expert.os.demos.books.api;


import expert.os.demos.books.application.MovieService;
import expert.os.demos.books.application.BookService;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
Expand All @@ -23,17 +23,17 @@
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Movies", description = "Operations related to movies")
public class MovieResource {
public class BookResource {


private final MovieService movieService;
private final BookService bookService;

@Inject
public MovieResource(MovieService movieService) {
this.movieService = movieService;
public BookResource(BookService bookService) {
this.bookService = bookService;
}

public MovieResource() {
public BookResource() {
this(null);
}

Expand All @@ -46,7 +46,7 @@ public Response getAllMovies(
@QueryParam("page") @DefaultValue("1") int page,
@Parameter(description = "Number of items per page", example = "10")
@QueryParam("size") @DefaultValue("10") int size) {
List<BookResponse> movies = movieService.getAllMovies(page, size);
List<BookResponse> movies = bookService.getAllMovies(page, size);
return Response.ok(movies).build();
}

Expand All @@ -59,7 +59,7 @@ public Response getAllMovies(
public Response getMovieById(
@Parameter(description = "ID of the movie to retrieve", required = true)
@PathParam("id") String id) {
Optional<BookResponse> movieResponse = movieService.getMovieById(id);
Optional<BookResponse> movieResponse = bookService.findById(id);
return movieResponse.map(Response::ok)
.orElse(Response.status(Response.Status.NOT_FOUND))
.build();
Expand All @@ -72,7 +72,7 @@ public Response getMovieById(
public Response createMovie(
@Parameter(description = "Movie data to be created", required = true)
BookRequest bookRequest) {
BookResponse bookResponse = movieService.createMovie(bookRequest);
BookResponse bookResponse = bookService.create(bookRequest);
return Response.status(Response.Status.CREATED).entity(bookResponse).build();
}

Expand All @@ -86,7 +86,7 @@ public Response updateMovie(
@PathParam("id") String id,
@Parameter(description = "Updated movie data", required = true)
BookRequest bookRequest) {
Optional<BookResponse> updatedMovie = movieService.updateMovie(id, bookRequest);
Optional<BookResponse> updatedMovie = bookService.update(id, bookRequest);
return updatedMovie.map(Response::ok)
.orElse(Response.status(Response.Status.NOT_FOUND))
.build();
Expand All @@ -100,7 +100,7 @@ public Response updateMovie(
public Response deleteMovie(
@Parameter(description = "ID of the movie to delete", required = true)
@PathParam("id") String id) {
movieService.deleteMovie(id);
bookService.delete(id);
return Response.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
import java.util.stream.Collectors;

@ApplicationScoped
public class MovieService {
public class BookService {

private static final Logger LOGGER = Logger.getLogger(MovieService.class.getName());
private static final Logger LOGGER = Logger.getLogger(BookService.class.getName());
public static final Order<Book> ORDER = Order.by(Sort.asc("title"));
private final BookRepository bookRepository;

private final MovieMapper movieMapper;

@Inject
public MovieService(@Database(DatabaseType.DOCUMENT) BookRepository bookRepository, MovieMapper movieMapper) {
public BookService(@Database(DatabaseType.DOCUMENT) BookRepository bookRepository, MovieMapper movieMapper) {
this.bookRepository = bookRepository;
this.movieMapper = movieMapper;
}

public MovieService() {
public BookService() {
this(null, null);
}

Expand All @@ -51,7 +51,7 @@ public List<BookResponse> getAllMovies(int page, int size) {
return movies;
}

public Optional<BookResponse> getMovieById(String id) {
public Optional<BookResponse> findById(String id) {
LOGGER.log(Level.INFO, "Fetching movie with ID: {0}", id);
Optional<BookResponse> movieResponse = bookRepository.findById(id)
.map(movieMapper::toResponse);
Expand All @@ -63,15 +63,15 @@ public Optional<BookResponse> getMovieById(String id) {
return movieResponse;
}

public BookResponse createMovie(BookRequest bookRequest) {
public BookResponse create(BookRequest bookRequest) {
LOGGER.log(Level.INFO, "Creating new movie with title: {0}", bookRequest.getTitle());
Book book = movieMapper.toEntity(bookRequest, UUID.randomUUID().toString());
Book savedBook = bookRepository.save(book);
LOGGER.log(Level.INFO, "Movie created with ID: {0}", savedBook.getId());
return movieMapper.toResponse(savedBook);
}

public Optional<BookResponse> updateMovie(String id, BookRequest bookRequest) {
public Optional<BookResponse> update(String id, BookRequest bookRequest) {
LOGGER.log(Level.INFO, "Updating movie with ID: {0}", id);
return bookRepository.findById(id)
.map(existingMovie -> {
Expand All @@ -83,7 +83,7 @@ public Optional<BookResponse> updateMovie(String id, BookRequest bookRequest) {
});
}

public void deleteMovie(String id) {
public void delete(String id) {
LOGGER.log(Level.INFO, "Deleting movie with ID: {0}", id);
bookRepository.deleteById(id);
}
Expand Down

0 comments on commit 4026c42

Please sign in to comment.