|
1 | | -from typing import Any, List |
| 1 | +from typing import Annotated |
2 | 2 |
|
3 | 3 | from fastapi import APIRouter, Depends, HTTPException |
4 | | -from sqlalchemy.orm import Session |
| 4 | +from sqlmodel import Session, select |
5 | 5 |
|
6 | | -from app import crud, models, schemas |
7 | 6 | from app.api import deps |
| 7 | +from app.models import Item, ItemCreate, ItemOut, ItemUpdate, User |
8 | 8 |
|
9 | 9 | router = APIRouter() |
10 | 10 |
|
| 11 | +SessionDep = Annotated[Session, Depends(deps.get_db)] |
| 12 | +CurrentUser = Annotated[User, Depends(deps.get_current_active_user)] |
11 | 13 |
|
12 | | -@router.get("/", response_model=List[schemas.Item]) |
| 14 | + |
| 15 | +@router.get("/") |
13 | 16 | 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]: |
19 | 19 | """ |
20 | 20 | Retrieve items. |
21 | 21 | """ |
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 |
24 | 26 | 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) |
27 | 32 | ) |
28 | | - return items |
| 33 | + return session.exec(statement).all() # type: ignore |
29 | 34 |
|
30 | 35 |
|
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: |
38 | 38 | """ |
39 | | - Create new item. |
| 39 | + Get item by ID. |
40 | 40 | """ |
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 |
43 | 47 |
|
44 | 48 |
|
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: |
53 | 53 | """ |
54 | | - Update an item. |
| 54 | + Create new item. |
55 | 55 | """ |
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 |
63 | 61 |
|
64 | 62 |
|
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: |
72 | 67 | """ |
73 | | - Get item by ID. |
| 68 | + Update an item. |
74 | 69 | """ |
75 | | - item = crud.item.get(db=db, id=id) |
| 70 | + item = session.get(Item, id) |
76 | 71 | if not item: |
77 | 72 | 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): |
79 | 74 | 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 |
81 | 82 |
|
82 | 83 |
|
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: |
90 | 86 | """ |
91 | 87 | Delete an item. |
92 | 88 | """ |
93 | | - item = crud.item.get(db=db, id=id) |
| 89 | + item = session.get(Item, id) |
94 | 90 | if not item: |
95 | 91 | 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): |
97 | 93 | 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