A simple and efficient Asset Management system built with FastAPI and MongoDB, providing CRUD APIs to manage and track company assets.
Note on the Base Model: This project currently represents a base model for the Asset Management API. The Pydantic models (Asset, AssetInDB) and potentially some of the API logic are designed as a starting point. They will be further adapted and refined based on the structure and content of the actual dataset received. Expect updates to the models and potentially the API endpoints to accurately reflect the data.
- Create Asset: Add new asset records to the database.
- Get Asset by ID: Retrieve details of a specific asset using its unique ID.
- Get Employee Assets: Fetch a list of all assets assigned to a particular employee.
- Update Asset: Modify the details of an existing asset.
- Delete Asset: Remove an asset record from the database.
- FastAPI: A modern, high-performance web framework for building APIs with Python.
- Pydantic: A data validation and settings management library using Python type hints.
- Motor: An asynchronous Python driver for MongoDB.
- MongoDB: A NoSQL document database.
- Python 3.7+
- pip (Python package installer)
- MongoDB installed and running (default local connection at
mongodb://localhost:27017is assumed).
-
Clone the repository (if you have one, otherwise just proceed to the next step for a new project):
git clone <repository_url> cd <project_directory>
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On macOS and Linux # venv\Scripts\activate # On Windows
-
Install dependencies:
pip install -r requirements.txt
(Note: You might need to create a
requirements.txtfile with the following content if it doesn't exist):fastapi uvicorn motor pydantic bson
- MongoDB Connection: The MongoDB connection URL and database name are defined in the script:
You can modify these variables in the main Python file (
MONGODB_URL = "mongodb://localhost:27017" DATABASE_NAME = "asset_management_db" ASSET_COLLECTION_NAME = "assets"
main.pyor the name of your FastAPI application file) to match your MongoDB setup.
-
Navigate to the project directory in your terminal.
-
Run the FastAPI application using Uvicorn:
uvicorn main:app --reload
(Replace
mainwith the name of your Python file if it's different, andappwith the name of your FastAPI application instance). -
Access the API documentation: Once the application is running, you can access the automatically generated interactive API documentation at
http://127.0.0.1:8000/docsorhttp://localhost:8000/docs.
- Endpoint:
/assets/ - Method:
POST - Request Body (JSON):
{ "employee_id": "EMP001", "asset_names": ["Laptop", "Monitor"], "asset_id": ["LAP-001", "MON-002"], "purchase_date": "2023-01-15", "serial_number": "SN12345", "condition": "Good" } - Response (JSON): Returns the newly created asset details, including its unique
idin the database.{ "employee_id": "EMP001", "asset_names": ["Laptop", "Monitor"], "asset_id": ["LAP-001", "MON-002"], "purchase_date": "2023-01-15", "serial_number": "SN12345", "condition": "Good", "id": "6440b5e7e7b2b8a7c9d3f0e1" } - Status Code:
201 Created
- Endpoint:
/assets/{asset_id} - Method:
GET - Path Parameter:
asset_id(the unique ID of the asset in the database) - Response (JSON): Returns the details of the requested asset.
{ "employee_id": "EMP001", "asset_names": ["Laptop", "Monitor"], "asset_id": ["LAP-001", "MON-002"], "purchase_date": "2023-01-15", "serial_number": "SN12345", "condition": "Good", "id": "6440b5e7e7b2b8a7c9d3f0e1" } - Status Codes:
200 OK: Asset found.404 Not Found: Asset with the given ID does not exist.400 Bad Request: Invalid asset ID format.
- Endpoint:
/employees/{employee_id}/assets/ - Method:
GET - Path Parameter:
employee_id(the ID of the employee) - Response (JSON): Returns a list of all assets assigned to the specified employee.
[ { "employee_id": "EMP001", "asset_names": ["Laptop", "Monitor"], "asset_id": ["LAP-001", "MON-002"], "purchase_date": "2023-01-15", "serial_number": "SN12345", "condition": "Good", "id": "6440b5e7e7b2b8a7c9d3f0e1" }, { "employee_id": "EMP001", "asset_names": ["Keyboard"], "asset_id": ["KEY-005"], "purchase_date": "2023-03-20", "serial_number": "SN67890", "condition": "Excellent", "id": "6440b6a1e7b2b8a7c9d3f0e2" } ] - Status Code:
200 OK
- Endpoint:
/assets/{asset_id} - Method:
PUT - Path Parameter:
asset_id(the unique ID of the asset to update) - Request Body (JSON): The fields to update. Only the provided fields will be modified.
{ "condition": "Fair" } - Response (JSON): Returns the updated asset details.
{ "employee_id": "EMP001", "asset_names": ["Laptop", "Monitor"], "asset_id": ["LAP-001", "MON-002"], "purchase_date": "2023-01-15", "serial_number": "SN12345", "condition": "Fair", "id": "6440b5e7e7b2b8a7c9d3f0e1" } - Status Codes:
200 OK: Asset updated successfully.404 Not Found: Asset with the given ID does not exist.400 Bad Request: Invalid asset ID format or no fields to update.
- Endpoint:
/assets/{asset_id} - Method:
DELETE - Path Parameter:
asset_id(the unique ID of the asset to delete) - Response (JSON):
{ "message": "Asset deleted successfully" } - Status Codes:
200 OK: Asset deleted successfully.404 Not Found: Asset with the given ID does not exist.400 Bad Request: Invalid asset ID format.
Contributions are welcome! Please feel free to submit pull requests with bug fixes, new features, or improvements.