-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sqlalchemy.orm import Session | ||
|
||
from app.models import User | ||
from app.schemas import UserBase | ||
|
||
|
||
def get_user(db: Session, user_id: str): | ||
return db.query(User).filter(User.user_id == user_id).first() | ||
|
||
|
||
def get_user_by_user_id(db: Session, user_id: str): | ||
return db.query(User).filter(User.user_id == user_id).first() | ||
|
||
|
||
def get_user_by_token(db: Session, token: str): | ||
return db.query(User).filter(User.token == token).first() | ||
|
||
|
||
def create_user(db: Session, user: UserBase): | ||
# first we delete any existing user | ||
delete_user(db, user_id=user["user_id"]) | ||
# then we (re)create a user | ||
db_user = User(user_id=user["user_id"], token=user["token"]) | ||
db.add(db_user) | ||
db.commit() | ||
db.refresh(db_user) | ||
return db_user | ||
|
||
|
||
def delete_user(db: Session, user_id: UserBase): | ||
db_user = get_user_by_user_id(db, user_id=user_id) | ||
if db_user: | ||
db.delete(db_user) | ||
db.commit() | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pydantic import BaseModel | ||
from pydantic import ConfigDict | ||
|
||
|
||
class UserBase(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
user_id: str | ||
token: str |