Skip to content

Commit 870d45f

Browse files
authored
✨ Upgrade items router with new SQLModel models, simplified logic, and new FastAPI Annotated dependencies (fastapi#560)
1 parent 9befa33 commit 870d45f

File tree

1 file changed

+59
-62
lines changed
  • src/backend/app/app/api/api_v1/endpoints

1 file changed

+59
-62
lines changed
Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,96 @@
1-
from typing import Any, List
1+
from typing import Annotated
22

33
from fastapi import APIRouter, Depends, HTTPException
4-
from sqlalchemy.orm import Session
4+
from sqlmodel import Session, select
55

6-
from app import crud, models, schemas
76
from app.api import deps
7+
from app.models import Item, ItemCreate, ItemOut, ItemUpdate, User
88

99
router = APIRouter()
1010

11+
SessionDep = Annotated[Session, Depends(deps.get_db)]
12+
CurrentUser = Annotated[User, Depends(deps.get_current_active_user)]
1113

12-
@router.get("/", response_model=List[schemas.Item])
14+
15+
@router.get("/")
1316
def read_items(
14-
db: Session = Depends(deps.get_db),
15-
skip: int = 0,
16-
limit: int = 100,
17-
current_user: models.User = Depends(deps.get_current_active_user),
18-
) -> Any:
17+
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
18+
) -> list[ItemOut]:
1919
"""
2020
Retrieve items.
2121
"""
22-
if crud.user.is_superuser(current_user):
23-
items = crud.item.get_multi(db, skip=skip, limit=limit)
22+
23+
if current_user.is_superuser:
24+
statement = select(Item).offset(skip).limit(limit)
25+
return session.exec(statement).all() # type: ignore
2426
else:
25-
items = crud.item.get_multi_by_owner(
26-
db=db, owner_id=current_user.id, skip=skip, limit=limit
27+
statement = (
28+
select(Item)
29+
.where(Item.owner_id == current_user.id)
30+
.offset(skip)
31+
.limit(limit)
2732
)
28-
return items
33+
return session.exec(statement).all() # type: ignore
2934

3035

31-
@router.post("/", response_model=schemas.Item)
32-
def create_item(
33-
*,
34-
db: Session = Depends(deps.get_db),
35-
item_in: schemas.ItemCreate,
36-
current_user: models.User = Depends(deps.get_current_active_user),
37-
) -> Any:
36+
@router.get("/{id}")
37+
def read_item(session: SessionDep, current_user: CurrentUser, id: int) -> ItemOut:
3838
"""
39-
Create new item.
39+
Get item by ID.
4040
"""
41-
item = crud.item.create_with_owner(db=db, obj_in=item_in, owner_id=current_user.id)
42-
return item
41+
item = session.get(Item, id)
42+
if not item:
43+
raise HTTPException(status_code=404, detail="Item not found")
44+
if not current_user.is_superuser and (item.owner_id != current_user.id):
45+
raise HTTPException(status_code=400, detail="Not enough permissions")
46+
return item # type: ignore
4347

4448

45-
@router.put("/{id}", response_model=schemas.Item)
46-
def update_item(
47-
*,
48-
db: Session = Depends(deps.get_db),
49-
id: int,
50-
item_in: schemas.ItemUpdate,
51-
current_user: models.User = Depends(deps.get_current_active_user),
52-
) -> Any:
49+
@router.post("/")
50+
def create_item(
51+
*, session: SessionDep, current_user: CurrentUser, item_in: ItemCreate
52+
) -> ItemOut:
5353
"""
54-
Update an item.
54+
Create new item.
5555
"""
56-
item = crud.item.get(db=db, id=id)
57-
if not item:
58-
raise HTTPException(status_code=404, detail="Item not found")
59-
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
60-
raise HTTPException(status_code=400, detail="Not enough permissions")
61-
item = crud.item.update(db=db, db_obj=item, obj_in=item_in)
62-
return item
56+
item = Item.from_orm(item_in, update={"owner_id": current_user.id})
57+
session.add(item)
58+
session.commit()
59+
session.refresh(item)
60+
return item # type: ignore
6361

6462

65-
@router.get("/{id}", response_model=schemas.Item)
66-
def read_item(
67-
*,
68-
db: Session = Depends(deps.get_db),
69-
id: int,
70-
current_user: models.User = Depends(deps.get_current_active_user),
71-
) -> Any:
63+
@router.put("/{id}")
64+
def update_item(
65+
*, session: SessionDep, current_user: CurrentUser, id: int, item_in: ItemUpdate
66+
) -> ItemOut:
7267
"""
73-
Get item by ID.
68+
Update an item.
7469
"""
75-
item = crud.item.get(db=db, id=id)
70+
item = session.get(Item, id)
7671
if not item:
7772
raise HTTPException(status_code=404, detail="Item not found")
78-
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
73+
if not current_user.is_superuser and (item.owner_id != current_user.id):
7974
raise HTTPException(status_code=400, detail="Not enough permissions")
80-
return item
75+
# TODO: check this actually works
76+
update_dict = item_in.dict(exclude_unset=True)
77+
item.from_orm(update_dict)
78+
session.add(item)
79+
session.commit()
80+
session.refresh(item)
81+
return item # type: ignore
8182

8283

83-
@router.delete("/{id}", response_model=schemas.Item)
84-
def delete_item(
85-
*,
86-
db: Session = Depends(deps.get_db),
87-
id: int,
88-
current_user: models.User = Depends(deps.get_current_active_user),
89-
) -> Any:
84+
@router.delete("/{id}")
85+
def delete_item(session: SessionDep, current_user: CurrentUser, id: int) -> ItemOut:
9086
"""
9187
Delete an item.
9288
"""
93-
item = crud.item.get(db=db, id=id)
89+
item = session.get(Item, id)
9490
if not item:
9591
raise HTTPException(status_code=404, detail="Item not found")
96-
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id):
92+
if not current_user.is_superuser and (item.owner_id != current_user.id):
9793
raise HTTPException(status_code=400, detail="Not enough permissions")
98-
item = crud.item.remove(db=db, id=id)
99-
return item
94+
session.delete(item)
95+
session.commit()
96+
return item # type: ignore

0 commit comments

Comments
 (0)