-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utils and items endpoints to API router
- Loading branch information
Showing
8 changed files
with
295 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
from typing import List | ||
|
||
from fastapi.encoders import jsonable_encoder | ||
from sqlalchemy.orm import Session | ||
from sqlmodel import col | ||
|
||
from bemore.crud.base import CRUDBase | ||
from bemore.models import Item | ||
from bemore.schemas.item import ItemCreate, ItemUpdate | ||
|
||
|
||
class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]): | ||
def create_with_owner( | ||
self, db: Session, *, obj_in: ItemCreate, owner_id: int | ||
) -> Item: | ||
obj_in_data = jsonable_encoder(obj_in) | ||
db_obj = self.model(**obj_in_data, owner_id=owner_id) | ||
def create(self, db: Session, *, obj_in: ItemCreate) -> Item: | ||
db_obj = Item( | ||
title=obj_in.title, | ||
description=obj_in.description, | ||
keywords=obj_in.keywords, | ||
raw_url=obj_in.raw_url, | ||
) | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
return db_obj | ||
|
||
def get_multi_by_owner( | ||
self, db: Session, *, owner_id: int, skip: int = 0, limit: int = 100 | ||
) -> List[Item]: | ||
return ( | ||
db.query(self.model) | ||
.filter(Item.owner_id == owner_id) | ||
.offset(skip) | ||
.limit(limit) | ||
.all() | ||
) | ||
def update(self, db: Session, *, db_obj: Item, obj_in: ItemUpdate) -> Item: | ||
if obj_in.title: | ||
db_obj.title = obj_in.title | ||
if obj_in.description: | ||
db_obj.description = obj_in.description | ||
if obj_in.keywords: | ||
db_obj.keywords = obj_in.keywords | ||
if obj_in.raw_url: | ||
db_obj.raw_url = obj_in.raw_url | ||
return super().update(db, db_obj=db_obj, obj_in=obj_in) | ||
|
||
def get_by_id(self, db: Session, *, id: int) -> Item: | ||
return db.query(Item).filter(Item.id == id).first() | ||
|
||
def get_by_fuzzy_title(self, db: Session, *, title: str) -> list[Item]: | ||
return db.query(Item).filter(col("title").ilike(f"%{title}%")).all() | ||
|
||
def create_bulk(self, db: Session, *, objs_in: list[ItemCreate]) -> list[Item]: | ||
objs = [Item(**obj_in.model_dump()) for obj_in in objs_in] | ||
db.add_all(objs) | ||
db.commit() | ||
db.refresh(objs) | ||
return objs | ||
|
||
|
||
item = CRUDItem(Item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from typing import Any | ||
|
||
from fastapi import APIRouter, HTTPException | ||
from sqlmodel import select | ||
|
||
from bemore.crud.crud_item import item as crud | ||
from bemore.web.api.deps import CurrentUser, SessionDep | ||
from bemore.models import Item, ItemCreate, ItemOut, ItemUpdate | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/", response_model=list[ItemOut]) | ||
def read_items( | ||
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100 | ||
) -> list[ItemOut]: | ||
""" | ||
Retrieve items. | ||
""" | ||
if current_user.is_superuser: | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
statement = select(Item).offset(skip).limit(limit) | ||
return session.exec(statement).all() | ||
|
||
|
||
@router.get("/{id}", response_model=ItemOut) | ||
def read_item(session: SessionDep, id: int) -> Any: | ||
""" | ||
Get item by ID. | ||
""" | ||
item = session.get(Item, id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
return item | ||
|
||
|
||
@router.get("/fuzzy/{title}", response_model=list[ItemOut]) | ||
def read_item_fuzzy(session: SessionDep, title: str) -> Any: | ||
""" | ||
Get item by fuzzy title. | ||
""" | ||
statement = crud.get_by_fuzzy_title(session, title=title) | ||
return statement | ||
|
||
|
||
@router.post("/", response_model=ItemOut) | ||
def create_item(*, session: SessionDep, item_in: ItemCreate) -> Any: | ||
""" | ||
Create new item. | ||
""" | ||
item = crud.create(session, obj_in=item_in) | ||
return item | ||
|
||
|
||
@router.post("/bulk", response_model=list[ItemOut]) | ||
def create_items(*, session: SessionDep, items_in: list[ItemCreate]) -> Any: | ||
""" | ||
Create new items. | ||
""" | ||
items = crud.create_bulk(session, objs_in=items_in) | ||
return items | ||
|
||
|
||
@router.put("/{id}", response_model=ItemOut) | ||
def update_item( | ||
*, session: SessionDep, current_user: CurrentUser, id: int, item_in: ItemUpdate | ||
) -> Any: | ||
""" | ||
Update an item. | ||
""" | ||
if not current_user.is_superuser: | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
item = crud.get(session, id=id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
item = crud.update(session, db_obj=item, obj_in=item_in) | ||
return item | ||
|
||
|
||
@router.delete("/{id}", response_model=ItemOut) | ||
def delete_item(session: SessionDep, current_user: CurrentUser, id: int) -> ItemOut: | ||
""" | ||
Delete an item. | ||
""" | ||
item = crud.get(session, id=id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not current_user.is_superuser: | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
item = crud.remove(session, id=id) | ||
return item |
Oops, something went wrong.