Class BookService

java.lang.Object
com.example.restapi.service.BookService

@Service public class BookService extends Object
Service class that provides business logic for managing Book and borrowing operations.
  • Field Details

    • bookRepository

      @Autowired private BookRepository bookRepository
      Repository for managing Book entities.
    • userRepository

      @Autowired private UserRepository userRepository
  • Constructor Details

    • BookService

      public BookService()
  • Method Details

    • getAllBooks

      public List<Book> getAllBooks()
      Retrieves all books from the repository.
      Returns:
      a list of all Book objects
    • getBookById

      public Optional<Book> getBookById(Long id)
      Retrieves a book by its unique identifier.
      Parameters:
      id - the ID of the book to retrieve
      Returns:
      an Optional containing the found Book, or empty if not found
    • createBook

      public Book createBook(Book book)
      Creates a new book entry in the repository.
      Parameters:
      book - the Book object to be created
      Returns:
      the created Book object
    • updateBook

      public Book updateBook(Long id, Book bookDetails) throws Exception
      Updates an existing book's details.
      Parameters:
      id - the ID of the book to update
      bookDetails - the updated Book details
      Returns:
      the updated Book object
      Throws:
      Exception - if the book with the specified ID is not found
    • deleteBook

      public void deleteBook(Long id)
      Deletes the Book identified by id. deleteBook() needs to be public because it is used by clients who wish to remove books identified by id
    • borrowBook

      public boolean borrowBook(Long bookId, Long userId)
    • returnBook

      public boolean returnBook(Long bookId)
      Returns a borrowed book, making it available for others.
      Parameters:
      bookId - the ID of the book to return
      Returns:
      true if the return was successful, false otherwise