🎉 Initialize FastAPI boilerplate
This commit is contained in:
parent
06ffc42ec6
commit
248d73806f
21 changed files with 638 additions and 0 deletions
1
app/crud/__init__.py
Normal file
1
app/crud/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .item import item
|
70
app/crud/base.py
Normal file
70
app/crud/base.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import HTTPException
|
||||
|
||||
from database import Base
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
||||
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
||||
|
||||
|
||||
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
def __init__(self, model: Type[ModelType]):
|
||||
"""
|
||||
CRUD object with default methods to Create, Read, Update, Delete (CRUD).
|
||||
|
||||
**Parameters**
|
||||
|
||||
* `model`: A SQLAlchemy model class
|
||||
* `schema`: A Pydantic model (schema) class
|
||||
"""
|
||||
self.model = model
|
||||
|
||||
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
||||
result = db.query(self.model).filter(self.model.id == id).first()
|
||||
if not result:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return result
|
||||
|
||||
def get_multi(
|
||||
self, db: Session, *, skip: int = 0, limit: int = 100
|
||||
) -> List[ModelType]:
|
||||
return db.query(self.model).offset(skip).limit(limit).all()
|
||||
|
||||
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
||||
obj_in_data = jsonable_encoder(obj_in)
|
||||
db_obj = self.model(**obj_in_data) # type: ignore
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update(
|
||||
self,
|
||||
db: Session,
|
||||
*,
|
||||
db_obj: ModelType,
|
||||
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
||||
) -> ModelType:
|
||||
obj_data = jsonable_encoder(db_obj)
|
||||
if isinstance(obj_in, dict):
|
||||
update_data = obj_in
|
||||
else:
|
||||
update_data = obj_in.dict(exclude_unset=True)
|
||||
for field in obj_data:
|
||||
if field in update_data:
|
||||
setattr(db_obj, field, update_data[field])
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def remove(self, db: Session, *, id: int) -> ModelType:
|
||||
obj = db.query(self.model).get(id)
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
return obj
|
40
app/crud/item.py
Normal file
40
app/crud/item.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models import Item
|
||||
from schemas.schemas import ItemCreate, ItemUpdate
|
||||
from crud.base import CRUDBase
|
||||
|
||||
|
||||
class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]):
|
||||
pass
|
||||
# def get_item(self, db: Session, item_id: int):
|
||||
# item = db.query(Item).filter(Item.id == item_id).first()
|
||||
# if not item:
|
||||
# raise HTTPException(status_code=404, detail="Item not found")
|
||||
# return item
|
||||
|
||||
# def get_items(self, db: Session, skip: int = 0, limit: int = 100):
|
||||
# return db.query(Item).offset(skip).limit(limit).all()
|
||||
|
||||
# def create_item(self, db: Session, item: ItemCreate):
|
||||
# db_item = Item(title=item.title, description=item.description)
|
||||
# db.add(db_item)
|
||||
# db.commit()
|
||||
# db.refresh(db_item)
|
||||
# return db_item
|
||||
|
||||
# def update_item(self, db: Session, item: ItemUpdate):
|
||||
# db_item = Item(title=item.title, description=item.description)
|
||||
# db.add(db_item)
|
||||
# db.commit()
|
||||
# db.refresh(db_item)
|
||||
# return db_item
|
||||
|
||||
# def delete_item(self, db: Session, item_id: int):
|
||||
# db.query(Item).filter(Item.id == item_id).first().delete()
|
||||
# db.commit()
|
||||
# return
|
||||
|
||||
|
||||
item = CRUDItem(Item)
|
Loading…
Add table
Add a link
Reference in a new issue