Skip to content

Commit

Permalink
- Allow query on multiple tags
Browse files Browse the repository at this point in the history
- one_or_none() in favor of first()
  • Loading branch information
JonasKs committed Mar 21, 2022
1 parent a395642 commit 0aa7179
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**Storage**: [gg.klepp.me](https://gg.klepp.me) -> TLS through AWS Certificate Manager -> AWS Cloudfront CDN -> AWS S3 bucket
**API**: [api.klepp.me](https://api.klepp.me/docs) -> TLS through Heroku -> Hosted on Heroku -> FastAPI -> [validate tokens](app/api/security.py) -> [S3 bucket](app/api/api_v1/endpoints/file.py)
**Authentication** [auth.klepp.me](https://auth.klepp.me) -> TLS through AWS Certificate Manager -> AWS Cognito -> Validating tokens in backend
**Frontend**: [klepp.me - Soon™](https://klepp.me) - x -> TLS -> React frontend -> Cognito auth -> Requests to the API
**Frontend**: [klepp.me](https://klepp.me) -> GitHub pages -> TLS -> React frontend -> Cognito auth -> Requests to the API

Visualized something like this:
![visualized stack](klepp.png)
Expand Down
2 changes: 1 addition & 1 deletion app/api/api_v2/endpoints/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def delete_file(
"""
video_statement = select(Video).where(and_(Video.path == path.path, Video.user_id == user.id))
db_result = await db_session.exec(video_statement) # type: ignore
video = db_result.first()
video = db_result.one_or_none()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down
9 changes: 5 additions & 4 deletions app/api/api_v2/endpoints/list_videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def get_all_files(
user: CognitoUser | None = Depends(cognito_scheme_or_anonymous),
username: Optional[str] = None,
hidden: bool = False,
tag: Optional[str] = None,
tags: Optional[str] = Query(default=None, description='Comma seperated list of tag names'),
offset: int = 0,
limit: int = Query(default=100, lte=100),
) -> dict[str, int | list]:
Expand All @@ -46,8 +46,9 @@ async def get_all_files(
)
else:
video_statement = video_statement.where(Video.hidden == False) # noqa
if tag:
video_statement = video_statement.where(Video.tags.any(name=tag)) # type: ignore
if tags:
for tag in tags.split(','):
video_statement = video_statement.where(Video.tags.any(name=tag)) # type: ignore

# Total count query based on query params, without pagination
count_statement = select(func.count('*')).select_from(video_statement) # type: ignore
Expand All @@ -60,5 +61,5 @@ async def get_all_files(
asyncio.create_task(session.exec(count_statement)),
]
results, count = await asyncio.gather(*tasks)
count_number = count.first()
count_number = count.one_or_none()
return {'total_count': count_number, 'response': results.all()}
4 changes: 2 additions & 2 deletions app/api/api_v2/endpoints/patch_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def patch_video(
.options(selectinload(Video.tags))
)
db_result = await db_session.exec(query_video) # type: ignore
video = db_result.first()
video = db_result.one_or_none()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down Expand Up @@ -74,4 +74,4 @@ async def patch_video(
.options(selectinload(Video.likes))
)
result = await db_session.exec(query_video) # type: ignore
return result.first()
return result.one_or_none()
2 changes: 1 addition & 1 deletion app/api/api_v2/endpoints/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ async def get_all_tags(
asyncio.create_task(session.exec(count_statement)),
]
results, count = await asyncio.gather(*tasks)
count_number = count.first()
count_number = count.one_or_none()
return {'total_count': count_number, 'response': results.all()}
2 changes: 1 addition & 1 deletion app/api/api_v2/endpoints/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ async def upload_file(
.options(selectinload(Video.likes))
)
result = await db_session.exec(query_video) # type: ignore
return result.first()
return result.one_or_none()
2 changes: 1 addition & 1 deletion app/api/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async def cognito_signed_in(
"""
select_user = select(User).where(User.name == cognito_user.username)
user_query = await db_session.exec(select_user) # type: ignore
user = user_query.first()
user = user_query.one_or_none()
if not user:
new_user = User(name=cognito_user.username)
db_session.add(new_user)
Expand Down

0 comments on commit 0aa7179

Please sign in to comment.