Coverage for docs_src/sql_databases/sql_app/crud.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from sqlalchemy.orm import Session 1abcde
3from . import models, schemas 1abcde
6def get_user(db: Session, user_id: int): 1abcde
7 return db.query(models.User).filter(models.User.id == user_id).first() 1abcde
10def get_user_by_email(db: Session, email: str): 1abcde
11 return db.query(models.User).filter(models.User.email == email).first() 1abcde
14def get_users(db: Session, skip: int = 0, limit: int = 100): 1abcde
15 return db.query(models.User).offset(skip).limit(limit).all() 1abcde
18def create_user(db: Session, user: schemas.UserCreate): 1abcde
19 fake_hashed_password = user.password + "notreallyhashed" 1abcde
20 db_user = models.User(email=user.email, hashed_password=fake_hashed_password) 1abcde
21 db.add(db_user) 1abcde
22 db.commit() 1abcde
23 db.refresh(db_user) 1abcde
24 return db_user 1abcde
27def get_items(db: Session, skip: int = 0, limit: int = 100): 1abcde
28 return db.query(models.Item).offset(skip).limit(limit).all() 1abcde
31def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int): 1abcde
32 db_item = models.Item(**item.dict(), owner_id=user_id) 1abcde
33 db.add(db_item) 1abcde
34 db.commit() 1abcde
35 db.refresh(db_item) 1abcde
36 return db_item 1abcde