Conversation
There was a problem hiding this comment.
-
compress routes layer by moving db logic to service layer ex:
-
dont use dictionary for creation use the pydantic model itself; heres what gemini said:
Loss of Introspection: When you pass a dict, your IDE (VS Code/PyCharm) has no idea what keys are inside it. You lose autocomplete and linting.
Double Work: The API route already validated the data into a FurnitureCreate Pydantic model. By calling .model_dump(), you are throwing away that structured object and turning it back into a "bag of strings."
Runtime Errors: If you change a field name in your Pydantic schema but forget to update the service logic, the dict will still pass through, but the database operation will crash at runtime.
this uses a custom set and a dictionary comprehension to filter out "unwritable" columns:
filtered = {k: v for k, v in data.items() if k in _WRITABLE_COLUMNS}
Why this is a failure:
Redundancy: Pydantic is literally built to do this. Your FurnitureCreate schema should already only contain writable fields.
Maintenance Nightmare: Every time you add a column to your database, you have to ensure it’s in the model, the schema, AND that your _WRITABLE_COLUMNS logic isn't accidentally blocking it or allowing something it shouldn't (like id).
Logic Leak: The service layer shouldn't be "guessing" what is writable by inspecting Furniture.table.c.keys(). The Schema defines what the user is allowed to send.
The Fix: Trust your Pydantic Schema.
fixed code should look sumn like this:
async def update_furniture(
furniture_id: str,
payload: FurnitureUpdate, # Use the Pydantic model
db: AsyncSession
) -> Furniture | None:
furniture = await get_furniture(furniture_id, db)
if not furniture:
return None
# model_dump(exclude_unset=True) ensures we ONLY update
# the fields the user actually provided in the request.
update_data = payload.model_dump(exclude_unset=True)
# Update the DB object
for key, value in update_data.items():
setattr(furniture, key, value)
await db.commit()
await db.refresh(furniture)
return furniture31fdffa to
2feafaf
Compare
|
#17 also involves a schema update. if that gets merged in before this PR:
|
df33a73 to
1a4c186
Compare
kenzysoror
left a comment
There was a problem hiding this comment.
👌 i've regenerated the migration, so there shouldn't be any merge issues there!
Jira ticket link
Jira ticket
Implementation description
Steps to test
donor_id="test-donor-1"client_id="string"route_id="string"date_donated(timezone misalignment to-be-fixed)date_received(timezone misalignment to-be-fixed)What should reviewers focus on?
Checklist
Format for branch, commit, and PR title: docs/GIT.md.