2024-07-22 17:49:02 -05:00
|
|
|
import uuid
|
2023-12-27 14:39:42 -05:00
|
|
|
from typing import Any
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
2023-12-27 14:39:42 -05:00
|
|
|
from fastapi import APIRouter, HTTPException
|
2024-02-25 19:39:33 +01:00
|
|
|
from sqlmodel import func, select
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
2023-11-29 12:13:15 -05:00
|
|
|
from app.api.deps import CurrentUser, SessionDep
|
2024-04-06 16:53:49 -05:00
|
|
|
from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2023-11-25 00:11:02 +01:00
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
@router.get("/", response_model=ItemsPublic)
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
def read_items(
|
2023-11-25 00:11:02 +01:00
|
|
|
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
|
2023-12-27 13:37:05 -05:00
|
|
|
) -> Any:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
|
|
|
Retrieve items.
|
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
|
|
|
|
if current_user.is_superuser:
|
2024-03-13 18:06:22 +00:00
|
|
|
count_statement = select(func.count()).select_from(Item)
|
|
|
|
count = session.exec(count_statement).one()
|
2023-11-25 00:11:02 +01:00
|
|
|
statement = select(Item).offset(skip).limit(limit)
|
2024-02-25 19:39:33 +01:00
|
|
|
items = session.exec(statement).all()
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
else:
|
2024-03-13 18:06:22 +00:00
|
|
|
count_statement = (
|
2024-03-12 13:23:33 -05:00
|
|
|
select(func.count())
|
|
|
|
.select_from(Item)
|
|
|
|
.where(Item.owner_id == current_user.id)
|
|
|
|
)
|
2024-03-13 18:06:22 +00:00
|
|
|
count = session.exec(count_statement).one()
|
2023-11-25 00:11:02 +01:00
|
|
|
statement = (
|
|
|
|
select(Item)
|
|
|
|
.where(Item.owner_id == current_user.id)
|
|
|
|
.offset(skip)
|
|
|
|
.limit(limit)
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
)
|
2024-02-25 10:04:47 -05:00
|
|
|
items = session.exec(statement).all()
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
return ItemsPublic(data=items, count=count)
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
@router.get("/{id}", response_model=ItemPublic)
|
2024-07-22 17:49:02 -05:00
|
|
|
def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
Get item by ID.
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
item = session.get(Item, id)
|
|
|
|
if not item:
|
|
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
|
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
2023-12-27 13:37:05 -05:00
|
|
|
return item
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
@router.post("/", response_model=ItemPublic)
|
2023-11-25 00:11:02 +01:00
|
|
|
def create_item(
|
|
|
|
*, session: SessionDep, current_user: CurrentUser, item_in: ItemCreate
|
2023-12-27 13:37:05 -05:00
|
|
|
) -> Any:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
Create new item.
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2024-02-17 21:29:15 +01:00
|
|
|
item = Item.model_validate(item_in, update={"owner_id": current_user.id})
|
2023-11-25 00:11:02 +01:00
|
|
|
session.add(item)
|
|
|
|
session.commit()
|
|
|
|
session.refresh(item)
|
2023-12-27 13:37:05 -05:00
|
|
|
return item
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
|
|
|
|
2024-04-06 16:53:49 -05:00
|
|
|
@router.put("/{id}", response_model=ItemPublic)
|
2023-11-25 00:11:02 +01:00
|
|
|
def update_item(
|
2024-07-22 17:49:02 -05:00
|
|
|
*,
|
|
|
|
session: SessionDep,
|
|
|
|
current_user: CurrentUser,
|
|
|
|
id: uuid.UUID,
|
|
|
|
item_in: ItemUpdate,
|
2023-12-27 13:37:05 -05:00
|
|
|
) -> Any:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
Update an item.
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
item = session.get(Item, id)
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
if not item:
|
2020-02-07 17:28:45 -03:00
|
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
2023-11-25 00:11:02 +01:00
|
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
2024-02-17 21:29:15 +01:00
|
|
|
update_dict = item_in.model_dump(exclude_unset=True)
|
|
|
|
item.sqlmodel_update(update_dict)
|
2023-11-25 00:11:02 +01:00
|
|
|
session.add(item)
|
|
|
|
session.commit()
|
|
|
|
session.refresh(item)
|
2023-12-27 13:37:05 -05:00
|
|
|
return item
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
|
|
|
|
|
2024-02-13 09:25:58 -05:00
|
|
|
@router.delete("/{id}")
|
2024-07-22 17:49:02 -05:00
|
|
|
def delete_item(
|
|
|
|
session: SessionDep, current_user: CurrentUser, id: uuid.UUID
|
|
|
|
) -> Message:
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
"""
|
|
|
|
Delete an item.
|
|
|
|
"""
|
2023-11-25 00:11:02 +01:00
|
|
|
item = session.get(Item, id)
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
if not item:
|
|
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
2023-11-25 00:11:02 +01:00
|
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
:sparkles: Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better.
* Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc.
* Upgrade packages.
* Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case.
* Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`.
* Update testing utils.
* Update linting rules, relax vulture to reduce false positives.
* Update migrations to include new Items.
* Update project README.md with tips about how to start with backend.
2019-04-19 09:45:23 +04:00
|
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
2023-11-25 00:11:02 +01:00
|
|
|
session.delete(item)
|
|
|
|
session.commit()
|
2024-02-13 09:25:58 -05:00
|
|
|
return Message(message="Item deleted successfully")
|