Spaces:
Paused
Paused
import app.sql.models as models | |
import app.core.schemas as schemas | |
from sqlalchemy.orm import Session | |
from passlib.context import CryptContext | |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
def hash_password(password: str): | |
return pwd_context.hash(password) | |
def create_prompt(prompt: str, user_id: int, db: Session): | |
db_prompt = models.Prompt(prompt=prompt, user_id=user_id) | |
db.add(db_prompt) | |
db.commit() | |
db.refresh(db_prompt) | |
return db_prompt | |
def get_all_prompts(db: Session): | |
return db.query(models.Prompt).all() | |
def get_prompt_by_user_id(user_id: int, db: Session): | |
return db.query(models.Prompt).filter(models.Prompt.user_id == user_id).all() | |
def create_user(user: schemas.UserCreate, db: Session): | |
hashed_password = hash_password(user.password) | |
db_user = models.User(username=user.username, password=hashed_password) | |
db.add(db_user) | |
db.commit() | |
db.refresh(db_user) | |
return db_user | |
def update_user(user: schemas.UserUpdate, db: Session): | |
db_user = db.query(models.User).filter( | |
models.User.user_id == user.user_id).first() | |
db_user.is_active = user.is_active | |
db_user.is_superuser = user.is_superuser | |
db.commit() | |
db.refresh(db_user) | |
return db_user | |
def get_all_users(db: Session): | |
return db.query(models.User).all() | |
def get_user_by_user_id(user_id: int, db: Session): | |
return db.query(models.User).filter(models.User.user_id == user_id).first() | |
def get_user_by_username(username: str, db: Session): | |
return db.query(models.User).filter(models.User.username == username).first() | |