tmpl-fastapi/app/sql/crud.py

39 lines
753 B
Python
Raw Normal View History

2023-02-19 16:49:44 +03:00
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[Optional[models.User]]:
return db \
.query(models.User) \
.offset(skip) \
.limit(limit) \
.all()
def create_user(
db: Session,
user: schemas.User) -> models.User:
user_model = models.User(**user.dict())
db.add(user_model)
db.commit()
db.refresh(user_model)
return user_model