Skip to content

Commit 46c4338

Browse files
committed
Update main.py
1 parent b8d2af9 commit 46c4338

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

backend/main.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from datetime import datetime
21

2+
from datetime import datetime
33
from bson import ObjectId
44
from database import schemas_collection
55
from datamodel import SchemaDefinition, UpdateSchema
@@ -18,7 +18,6 @@
1818
allow_headers=["*"],
1919
)
2020

21-
2221
@app.get("/schemas")
2322
async def get_all_schemas():
2423
"""
@@ -29,37 +28,37 @@ async def get_all_schemas():
2928
"""
3029
# Fetch all schema documents from MongoDB
3130
schema_documents = list(schemas_collection.find())
32-
33-
# Verbose loop: clarify what we're iterating over and why
31+
# Convert ObjectId to string for each document
3432
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
3733
original_id = schema_document["_id"]
3834
schema_document["_id"] = str(original_id)
39-
40-
# Optional: Add logging or debugging info
41-
# print(f"Converted ObjectId {original_id} to string for schema: {schema_document}")
42-
4335
return schema_documents
4436

45-
4637
@app.post("/schemas")
4738
async def add_schema(schema: SchemaDefinition):
4839
"""
4940
Add a new schema to the database.
5041
Args:
5142
schema (SchemaDefinition): The schema data to insert.
5243
Returns:
53-
dict: The ID of the inserted schema.
44+
dict: A dictionary with key being the ID of the inserted schema,
45+
and value the actual inserted schema.
5446
"""
5547
# Update timestamp before insertion
5648
schema.updated_at = datetime.utcnow()
5749

5850
# Insert schema into MongoDB
5951
result = schemas_collection.insert_one(schema.dict())
52+
inserted_id = result.inserted_id # ObjectId
6053

61-
return {"id": str(result.inserted_id)}
54+
# Fetch the stored document so we return what the DB actually has
55+
inserted_doc = schemas_collection.find_one({"_id": inserted_id})
56+
# Normalize _id for JSON
57+
if inserted_doc is not None and "_id" in inserted_doc:
58+
inserted_doc["_id"] = str(inserted_doc["_id"])
6259

60+
# Return as { "<id>": <document> } per your requirement
61+
return {str(inserted_id): inserted_doc}
6362

6463
@app.put("/schemas/{id}")
6564
async def update_schema(id: str, update: UpdateSchema):
@@ -82,13 +81,11 @@ async def update_schema(id: str, update: UpdateSchema):
8281
{"_id": ObjectId(id)},
8382
{"$set": update_fields},
8483
)
85-
86-
if result.modified_count == 0:
84+
if result.modified_count == 0 and result.matched_count == 0:
8785
raise HTTPException(status_code=404, detail="Schema not found")
8886

8987
return {"message": "Schema updated"}
9088

91-
9289
@app.delete("/schemas/{id}")
9390
async def delete_schema(id: str):
9491
"""
@@ -101,6 +98,6 @@ async def delete_schema(id: str):
10198
dict: Success message.
10299
"""
103100
result = schemas_collection.delete_one({"_id": ObjectId(id)})
104-
105101
if result.deleted_count == 0:
106102
raise HTTPException(status_code=404, detail="Schema not found")
103+
return {"message": "Schema deleted"}

0 commit comments

Comments
 (0)