Skip to content

Commit 05c1ce3

Browse files
committed
Updated datamodel.py main.py model.py
1 parent f5dacc3 commit 05c1ce3

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

backend/datamodel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ class SchemaDefinition(BaseModel):
2020
None,
2121
description="Timestamp of the last update (optional)"
2222
)
23+
24+
25+
26+
class UpdateSchema(BaseModel):
27+
name: str | None = None
28+
version: str | None = None

backend/main.py

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

2+
from datetime import datetime
33
from bson import ObjectId
44
from database import schemas_collection
5-
from datamodel import SchemaDefinition
5+
from datamodel import SchemaDefinition, UpdateSchema # ✅ Updated import
66
from fastapi import FastAPI, HTTPException
77
from fastapi.middleware.cors import CORSMiddleware
8-
from model import UpdateSchema
9-
10-
# https://github.com/BAMresearch/jsoned/tree/main/backend
118

129
app = FastAPI()
1310

11+
# Enable CORS for all origins
1412
app.add_middleware(
1513
CORSMiddleware,
1614
allow_origins=["*"],
@@ -19,27 +17,49 @@
1917
allow_headers=["*"],
2018
)
2119

22-
23-
# Get all schemas
2420
@app.get("/schemas")
2521
async 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")
3434
async 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}")
4249
async 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}")
5472
async 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")

backend/model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
├── services/ # Business logic
1414
├── utils/ # Helpers
1515
"""
16+
from datetime import datetime
17+
from pydantic import BaseModel, Field
1618

17-
from pydantic import BaseModel
18-
19+
class SchemaDefinition(BaseModel):
20+
id: str = Field(..., description="Unique identifier for the schema")
21+
name: str = Field(..., description="Human-readable name of the schema", min_length=3)
22+
version: str = Field("1.0.0", description="Version of the schema")
23+
content: dict = Field(..., description="The actual schema content as a dictionary")
24+
updated_at: datetime | None = Field(None, description="Timestamp of the last update (optional)")
1925

2026
class UpdateSchema(BaseModel):
2127
name: str | None = None

0 commit comments

Comments
 (0)