- Database Setup
- Project Setup
- Book Marketplace Application
- Technologies Used
- Project Structure
- Custom Exceptions
- Exception Handling
- API Documentation
The application uses MySQL as its database system. To set up the database, follow these steps:
-
Install MySQL: Install MySQL on your system if you haven't already.
-
Database Configuration: Open
application.properties
in theresources
directory and configure your MySQL database connection properties, such asspring.datasource.url
,spring.datasource.username
, andspring.datasource.password
. -
Schema Creation: The application automatically creates the necessary schema and tables on startup, thanks to Spring Data JPA. You can also manually create the schema using SQL scripts (script.sql) in the
resources
directory if needed. -
Database Initialization: You can initialize the database with sample data or specific setup scripts, if required.
To set up the project and run it locally, follow these steps:
-
Clone the Repository: Clone the project repository to your local machine.
-
Import into IDE: Import the project into your preferred Java IDE (e.g., IntelliJ IDEA or Eclipse).
-
Build and Run: Build the project and run it as a Spring Boot application.
-
Access the API: The API will be accessible at the defined port (usually 8080) by default. You can explore and test the endpoints using Swagger or Postman, as mentioned in the API Documentation section.
The Book Marketplace Application is a web-based platform that enables users to buy and sell books for students. It offers a range of features for managing user accounts, listing books for sale, purchasing books, and handling various book-related operations. This README provides an in-depth overview of the application's capabilities, components, and usage.
The Book Marketplace Application offers the following key features:
Fixed size pool of books in the inventory, to be sold, bought back and resold to students. Price of book depreciates by 10% at each transaction.
-
User Management:
- Retrieve all users.
- Retrieve a user by their ID.
- Search for a user by username or email.
- Add a new user.
- Update an existing user.
- Delete a user by their ID.
- Retrieve books purchased by a user.
- Retrieve books sold by a user.
- Add funds to a user's account.
-
Book Management:
- Retrieve all books or books matching specific criteria.
- Retrieve a book by its ISBN.
- Search for books based on a keyword.
- Add a new book to the marketplace.
- Update an existing book.
- Delete a book by its ID.
- Purchase a book by a user.
- Sell a book by a user.
- Sell a book by ISBN.
- Retrieve All Users: GET
/users/
- Retrieve a list of all users. - Retrieve User by ID: GET
/users/getUserById?userId={id}
- Retrieve a user by their unique ID. - Search Users: GET
/users/searchUsers?keyword={searchTerm}
- Search for a user by username or email. - Add User: POST
/users/addUser
- Register a new user. - Update User: PUT
/users/updateUser?userId={id}
- Update an existing user. - Delete User: DELETE
/users/deleteUser?userId={id}
- Delete a user by their ID. - Retrieve Purchased Books: GET
/users/purchasedBooks?userId={id}
- Retrieve books purchased by a user. - Retrieve Books Sold by User: GET
/users/booksSoldByUser?userId={id}
- Retrieve books sold by a user. - Add Funds to User's Account: PUT
/users/addFunds?userId={id}&funds={amount}
- Add funds to a user's account.
- Retrieve All Books: GET
/books/
: Retrieve all books or books matching specific criteria. - Retrieve Book by ISBN: GET
/books/getBookByISBN?isbn={isbn}
: Retrieve a book by its ISBN. - Retrieve Book by Book ID: GET
/books/getBookById?bookId={bookId}
: Retrieve a book by its ID. - Retrieve Book by Category: GET
/books/getBooksByCategory?category={category}
: Retrieve books by category. - Search Books by title, author or category: GET
/books/searchBooks?keyword={searchTerm}
: Search for books based on a keyword. - Add Book: POST
/books/addBook
: Add a new book to the marketplace. - Update Book: PUT
/books/updateBook?bookId={bookId}
: Update an existing book. - Delete Book: DELETE
/books/deleteBook?bookId={bookId}
: Delete a book by its ID. - Buy Book: POST
/books/buyBook
: Purchase a book by a user. - Sell Book: POST
/books/sellBook
: Sell back a book owned by a user. - Sell Book by ISBN: POST
/books/sellBookByISBN
: Sell a book by ISBN if exists or add a new book to the inventory for sale.
- Java: The primary programming language.
- Spring Boot: The framework for building Java applications.
- Spring Data JPA: For simplified data access and database interaction.
- Spring MVC: For building web applications and RESTful APIs.
- Lombok: To reduce boilerplate code.
- Restful API
- MySQL
The application is organized as follows:
com.books.bookmarketplace.controller
: Contains controller classes for handling HTTP requests and defining API endpoints.com.books.bookmarketplace.entity
: Defines JPA entity classes for users, books, and transactions.com.books.bookmarketplace.model
: Defines model classes for simplified data representation.com.books.bookmarketplace.repository
: Provides repository interfaces for database operations.com.books.bookmarketplace.service
: Contains service classes for handling the logic behind the API calls.com.books.bookmarketplace.errorhandler
: Contains custom exception classes and global exception handling.resources
: Configuration files, including application properties and database setup.
The application includes custom exception classes with specific purposes:
UserNotFoundException
: Thrown when a user is not found.UserAlreadyExistsException
: Thrown when a user already exists.BookNotFoundException
: Thrown when a book is not found.BookAlreadyExistsException
: Thrown when a book already exists.InventoryFullException
: Thrown when the inventory is full.ValidationException
: Thrown for validation errors.
The GlobalExceptionHandler
class handles all custom exceptions and other exceptions to provides meaningful error responses. It maps different exceptions to appropriate HTTP status codes and custom error messages.
The API is documented using Swagger and Postman, which provides interactive documentation for all endpoints in swagger.yml file and BookApplication.postman_collection.json.