예제 #1
0
def get_product(
    product_id: int = Path(...),
    db: sa.orm.Session = get_db,
) -> ProductSchema:
    product = db.get(Product, product_id)
    if product is None:
        HTTPException(404, "Product not found.")
    return product
예제 #2
0
def read_user_by_id(
        user_id: int = Path(...),
        db: sa.orm.Session = get_db,
) -> UserSchema:
    """
    Get a specific user by id.
    """
    user = db.get(User, user_id)
    if user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return user
예제 #3
0
def update_user(
        updated_user: UpdateUserSchema,
        user_id: int = Path(...),
        db: sa.orm.Session = get_db,
) -> UserSchema:
    """
    Update a user.
    """
    user = db.get(User, user_id)
    if user is None:
        raise HTTPException(status_code=404, detail="User not found")

    updated_user = updated_user.dict(exclude_unset=True)
    try:
        updated_user["password"] = hash_password(updated_user["password"])
    except KeyError:
        pass

    user.update(updated_user)
    db.commit()

    return user