This is a RESTful API for managing a bookstore inventory built with Django and Django REST Framework. The API includes features for filtering books by author, category, and price, ordering books by title, price, and stock, and applying pagination. Swagger is used for API documentation.
- Features
- Installation
- Configuration
- Usage
- API Endpoints
- Filtering and Ordering
- Pagination
- Contributing
- List all books
- Add a new book
- Retrieve, update, and delete a specific book
- List all authors
- List all categories
- Filter books by author, category, and price
- Order books by title, price, and stock
- Pagination for book listings
- Python 3.x
- pip
- virtualenv (optional, but recommended)
git clone https://github.com/Mohamed-Taha-Essa/django-bookstore-inventory.git
cd bookstore-inventory-api
python -m venv env
source env/bin/activate # On Windows use `env\Scripts\activate`
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Create a .env
file in the root directory and add any necessary environment variables, such as database configuration, secret keys, etc.
Example:
SECRET_KEY=your_secret_key
DEBUG=True
DATABASE_NAME=db.sqlite3
ALLOWED_HOSTS=localhost, 127.0.0.1
Swagger documentation is automatically generated and available at /swagger/
once the server is running.
To start the application, use the following command:
python manage.py runserver
Navigate to http://localhost:8000/admin
and log in with your superuser credentials.
Navigate to http://localhost:8000/swagger/
to view the API documentation.
GET /api/book/list
- List all booksPOST /api/book/list
- Add a new bookGET /api/book/list/<int:pk>
- Retrieve a specific bookPUT /api/book/list/<int:pk>
- Update a specific bookDELETE /api/book/list/<int:pk>
- Delete a specific book
GET /api/author/list
- List all authors
GET /api/category/list
- List all categories
You can filter the books by author, category, and price, and order by title, price, and stock using query parameters.
- Filter by author:
GET /api/book/list?author=author_name
- Filter by category:
GET /api/book/list?category=category_name
- Filter by price range:
GET /api/book/list?min_price=10&max_price=50
- Order by title:
GET /api/book/list?ordering=title
- Order by price:
GET /api/book/list?ordering=price
- Order by stock:
GET /api/book/list?ordering=stock
To reverse the order, prefix the field with a minus sign (-
):
- Reverse order by title:
GET /api/book/list?ordering=-title
- Reverse order by price:
GET /api/book/list?ordering=-price
- Reverse order by stock:
GET /api/book/list?ordering=-stock
Pagination is applied to the book listing endpoint to handle large sets of data. Use the following query parameters to navigate through pages:
GET /api/book/list?page=1
- Get the first page of resultsGET /api/book/list?page_size=10
- Set the number of results per page (default is 10)
To get the second page of books ordered by price in descending order:
GET /api/book/list?ordering=-price&page=2
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.