-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_operations.py
95 lines (68 loc) · 2.55 KB
/
crud_operations.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# crud_operations.py
from uuid import UUID
from sqlalchemy.orm import Session, aliased
import models
import schemas as schema
from security import hash_password
"""
CRUD operations for the database.
"""
def create_user(db: Session, user: schema.UserCreate):
default_password = "123456789" # using common password for demo purposes
hashed_password = hash_password(default_password)
db_user = models.User(name=user.name, hashed_password=hashed_password)
try:
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
except Exception as e:
db.rollback()
print(f"Error: {e}")
return None
def get_users(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.User).offset(skip).limit(limit).all()
def get_user_by_id(db: Session, user_id: str):
return db.query(models.User).filter(models.User.id == user_id).first()
def get_user_by_username(db: Session, username: str):
return db.query(models.User).filter(models.User.name == username).first()
def update_user(db: Session, user: schema.User):
db_user = db.query(models.User).filter(models.User.id == user.id).first()
for key, value in user.dict().items():
setattr(db_user, key, value)
db.commit()
db.refresh(db_user)
return db_user
def delete_user(db: Session, user_id: str):
db_user = db.query(models.User).filter(models.User.id == user_id).first()
db.delete(db_user)
db.commit()
return db_user
def get_departments(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.Department).offset(skip).limit(limit).all()
def get_department_by_name(db: Session, department_name: str):
return (
db.query(models.Department)
.filter(models.Department.name == department_name)
.first()
)
def get_department_roles(db: Session, skip: int = 0, limit: int = 10):
return db.query(models.DepartmentRole).offset(skip).limit(limit).all()
def get_department_by_id(db: Session, department_id: str):
return (
db.query(models.Department)
.filter(models.Department.id == department_id)
.first()
)
def get_department_role_by_id(db: Session, department_role_id: str):
return (
db.query(models.DepartmentRole)
.filter(models.DepartmentRole.id == department_role_id)
.first()
)
def get_department_roles_by_department_id(db: Session, department_id: UUID):
return (
db.query(models.DepartmentRole)
.filter(models.DepartmentRole.department_id == department_id)
.all()
)