Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaminMammadzada/b7a2d902b9635caf5d669d118ffc8048 to your computer and use it in GitHub Desktop.
Save RaminMammadzada/b7a2d902b9635caf5d669d118ffc8048 to your computer and use it in GitHub Desktop.
Sample Project: Online Book Store API

Project: Online Book Store API

Objective: Implement a RESTful API for an online book store using Java and Spring Boot. The API should allow users to perform CRUD (Create, Read, Update, Delete) operations on books and authors.

Requirements:

  1. Create a new Spring Boot project using Spring Initializr. Include the following dependencies: Web, JPA, and H2.
  2. Set up the project with the following structure:
    • model package: containing the domain model classes (e.g., Book, Author)
    • repository package: containing the repository interfaces extending JpaRepository
    • service package: containing the service classes for business logic
    • controller package: containing the REST API controllers
    • exception package: containing custom exception classes
    • dto package: containing the data transfer objects used in the API
  3. Implement the domain model:
    • Book class: should have fields for id, title, isbn, publishDate, price, and an Author relationship (ManyToOne)
    • Author class: should have fields for id, name, email, and a list of Book relationships (OneToMany)
  4. Implement the repositories:
    • BookRepository interface: should extend JpaRepository<Book, Long>
    • AuthorRepository interface: should extend JpaRepository<Author, Long>
  5. Implement the services:
    • BookService class: should contain methods for creating, updating, deleting, and retrieving books
    • AuthorService class: should contain methods for creating, updating, deleting, and retrieving authors
  6. Implement the REST API controllers:
    • BookController class: should handle HTTP requests for CRUD operations on books
    • AuthorController class: should handle HTTP requests for CRUD operations on authors
  7. Implement the data transfer objects (DTOs) for the API:
    • BookDTO: should contain fields for id, title, isbn, publishDate, price, and an Author relationship
    • AuthorDTO: should contain fields for id, name, and email
  8. Implement exception handling:
    • Create custom exceptions (e.g., ResourceNotFoundException, BadRequestException)
    • Implement a global exception handler using @ControllerAdvice to handle exceptions and return appropriate HTTP response status codes
  9. Configure an H2 in-memory database for the application.
  10. Write unit tests for the service classes using JUnit and Mockito.
  11. (Optional) Implement API documentation using Swagger or OpenAPI.

Deliverables:

  • A fully functional Spring Boot project, including source code and tests
  • A README file explaining how to build and run the project, as well as any design decisions made

When submitting your project, make sure to include all the necessary files in a single archive (e.g., ZIP or TAR) and provide clear instructions for building and running your application. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment