|
1 | 1 |
|
2 | 2 | from datetime import datetime |
3 | | - |
4 | 3 | from bson import ObjectId |
5 | | -from database import schemas_collection |
6 | | -from datamodel import SchemaDefinition, UpdateSchema |
7 | 4 | from fastapi import FastAPI, HTTPException |
8 | 5 | from fastapi.middleware.cors import CORSMiddleware |
9 | 6 |
|
| 7 | +from database import schemas_collection |
| 8 | +from datamodel import SchemaDefinition, UpdateSchema |
| 9 | + |
10 | 10 | # Initialize FastAPI application |
11 | 11 | app = FastAPI() |
12 | 12 |
|
|
22 | 22 | @app.get("/schemas") |
23 | 23 | async def get_all_schemas(): |
24 | 24 | """ |
25 | | - Retrieve all schemas from the database. |
| 25 | + Retrieve all schema documents from the database. |
26 | 26 | Converts MongoDB ObjectId to string for JSON serialization. |
27 | 27 | Returns: |
28 | 28 | list: A list of schema documents with stringified IDs. |
29 | 29 | """ |
30 | | - # Fetch all schemas from MongoDB |
| 30 | + # Fetch all schema documents from MongoDB |
31 | 31 | schema_documents = list(schemas_collection.find()) |
32 | 32 |
|
33 | | - # Verbose loop: explain and transform each schema |
34 | | - for schema_doc in schema_documents: |
35 | | - # Convert ObjectId to string for API response |
36 | | - original_id = schema_doc["_id"] |
37 | | - schema_doc["_id"] = str(original_id) |
| 33 | + # Verbose loop: clarify what we're iterating over and why |
| 34 | + for schema_document in schema_documents: |
| 35 | + # Each schema_document is a MongoDB record representing a schema definition |
| 36 | + # Convert its ObjectId to string so it can be returned in JSON |
| 37 | + original_id = schema_document["_id"] |
| 38 | + schema_document["_id"] = str(original_id) |
38 | 39 |
|
39 | | - # Optional: Add extra clarity or logging (could use logging module) |
40 | | - # print(f"Processed schema: original_id={original_id}, converted_id={schema_doc['_id']}") |
| 40 | + # Optional: Add logging or debugging info |
| 41 | + # print(f"Converted ObjectId {original_id} to string for schema: {schema_document}") |
41 | 42 |
|
42 | 43 | return schema_documents |
43 | 44 |
|
@@ -73,7 +74,7 @@ async def update_schema(id: str, update: UpdateSchema): |
73 | 74 | dict: Success message. |
74 | 75 | """ |
75 | 76 | # Prepare update fields (ignore None values) |
76 | | - update_fields = {k: v for k, v in update.dict().items() if v is not None} |
| 77 | + update_fields = {key: value for key, value in update.dict().items() if value is not None} |
77 | 78 |
|
78 | 79 | result = schemas_collection.update_one( |
79 | 80 | {"_id": ObjectId(id)}, |
@@ -102,4 +103,8 @@ async def delete_schema(id: str): |
102 | 103 | if result.deleted_count == 0: |
103 | 104 | raise HTTPException(status_code=404, detail="Schema not found") |
104 | 105 |
|
105 | | - return {"message": "Schema deleted"} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
0 commit comments