This project is a Django-based RESTful API designed to manage books and authors, focusing on user authentication, search functionality, and book recommendations.
- The system supports adding, updating, retrieving, and deleting books and authors.
- It includes a recommendation system that suggests books based on user favorites.
- The system considers authors, genres, and themes from saved books to recommend similar titles.
- Personalized suggestions are generated using user preferences to provide relevant book recommendations.
- User Authentication: JWT-based authentication for secure API access.
- CRUD Operations: Create, retrieve, update, and delete books and authors.
- Search Functionality: Search for books by title or author name.
- Recommendation System: Suggest similar books based on a user’s favorites list.
GET /books: Retrieve a list of all books.GET /books/:id: Retrieve a specific book by ID.POST /books: Create a new book (JWT Protected).PUT /books/:id: Update an existing book (JWT Protected).DELETE /books/:id: Delete a book (JWT Protected).
GET /authors: Retrieve a list of all authors.GET /authors/:id: Retrieve a specific author by ID.POST /authors: Create a new author (JWT Protected).PUT /authors/:id: Update an existing author (JWT Protected).DELETE /authors/:id: Delete an author (JWT Protected).
POST /register: User registration endpoint.POST /login: User login, returns JWT tokens.
GET /books?search=query: Search for books by title or author name.
POST /favorites: Add a book to the user's favorites list (JWT Protected).DELETE /favorites/:id: Remove a book from the user's favorites list (JWT Protected).GET /favorites/: Get 5 recommended books based on the user’s favorite books ==(JWT Protected).
- User: A custom user model is defined to handle user registration and authentication via JWT. This model includes the fields
username,email, andpasswordfor managing users.
from django.db import models class User(models.Model): username = models.CharField(max_length=255, unique=True) email = models.EmailField(unique=True, blank=True, null=True) password = models.CharField(max_length=255) def __str__(self): return self.username
- This custom
Usermodel is used for user management, enabling secure registration, login, and JWT-based authentication for API access. - Book:
class Book(models.Model): title = models.CharField(max_length=255) author = models.ForeignKey(Author, on_delete=models.CASCADE) description = models.TextField(blank=True) published_date = models.DateField(null=True, blank=True)
- Author:
class Author(models.Model): name = models.CharField(max_length=255) image_url = models.URLField(blank=True) bio = models.TextField(blank=True)
- Favorite:
class Favorite(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) book = models.ForeignKey('Book', on_delete=models.CASCADE)
- Book-Author: A
Bookis linked to anAuthorthrough a Foreign Key. - User-FavoriteBook: A user can have multiple favorite books, stored in the
Favoritemodel, which links aUserand aBook.
- The recommendation system is designed to suggest books similar to those marked as favorites by the user. A similarity algorithm is employed to recommend books based on matching authors, genres, or other factors.
- Recommendation Endpoint:
GET /favorites/recommendationsprovides up to 5 similar books based on a user’s favorite books, calculated in less than 1 second.
-
A basic similarity algorithm based on matching authors, genres, and keywords. The system compares the user’s favorite books to the entire dataset and recommends titles with the highest similarity score.
-
- Find books by the same author.
- Find books in the same genre (if applicable).
- Use cosine similarity or another similarity metric on book descriptions or keywords.
- Endpoint:
GET /books?search=query - The search functionality allows users to search books by title or author name.
- It supports partial matches and is case-insensitive.
- JWT is used for user authentication.
- Protected endpoints (create, update, delete books/authors) require a valid JWT token.
- Only registered users can access the recommendation and favorites functionality.
- The recommendation system is designed to return results within 1 second, even for large datasets.
- Optimization Strategies:
- Efficient database queries with indexing.
- Caching frequently requested data.
- Using Django’s
select_relatedandprefetch_relatedto minimize database hits.
1. Clone the Repository
git clone git@github.com:preston-56/library-API.git
cd library-API2. Set Up Python Virtual Environment Create and activate a Python virtual environment to ensure dependencies are installed in an isolated environment:
-
Create the virtual environment:
python3 -m venv env
-
Activate the virtual environment:
-
On macOS/Linux:
source env/bin/activate -
On Windows:
.\env\Scripts\activate
-
Once activated, you should see (env) at the beginning of your command prompt, indicating the virtual environment is active.
3. Install Dependencies
pip install -r requirements.txt
4. Set Up the Database Before applying migrations, ensure your database is correctly configured.
-
Install and Set Up PostgreSQL (or your preferred database): If you're using PostgreSQL, make sure you have it installed and running. You can install PostgreSQL using:
-
On macOS:
brew install postgresql
-
On Ubuntu:
sudo apt update sudo apt install postgresql postgresql-contrib
-
Create Database: Once PostgreSQL is installed, create the database as specified in the
**.env**file:psql -U postgres CREATE DATABASE <DB_NAME>;
-
Ensure that
<DB_NAME>matches the database name defined in your.envfile. -
Configure your Database Connection: In your
.envfile, set the following environment variables to configure the database connection:DB_NAME=<your_db_name> DB_USER=<your_db_user> DB_PASSWORD=<your_db_password> DB_HOST=localhost DB_PORT=5432
-
Replace
<your_db_name>,<your-db-user>, and<your-db-password>with your actual PostgreSQL credentials.
5. Make Migrations
python3 manage.py makemigrations authors books favorite login user
6. Apply Migrations
python3 manage.py migrate
7. Run the Server
python3 manage.py runserver
8. Register a New User and Login
- Use
/registerto create a new user. - Use
/loginto generate a JWT token for authentication.
Once logged in, use the provided endpoints to manage these resources: books, authors and favorite.
-
Swagger UI: The interactive API documentation can be accessed at the following URL:
- Swagger UI (
http://127.0.0.1:8000/swagger/) - You can use Swagger UI to view, test, and interact with the API endpoints.
- To perform protected actions, simply provide your JWT token in the
Authorizesection.
- Swagger UI (
-
ReDoc: The static API documentation is available at the following URL:
-
ReDoc UI (
http://127.0.0.1:8000/swagger/) -
ReDoc provides a clean, human-readable layout for understanding the API structure and details, but does not allow you to interact with the API directly.
-
For more details on the system's implementation and features, check out the individual routes and API documentation.