This is a FastAPI-based Library Management System with user authentication, role-based access (Admin/User), and book borrowing functionality.
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).
POST /users/login
-
Uses
form-datawith fields:usernameβ user emailpasswordβ user password
β‘οΈ Returns JWT token:
{
"access_token": "<jwt>",
"token_type": "bearer"
}GET /users/
- Requires Bearer Token.
- Only
adminrole can access.
PUT /users/promote/{user_id}
- Requires Bearer Token.
- Admin only.
- Promotes a user to
adminrole.
POST /books/
{
"title": "Book Title",
"author": "Author Name",
"isbn": "123456"
}β‘οΈ Returns created book details.
GET /books/?skip=0&limit=10
β‘οΈ Returns paginated list of books.
GET /books/{book_id}
β‘οΈ Returns book details.
PUT /books/{book_id}
{
"title": "New Title",
"author": "New Author",
"isbn": "654321"
}β‘οΈ Updates and returns book.
DELETE /books/{book_id}
β‘οΈ Deletes book and returns confirmation.
POST /borrow/borrow/{book_id}
- Requires Bearer Token.
- Marks a book as borrowed by the current user.
POST /borrow/return/{book_id}
- Requires Bearer Token.
- Marks a book as returned.
- Admin β Can manage books & users.
- User β Can borrow/return books, view books.
This project requires environment variables. Copy the example file and rename it:
cp .env.example .envThen, update .env with your values (e.g., database URL, JWT secret, etc.).
uvicorn app.main:app --reloadAPI docs available at:
- Swagger UI β
http://127.0.0.1:8000/docs - ReDoc β
http://127.0.0.1:8000/redoc
This project includes pytest-based tests.
Run all tests:
pytest -vIf pytest command is not recognized, try:
python -m pytest -vpython3 -m pytest -vRun only user tests:
pytest app/tests/test_users.py -vRun with live logs:
pytest -sThis project is licensed under the MIT License - see the LICENSE file for details.
Copyright Β© 2025 Backstacked