In the digital age, online bookstores have revolutionized how we buy and sell books, offering unparalleled convenience and accessibility. Book Nook App was inspired by the need for a seamless, user-friendly backend platform where book lovers can discover, purchase, and manage their favorite reads.
Book Nook App addresses key challenges:
- For Customers: Simplifies finding the right book with robust search and filter options, ensuring a smooth purchasing experience through an efficient order management system.
- For Administrators: Provides comprehensive tools for inventory management, order tracking, and real-time updates, making it easy to manage an online bookstore.
Built with Spring Boot, Spring Security, Spring Data JPA, and Springdoc OpenAPI, Book Nook App offers a secure and robust backend, perfect for supporting the operations of an online bookstore.
- ▶Short Overview
- ▶Demo
- ▶Technologies Used
- ▶Features
- ▶Setup Instructions
- ▶Postman Collection
- ▶Challenges Faced
- ▶Author
- ▶License
Use Swagger UI
-
Open link.
-
Open Authentication management Registration endpoint:
-
Click on "Try it out" button:
-
Register a new user (fill in a unique email) and "Execute":
-
If you successfully registered, you'll see status code 201 with your registration data:
-
Repeat steps 1-3 on Authentication management Login endpoint (use registered login and password).
-
Copy token from response body:
-
Click on the "Authorize" button on the top of the page, paste the token in the field, and click "Authorize":
-
Now you can use endpoints for users.
If you want to use manager's endpoints, fill free to use credentials below:
{
"email":"[email protected]",
"password":"12345678"
}
Note: The token expires after 1 hour, so you must log in again.
- Spring Boot: Framework for building Java-based web applications.
- Spring Security: Provides authentication and authorization.
- Spring Data JPA: Simplifies database interactions.
- Spring Web: For building web-based applications with Spring MVC.
- Springdoc OpenAPI: Generates API documentation.
- MySQL: Database management system.
- Hibernate: As the ORM tool.
- Liquibase: Database migration tool.
- MapStruct: Object mapping framework.
- JUnit: Testing framework.
- Mockito: Mocking framework for tests.
- Maven: For project management and dependency management.
- Testcontainers: Provides lightweight, throwaway instances of databases, selenium web servers, or anything else that can run in a Docker container.
- Docker: Containerization platform.
Swagger is integrated into the project to provide comprehensive API documentation and facilitate testing of endpoints. With Swagger, you can visualize and interact with the API's resources without having any of the implementation logic in place.
To access the Swagger UI and explore the API documentation:
Once the application is running, navigate to http://localhost:8080/api/swagger-ui/index.html
in your web browser.
You will see the Swagger UI interface, where you can view all available endpoints, their descriptions, request parameters, and response schemas.
Use the interactive features of Swagger UI to make requests directly from the browser and observe the responses.
Authentication Endpoints: Users can register and log in.
- Register a new user
- Request:
POST /api/auth/register
- Response:
201 Created
- Request:
- Login a user
- Request:
POST /api/auth/login
- Response:
200 OK
with JWT token
- Request:
User's Endpoints: View and search books.
- Get all books
- Request:
GET /api/books
- Response:
200 OK
- Request:
- Get a book by ID
- Request:
GET /api/books/{id}
- Response:
200 OK
- Request:
- Search books by params
- Request
GET /api/books/search
- Response:
200 OK
- Request
Manager's and Admin's Endpoints: Add, update, and delete books.
- Add a new book
- Request
POST /api/books
- Response:
201 Created
- Request
- Update a book
- Request
PUT /api/books/{id}
- Response:
202 Accepted
- Request
- Delete a book
- Request
DELETE /api/books/{id}
- Response:
204 No Content
- Request
User's Endpoints: View categories.
- Get all categories
- Request
GET /api/categories
- Response:
200 OK
- Request
- Get a category by ID
- Request
GET /api/categories/{id}
- Response:
200 OK
- Request
- Get all books from category
- Request
GET /api/categories/{id}/books
- Response:
200 OK
- Request
Manager's and Admin's Endpoints: Create, update, and delete book categories.
- Add a new category
- Request
POST /api/categories
- Response:
201 Created
- Request
- Update a category
- Request
PUT /api/categories/{id}
- Response:
202 Accepted
- Request
- Delete a category
- Request
DELETE /api/categories/{id}
- Response:
204 No Content
- Request
User's Endpoints: Add books to the cart, view cart items, update and remove items from the cart.
- Get current user's shopping cart
- Request
GET /api/cart
- Response:
200 Ok
- Request
- Add a book to the cart
- Request
POST /api/cart
- Response:
201 Created
- Request
- Update book quantity
- Request
PUT /api/cart/cart-items/{cartItemId}
- Response:
202 Accepted
- Request
- Remove a book from the cart
- Request
DELETE /api/cart/cart-items/{cartItemId}
- Response:
204 No Content
- Request
User's Endpoints: Complete the purchase, view past orders and their details.
- Create order
- Request
POST /api/orders
- Response:
201 Created
- Request
- Get all orders for current user
- Request
GET /api/orders
- Response:
200 Ok
- Request
- Get an order details by ID
- Request
GET /api/orders/{orderId}/items
- Response:
200 Ok
- Request
- Get an ordered book details by ID
- Request
GET /api/orders/{orderId}/items/{id}
- Response:
200 Ok
- Request
Manager's and Admin's Order Endpoints: Update order statuses.
- Update order status
- Request
PATCH /api/orders/{id}
- Response:
202 Accepted
- Request
- Docker
- Docker Compose
-
Clone the Repository:
git clone https://github.com/k0sm0naft/java-online-book-store.git cd java-online-book-store
-
Set Environment Variables:
Create a
.env
file in the project root directory and populate it with the following environment variables:MYSQLDB_USER=your_db_user_name MYSQLDB_ROOT_PASSWORD=your_db_password JWT_SECRET=yourVeryLongSecretStringForJwtSecretKey JWT_EXPIRATION=3600000 MYSQLDB_DATABASE=your_db_name MYSQLDB_LOCAL_PORT=3306 MYSQLDB_DOCKER_PORT=3306 SPRING_LOCAL_PORT=8080 SPRING_DOCKER_PORT=8080 DEBUG_PORT=5005
-
Install dependencies and build the project:
mvn clean install
-
Build and Run the Docker Containers:
docker-compose up
-
Access the Application:
Open your browser and go to http://localhost:8080/api/swagger-ui.html to access the Swagger API documentation.
-
Stop and Remove Containers: To stop and remove the containers created by the Compose file, use the docker-compose down command:
docker-compose down
-
To run tests, use the following command:
mvn test
-
Testing with JUnit and Mockito
-
The project uses JUnit for unit testing and Mockito for mocking dependencies. This ensures that the application logic is tested in isolation, making the tests more reliable and easier to maintain.
To facilitate testing, a Postman collection has been provided. You can import it into Postman and use it to test the API endpoints.
To chose URL:
- After you fork collection, you can register a new user, or use credentials below in the text.
- After log in, the generated token will be automatically added to all next requests that required authentication.
User
{
"email": "[email protected]",
"password": "passExample123"
}
Manager
{
"email":"[email protected]",
"password":"12345678"
}
Security: Setting up robust authentication and authorization using Spring Security.
Database Management: Utilized Spring Data JPA and Hibernate for efficient database interactions and ORM capabilities.
Database Migrations: Implementing database migrations with Liquibase to ensure consistency across different environments.
API Documentation: Ensuring comprehensive API documentation with Springdoc OpenAPI.
- LinkedIn: Artem Akymenko
- Github: @k0sm0naft
Copyright © 2024 Artem Akymenko.
This project is MIT licensed.