Skip to content

Latest commit

 

History

History
267 lines (218 loc) · 9.1 KB

README.md

File metadata and controls

267 lines (218 loc) · 9.1 KB

Library Service API

This is a Django-based RESTful API for managing a library system with book borrowings, payments, and notifications. The API supports CRUD operations for books, user management, borrowing, payments, and integrates with the Stripe payment gateway and Telegram API.

Table of Contents

Getting Started

🛜 Prerequisites

  • Python 3.x
  • Docker
  • Docker Compose

⚙️ Installation

  1. Clone the repository:

    git clone https://github.com/sakhaline/library-service.git
    cd library-service-api
  2. You can open project in IDE and configure .env file using .env.sample file as an example.

  3. Build and run the Docker containers:

    docker-compose up --build

The API will be accessible at http://localhost:8000/.

👩‍💻 Setup Telegram Bot

You must add admin Chat ID and Initialize Chat to take notification to Telegram.

Click me

🧠 Create bot in Bot Father

  • Start chat with Bot Father
  • /newbot - to create new bot
  • Send bot's name
  • /setprivacy - change to Disable
  • Copy and save API Key, Bot Link

👯 Create chat

  • You need to create channel
  • Than, you should add your bot using Bot Link

🤔 Find chat id

  • Write some message in chat
  • Go to https://api.telegram.org/bot<TOKEN>/getUpdates.
  • In <TOKEN> place API Key
  • You will get response:
  "chat": {
    "id": -4017738106,
    "title": "Order Tickets",
    "type": "group",
    "all_members_are_administrators": true
   },
  • Save your Chat ID
  • Write all saved information inside .env file like that:
TELEGRAM_API_KEY=6503311767:AAEkcCdnc3MewRnLe53YZgnDSdqdq1pq7mE
TELEGRAM_CHAT_ID=-4017738106

📫 Image configure

You can change images that bot sends in telegram_notifications.py

BORROW_PHOTO = ("link-to-image")
PAYMENT_PHOTO = ("link-to-image")

🐻 Setup User Chat ID

If User add his Chat ID and Initialize Chat with Bot, he can take notification to Telegram.

Click me

🐦 Initialize Chat with Bot

🐧 Get Chat ID

☃️ Save Chat ID

  • Go to /api/user/me/
  • Paste Chat id to Telegram chat id field
  • Make sure that you Initialized Chat with Bot
  • Select Chat initialized checkbox
  • Make PUT request
  • All must be like here: img.png

🎄 Now you will get new notification to your Telegram !

Components

📚 Books Service

Managing books (CRUD operations)

Description The Books Service in the Library API offers complete CRUD functionality, including the initialization of the "books" app, creation of a book model, and implementation of serializers and views for essential endpoints. Additionally, robust permissions are enforced, ensuring that only admin users have the authority to create, update, and delete books, while all users, including those not authenticated, can access a comprehensive list of available books. JWT token authentication from the users' service enhances security and user-specific interactions within the Books Service.
API Endpoints
  • POST: /books/ - Add a new book
  • GET: /books/ - Get a list of books
  • GET: /books/<id>/ - Get detailed information about a book
  • PUT/PATCH: /books/<id>/ - Update book details (including inventory)
  • DELETE: /books/<id>/ - Delete a book

👨‍👩‍👧‍👦 Users Service

Managing authentication & user registration

Description The Users Service in the Library API implements full CRUD functionality, beginning with the initialization of the "users" app. This involves adding a user model with email support and integrating JWT (JSON Web Token) authentication for enhanced security. The implementation includes serializers and views for all endpoints, ensuring a seamless and secure user management system.
API Endpoints
  • POST: /users/ - Register a new user
  • POST: /users/token/ - Get JWT tokens
  • POST: /users/token/refresh/ - Refresh JWT token
  • GET: /users/me/ - Get user's profile information
  • PUT/PATCH: /users/me/ - Update user's profile information

🗒 Borrowings Service

Managing users' borrowings of books

Description The Borrowings Service in the Library API provides robust functionality for managing book borrowings. It includes the initialization of the borrowing app, a model with constraints for accurate tracking, and a read serializer for detailed book information. The service offers endpoints for listing, retrieving details, and creating borrowings, with validations for inventory and user permissions. Borrowings can be marked as returned, triggering inventory updates, and notifications are sent for new borrowings and overdue items. Telegram integration enables real-time notifications, and a scheduled task checks for daily overdue borrowings, providing detailed alerts or a notification of no overdue borrowings. This service ensures efficient borrowing processes and timely communication with users.
API Endpoints
  • POST: /borrowings/ - Add a new borrowing (decrement inventory by 1)
  • GET: /borrowings/?user_id=...&is_active=... - Get borrowings by user id and active/inactive status
  • GET: /borrowings/<id>/ - Get specific borrowing
  • POST: /borrowings/<id>/return/ - Set actual return date (increment inventory by 1)

💬 Notifications Service (Telegram)

Sending notifications about new borrowing created, borrowings overdue & successful payment

  • Notifications are sent asynchronously using Django Celery.
  • Interacts with other services to send notifications to library administrators.
  • Uses Telegram API, Telegram Chats & Bots.

💵 Payments Service (Stripe)

Description Stripe is a widely used payment processing platform that enables businesses and individuals to accept online payments. Stripe offers a RESTful API that allows developers to interact with various resources, such as customers, payments, subscriptions, and more. Admins Only - Refund money to a user in case of unexpected issues. Accessible to users with is_staff = True.

Bellow we can see how the payment session looks like:

payment

Payment Endpoints
  • GET: /api/payments - Retrieve all payments for an authenticated user.
  • GET: /api/payments/pk/ - Retrieve detailed information about a specific payment.
  • GET: /api/payments/pk/success/ - Description: Check the success status of a Stripe payment by examining the payment status.
  • GET: /api/payments/pk/cancel/ - Return a payment paused message, allowing the user's payment link to be available for 24 hours.
  • GET: /api/payments/pk/refund/ - Admins Only - Refund money to a user in case of unexpected issues. Accessible to users with is_staff = True.

You can test how the payment session works using these test card credentials 💳:

  • Email: [email protected]
  • Cardholder name: Joe Doe
  • Card Number: 4242 4242 4242 4242
  • Date MM/YY: 11/30 (random feature date)
  • CVC: 123

Perform payments for book borrowings through the platform

API Endpoints:
  • GET: /success/ - Check successful Stripe payment
  • GET: /cancel/ - Return payment paused message

Interacts with Stripe API using the stripe package.

Documentation

The API is documented using the OpenAPI standard. Access the API documentation by running the server and navigating to http://localhost:8000/api/doc/swagger/ or http://localhost:8000/api/doc/redoc/. Screenshot from 2023-12-18 16-14-21

DB Schema

Screenshot from 2023-12-18 17-01-48