-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
62 lines (41 loc) · 1.37 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from models import Users
from sqlalchemy.orm import Session
import models, schemas
import json
from database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def get_user(db: Session, user_name: str):
return db.query(models.Users).filter(models.Users.name == user_name)
def get_user_by_email(db: Session, email: str):
return db.query(models.Users).filter(models.Users.email == email).first()
def get_users( skip: int = 0, limit: int = 100):
with SessionLocal() as db:
users_data = db.query(models.Users).offset(skip).limit(limit).all()
#print(users_data)
temp_data = [user.__dict__ for user in users_data]
#print(temp_data)
return temp_data
def create_user(name, email, password):
with SessionLocal() as db:
secret_password = password + "notreallyhashed"
db_user = models.Users(name=name, email=email, password=password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def delete_user(name):
with SessionLocal() as db:
try:
db_user = db.query(models.Users).filter(models.Users.name == name).delete()
db.commit()
#db.refresh(db_user)
return True
except Exception as e:
print(e)
return False