Skip to content

Commit c1c83dc

Browse files
committed
now only querying what's necessary for api endpoints (response_model usually) where possible
1 parent 6b19cb4 commit c1c83dc

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

src/app/api/v1/posts.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def write_post(
2323
current_user: Annotated[UserRead, Depends(get_current_user)],
2424
db: Annotated[AsyncSession, Depends(async_get_db)]
2525
):
26-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
26+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
2727
if db_user is None:
2828
raise HTTPException(status_code=404, detail="User not found")
2929

@@ -44,11 +44,11 @@ async def read_posts(
4444
username: str,
4545
db: Annotated[AsyncSession, Depends(async_get_db)]
4646
):
47-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
47+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
4848
if db_user is None:
4949
raise HTTPException(status_code=404, detail="User not found")
5050

51-
posts = await crud_posts.get_multi(db=db, created_by_user_id=db_user.id, is_deleted=False)
51+
posts = await crud_posts.get_multi(db=db, schema_to_select=PostRead, created_by_user_id=db_user.id, is_deleted=False)
5252
return posts
5353

5454

@@ -60,11 +60,11 @@ async def read_post(
6060
id: int,
6161
db: Annotated[AsyncSession, Depends(async_get_db)]
6262
):
63-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
63+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
6464
if db_user is None:
6565
raise HTTPException(status_code=404, detail="User not found")
6666

67-
db_post = await crud_posts.get(db=db, id=id, created_by_user_id=db_user.id, is_deleted=False)
67+
db_post = await crud_posts.get(db=db, schema_to_select=PostRead, id=id, created_by_user_id=db_user.id, is_deleted=False)
6868
if db_post is None:
6969
raise HTTPException(status_code=404, detail="Post not found")
7070

@@ -85,14 +85,14 @@ async def patch_post(
8585
current_user: Annotated[UserRead, Depends(get_current_user)],
8686
db: Annotated[AsyncSession, Depends(async_get_db)]
8787
):
88-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
88+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
8989
if db_user is None:
9090
raise HTTPException(status_code=404, detail="User not found")
9191

9292
if current_user.id != db_user.id:
9393
raise privileges_exception
9494

95-
db_post = await crud_posts.get(db=db, id=id, is_deleted=False)
95+
db_post = await crud_posts.get(db=db, schema_to_select=PostRead, id=id, is_deleted=False)
9696
if db_post is None:
9797
raise HTTPException(status_code=404, detail="Post not found")
9898

@@ -113,14 +113,14 @@ async def erase_post(
113113
current_user: Annotated[UserRead, Depends(get_current_user)],
114114
db: Annotated[AsyncSession, Depends(async_get_db)]
115115
):
116-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
116+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
117117
if db_user is None:
118118
raise HTTPException(status_code=404, detail="User not found")
119119

120120
if current_user.id != db_user.id:
121121
raise privileges_exception
122122

123-
db_post = await crud_posts.get(db=db, id=id, is_deleted=False)
123+
db_post = await crud_posts.get(db=db, schema_to_select=PostRead, id=id, is_deleted=False)
124124
if db_post is None:
125125
raise HTTPException(status_code=404, detail="Post not found")
126126

@@ -141,11 +141,11 @@ async def erase_db_post(
141141
id: int,
142142
db: Annotated[AsyncSession, Depends(async_get_db)]
143143
):
144-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
144+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
145145
if db_user is None:
146146
raise HTTPException(status_code=404, detail="User not found")
147147

148-
db_post = await crud_posts.get(db=db, id=id, is_deleted=False)
148+
db_post = await crud_posts.get(db=db, schema_to_select=PostRead, id=id, is_deleted=False)
149149
if db_post is None:
150150
raise HTTPException(status_code=404, detail="Post not found")
151151

src/app/api/v1/users.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def write_user(
3838

3939
@router.get("/users", response_model=List[UserRead])
4040
async def read_users(request: Request, db: Annotated[AsyncSession, Depends(async_get_db)]):
41-
users = await crud_users.get_multi(db=db, is_deleted=False)
41+
users = await crud_users.get_multi(db=db, schema_to_select=UserRead, is_deleted=False)
4242
return users
4343

4444

@@ -48,12 +48,10 @@ async def read_users_me(
4848
):
4949
return current_user
5050

51-
from app.core.cache import cache
52-
5351

5452
@router.get("/user/{username}", response_model=UserRead)
5553
async def read_user(request: Request, username: str, db: Annotated[AsyncSession, Depends(async_get_db)]):
56-
db_user = await crud_users.get(db=db, username=username, is_deleted=False)
54+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username, is_deleted=False)
5755
if db_user is None:
5856
raise HTTPException(status_code=404, detail="User not found")
5957

@@ -68,7 +66,7 @@ async def patch_user(
6866
current_user: Annotated[UserRead, Depends(get_current_user)],
6967
db: Annotated[AsyncSession, Depends(async_get_db)]
7068
):
71-
db_user = await crud_users.get(db=db, username=username)
69+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username)
7270
if db_user is None:
7371
raise HTTPException(status_code=404, detail="User not found")
7472

@@ -96,11 +94,11 @@ async def erase_user(
9694
current_user: Annotated[UserRead, Depends(get_current_user)],
9795
db: Annotated[AsyncSession, Depends(async_get_db)]
9896
):
99-
db_user = await crud_users.get(db=db, username=username)
100-
if db_user is None:
97+
db_user = await crud_users.get(db=db, schema_to_select=UserRead, username=username)
98+
if not db_user:
10199
raise HTTPException(status_code=404, detail="User not found")
102100

103-
if db_user.username != current_user.username:
101+
if username != current_user.username:
104102
raise privileges_exception
105103

106104
await crud_users.delete(db=db, db_row=db_user, username=username)

src/app/crud/crud_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sqlalchemy.ext.asyncio import AsyncSession
77
from sqlalchemy.engine.row import Row
88

9-
from .helper import _extract_matching_columns_from_schema, _extract_matching_columns_from_kwargs
9+
from .helper import _extract_matching_columns_from_schema, _extract_matching_columns_from_kwargs, _extract_matching_columns_from_column_names
1010

1111
ModelType = TypeVar("ModelType")
1212
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)

src/app/crud/helper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ def _extract_matching_columns_from_kwargs(model: Type[Base], kwargs: dict) -> Li
4040
column_list.append(getattr(model, column_name))
4141

4242
return column_list
43+
44+
45+
def _extract_matching_columns_from_column_names(model: Type[Base], column_names: List) -> List[Any]:
46+
column_list = []
47+
for column_name in column_names:
48+
if hasattr(model, column_name):
49+
column_list.append(getattr(model, column_name))
50+
51+
return column_list

src/app/schemas/user.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class UserRead(BaseModel):
3939
str,
4040
Field(min_length=2, max_length=20, pattern=r"^[a-z0-9]+$", examples=["userson"])
4141
]
42+
email: Annotated[
43+
EmailStr,
44+
Field(examples=["[email protected]"])
45+
]
4246
profile_image_url: str
4347

4448

0 commit comments

Comments
 (0)