Skip to content

A Django-based RESTful API for managing a library system, featuring user authentication, book recommendations, search functionality, and data science integration for suggesting similar titles based on user preferences.

Notifications You must be signed in to change notification settings

preston-56/library-API

Repository files navigation

Library Management System (Django RESTful API)

Overview:

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.

Key Features:

  1. User Authentication: JWT-based authentication for secure API access.
  2. CRUD Operations: Create, retrieve, update, and delete books and authors.
  3. Search Functionality: Search for books by title or author name.
  4. Recommendation System: Suggest similar books based on a user’s favorites list.

API Endpoints

Books:

  • 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).

Authors:

  • 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).

User Authentication:

  • POST /register: User registration endpoint.
  • POST /login: User login, returns JWT tokens.

Search Functionality:

  • GET /books?search=query: Search for books by title or author name.

Favorites and Recommendations:

  • 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).

Database Schema

Models
  • User: A custom user model is defined to handle user registration and authentication via JWT. This model includes the fields username, email, and password for 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 User model 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)

Relationships:

  • Book-Author: A Book is linked to an Author through a Foreign Key.
  • User-FavoriteBook: A user can have multiple favorite books, stored in the Favorite model, which links a User and a Book.

Recommendation System

  • 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/recommendations provides up to 5 similar books based on a user’s favorite books, calculated in less than 1 second.

Algorithm:

  • 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.

  • Logic:

    • 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.

Search Functionality

  • 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.

Security

  • 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.

Performance

  • 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_related and prefetch_related to minimize database hits.

Setting Up the Project

1. Clone the Repository

git clone [email protected]:preston-56/library-API.git
cd library-API

2. 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 .env file.

  • Configure your Database Connection: In your .env file, 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 /register to create a new user.
  • Use /login to generate a JWT token for authentication.

Once logged in, use the provided endpoints to manage these resources: books, authors and favorite.


Library API Documentation

  • 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 Authorize section.
  • 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.


Additional Information

For more details on the system's implementation and features, check out the individual routes and API documentation.

About

A Django-based RESTful API for managing a library system, featuring user authentication, book recommendations, search functionality, and data science integration for suggesting similar titles based on user preferences.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published