tmpl-fastapi/app/sql/crud.py
2023-02-20 12:09:43 +04:00

39 lines
750 B
Python

from typing import Optional, List
from sqlalchemy.orm import Session
from . import models
from . import schemas
def get_user(
db: Session,
user_id: int) -> Optional[models.User]:
return db \
.query(models.User) \
.filter(models.User.id == user_id) \
.first()
def get_users(
db: Session,
skip: int = 0,
limit: int = 100) -> List[models.User]:
return db \
.query(models.User) \
.offset(skip) \
.limit(limit) \
.all()
def create_user(
db: Session,
user: schemas.UserCreate) -> models.User:
user_model = models.User(**user.dict())
db.add(user_model)
db.commit()
db.refresh(user_model)
return user_model