Skip to content

πŸ“š Library Management System API built with FastAPI β€” features JWT authentication, role-based access (Admin/User), and book borrowing/return management.

License

Notifications You must be signed in to change notification settings

BackStacked/Library-Management-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“š Library Management System API

This is a FastAPI-based Library Management System with user authentication, role-based access (Admin/User), and book borrowing functionality.


πŸ”‘ Authentication

Signup (Register User/Admin)

POST /users/signup

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "mypassword",
  "admin_secret": "supersecretadminkey" // optional β†’ only if registering as admin
}

➑️ Returns created user details (without password).

Login

POST /users/login

  • Uses form-data with fields:

    • username β†’ user email
    • password β†’ user password

➑️ Returns JWT token:

{
  "access_token": "<jwt>",
  "token_type": "bearer"
}

πŸ‘€ Users

List Users (Admin Only)

GET /users/

  • Requires Bearer Token.
  • Only admin role can access.

Promote User to Admin

PUT /users/promote/{user_id}

  • Requires Bearer Token.
  • Admin only.
  • Promotes a user to admin role.

πŸ“– Books

Add Book (Admin Only)

POST /books/

{
  "title": "Book Title",
  "author": "Author Name",
  "isbn": "123456"
}

➑️ Returns created book details.

List Books

GET /books/?skip=0&limit=10 ➑️ Returns paginated list of books.

Get Book by ID

GET /books/{book_id} ➑️ Returns book details.

Update Book (Admin Only)

PUT /books/{book_id}

{
  "title": "New Title",
  "author": "New Author",
  "isbn": "654321"
}

➑️ Updates and returns book.

Delete Book (Admin Only)

DELETE /books/{book_id} ➑️ Deletes book and returns confirmation.


πŸ“• Borrow / Return

Borrow a Book

POST /borrow/borrow/{book_id}

  • Requires Bearer Token.
  • Marks a book as borrowed by the current user.

Return a Book

POST /borrow/return/{book_id}

  • Requires Bearer Token.
  • Marks a book as returned.

βš™οΈ Roles & Access Control

  • Admin β†’ Can manage books & users.
  • User β†’ Can borrow/return books, view books.

⚑ Environment Setup

This project requires environment variables. Copy the example file and rename it:

cp .env.example .env

Then, update .env with your values (e.g., database URL, JWT secret, etc.).

▢️ Running the Project

uvicorn app.main:app --reload

API docs available at:

  • Swagger UI β†’ http://127.0.0.1:8000/docs
  • ReDoc β†’ http://127.0.0.1:8000/redoc

πŸ§ͺ Testing the Project

This project includes pytest-based tests.

Run all tests:

pytest -v

If pytest command is not recognized, try:

python -m pytest -v

or

python3 -m pytest -v

Run only user tests:

pytest app/tests/test_users.py -v

Run with live logs:

pytest -s

License

This project is licensed under the MIT License - see the LICENSE file for details.
Copyright Β© 2025 Backstacked

About

πŸ“š Library Management System API built with FastAPI β€” features JWT authentication, role-based access (Admin/User), and book borrowing/return management.

Topics

Resources

License

Stars

Watchers

Forks

Languages