1- from datetime import datetime
21
2+ from datetime import datetime
33from bson import ObjectId
44from database import schemas_collection
5- from datamodel import SchemaDefinition
5+ from datamodel import SchemaDefinition , UpdateSchema # ✅ Updated import
66from fastapi import FastAPI , HTTPException
77from fastapi .middleware .cors import CORSMiddleware
8- from model import UpdateSchema
9-
10- # https://github.com/BAMresearch/jsoned/tree/main/backend
118
129app = FastAPI ()
1310
11+ # Enable CORS for all origins
1412app .add_middleware (
1513 CORSMiddleware ,
1614 allow_origins = ["*" ],
1917 allow_headers = ["*" ],
2018)
2119
22-
23- # Get all schemas
2420@app .get ("/schemas" )
2521async def get_all_schemas ():
22+ """
23+ Retrieve all schemas from the database.
24+
25+ Returns:
26+ list: A list of schema documents with stringified IDs.
27+ """
2628 schemas = list (schemas_collection .find ())
2729 for s in schemas :
2830 s ["_id" ] = str (s ["_id" ])
2931 return schemas
3032
31-
32- # Add new schema
3333@app .post ("/schemas" )
3434async def add_schema (schema : SchemaDefinition ):
35+ """
36+ Add a new schema to the database.
37+
38+ Args:
39+ schema (SchemaDefinition): The schema data to insert.
40+
41+ Returns:
42+ dict: The ID of the inserted schema.
43+ """
3544 schema .updated_at = datetime .utcnow ()
3645 result = schemas_collection .insert_one (schema .dict ())
3746 return {"id" : str (result .inserted_id )}
3847
39-
40- # Update schema (PUT)
4148@app .put ("/schemas/{id}" )
4249async def update_schema (id : str , update : UpdateSchema ):
50+ """
51+ Update an existing schema by ID.
52+
53+ Args:
54+ id (str): The schema ID.
55+ update (UpdateSchema): Fields to update.
56+
57+ Raises:
58+ HTTPException: If the schema is not found.
59+
60+ Returns:
61+ dict: Success message.
62+ """
4363 result = schemas_collection .update_one (
4464 {"_id" : ObjectId (id )},
4565 {"$set" : {k : v for k , v in update .dict ().items () if v is not None }},
@@ -48,10 +68,20 @@ async def update_schema(id: str, update: UpdateSchema):
4868 raise HTTPException (status_code = 404 , detail = "Schema not found" )
4969 return {"message" : "Schema updated" }
5070
51-
52- # Delete schema
5371@app .delete ("/schemas/{id}" )
5472async def delete_schema (id : str ):
73+ """
74+ Delete a schema by ID.
75+
76+ Args:
77+ id (str): The schema ID.
78+
79+ Raises:
80+ HTTPException: If the schema is not found.
81+
82+ Returns:
83+ dict: Success message.
84+ """
5585 result = schemas_collection .delete_one ({"_id" : ObjectId (id )})
5686 if result .deleted_count == 0 :
5787 raise HTTPException (status_code = 404 , detail = "Schema not found" )
0 commit comments