-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e927ef6
commit 863a467
Showing
5 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends | ||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session | ||
|
||
from app.database import get_session | ||
from app.models import Preference, User | ||
from app.schemas import PreferencesList, PreferencesPublic | ||
from app.security import get_current_user | ||
|
||
router = APIRouter(prefix='/preferences', tags=['preferences']) | ||
|
||
DbSession = Annotated[Session, Depends(get_session)] | ||
CurrentUser = Annotated[User, Depends(get_current_user)] | ||
|
||
|
||
# Routes | ||
@router.get('/', response_model=PreferencesList) | ||
def list_settings(session: DbSession, user: CurrentUser): | ||
preferences = session.scalars(select(Preference)).all() | ||
return { | ||
'preferences': [ | ||
PreferencesPublic.model_validate(p) for p in preferences | ||
] | ||
} | ||
|
||
|
||
@router.put('/', response_model=PreferencesList) | ||
def update_preferences( | ||
request: PreferencesList, | ||
session: DbSession, | ||
user: CurrentUser, | ||
): | ||
# Map the incoming preferences by key for quick lookup | ||
incoming_preferences = { | ||
pref.key: pref.value for pref in request.preferences | ||
} | ||
|
||
# Fetch existing preferences from the database that match the incoming keys | ||
existing_preferences = list( | ||
session.scalars( | ||
select(Preference).where( | ||
Preference.key.in_(incoming_preferences.keys()) | ||
) | ||
).all() | ||
) | ||
|
||
# Update values for existing preferences | ||
updated_keys = set() | ||
for pref in existing_preferences: | ||
if pref.key in incoming_preferences: | ||
pref.value = incoming_preferences[pref.key] | ||
updated_keys.add(pref.key) | ||
|
||
# Create new preferences for keys that were not updated | ||
new_preferences = [ | ||
Preference(key=key, value=value) # type: ignore | ||
for key, value in incoming_preferences.items() | ||
if key not in updated_keys | ||
] | ||
session.add_all(new_preferences) | ||
|
||
# Commit all changes at once | ||
session.commit() | ||
|
||
# Refresh all preferences (both updated and new) | ||
all_preferences = existing_preferences + new_preferences | ||
for pref in all_preferences: | ||
session.refresh(pref) | ||
|
||
# Prepare the response | ||
return { | ||
'preferences': [ | ||
PreferencesPublic( | ||
key=p.key, | ||
value=p.value, | ||
created_at=p.created_at, | ||
updated_at=p.updated_at, | ||
) | ||
for p in all_preferences | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
migrations/versions/202412270635-59f0de858d13_create_preferences_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""create preferences table | ||
Revision ID: 59f0de858d13 | ||
Revises: f6b2d59ed89a | ||
Create Date: 2024-12-27 06:35:08.610715 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '59f0de858d13' | ||
down_revision: Union[str, None] = 'f6b2d59ed89a' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('preferences', | ||
sa.Column('key', sa.String(), nullable=False), | ||
sa.Column('value', sa.String(), nullable=False), | ||
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), | ||
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False), | ||
sa.PrimaryKeyConstraint('key') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('preferences') | ||
# ### end Alembic commands ### |