REST API to manage a collection of rare books.
- Manage Rare Books (Get, Create, Update, Delete)
- Manage Book Categories
- Search and filter books by various criteria
- Pagination support
- Validation and error handling
These instructions will help you set up and run the project on your local machine for development and testing purposes.
- Java 21 or higher
- Maven 3.6+
- MySQL 8.x
- Docker (optional, for containerized setup)
Set the required environment variables for the application based on .env.example
:
SPRING_SERVER_PORT=
SPRING_DATASOURCE_URL=
SPRING_DATASOURCE_USERNAME=
SPRING_DATASOURCE_PASSWORD=
SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.cj.jdbc.Driver
SPRING_FLYWAY_LOCATIONS=classpath:db/migration/mysql
SPRING_HIBERNATE_DIALECT=org.hibernate.dialect.MySQLDialect
Now set the application.properties
file:
spring.server.port=${SPRING_SERVER_PORT}
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
spring.datasource.driver-class-name=${SPRING_DATASOURCE_DRIVER_CLASS_NAME}
spring.jpa.properties.hibernate.dialect=${SPRING_HIBERNATE_DIALECT}
spring.flyway.locations=${SPRING_FLYWAY_LOCATIONS}
Alternatively you can set fixed values in the application.properties
file:
spring.server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/rarebookdb
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.flyway.locations=classpath:db/migration/mysql
-
Build the project:
mvn clean install
-
Run the application:
mvn spring-boot:run
The API will be available at http://localhost:<server port>/api/v1/
.
This project includes support for running the Rare Book API in a containerized environment using Docker and Docker Compose.
-
Build the Docker Image
docker build -t rare-book-api:latest .
-
Run with Docker Compose:
docker compose up
- app: Rare Book API on port
8080
. - mysql: MySQL server on port
3307
. - phpmyadmin: phpMyAdmin on
http://localhost:9090
.
docker compose down
Method | Endpoint | Description |
---|---|---|
GET |
/api/v1/books |
Get a paginated list of books with optional filters |
GET |
/api/v1/books/{id} |
Get a book by its ID |
POST |
/api/v1/books |
Create a new book |
PUT |
/api/v1/books/{id} |
Update an existing book by its ID |
DELETE |
/api/v1/books/{id} |
Delete a book by its ID |
Method | Endpoint | Description |
---|---|---|
GET |
/api/v1/categories |
List all categories |
GET |
/api/v1/categories/{id} |
Get a category by ID |
POST |
/api/v1/categories |
Create a new category |
PUT |
/api/v1/categories/{id} |
Update an existing category |
DELETE |
/api/v1/categories/{id} |
Delete a category |
A Postman collection is provided for testing the API.
-
Download the Collection: Get the Postman Collection File
-
Import into Postman:
- Open Postman.
- Click
Import
and select therare-book-api-collection.json
file.
-
Run Requests: You can now run the requests to interact with the API endpoints.
This project uses JUnit for unit and integration tests. You can run the tests with the following command:
mvn test
This project is licensed under the MIT License - see the LICENSE file for details.
- wait_for_it.sh: Utility script for docker containers.