1- from datetime import datetime
21
2+ from datetime import datetime
33from bson import ObjectId
44from database import schemas_collection
55from datamodel import SchemaDefinition , UpdateSchema
1818 allow_headers = ["*" ],
1919)
2020
21-
2221@app .get ("/schemas" )
2322async 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" )
4738async 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}" )
6564async 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}" )
9390async 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