-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrud.py
40 lines (25 loc) · 1.18 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from sqlalchemy.orm import Session
import models, schemas
import bcrypt
def get_user_by_username(db: Session, username: str):
return db.query(models.UserInfo).filter(models.UserInfo.username == username).first()
def create_user(db: Session, user: schemas.UserCreate):
hashed_password = bcrypt.hashpw(user.password.encode('utf-8'), bcrypt.gensalt())
db_user = models.UserInfo(username=user.username, password=hashed_password, fullname=user.fullname)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def check_username_password(db: Session, user: schemas.UserAuthenticate):
db_user_info: models.UserInfo = get_user_by_username(db, username=user.username)
return bcrypt.checkpw(user.password.encode('utf-8'), db_user_info.password.encode('utf-8'))
def create_new_blog(db: Session, blog: schemas.BlogBase):
db_blog = models.Blog(title=blog.title, content=blog.content)
db.add(db_blog)
db.commit()
db.refresh(db_blog)
return db_blog
def get_all_blogs(db: Session):
return db.query(models.Blog).all()
def get_blog_by_id(db: Session, blog_id: int):
return db.query(models.Blog).filter(models.Blog.id == blog_id).first()