-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
126 lines (90 loc) · 3.14 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import schemas
from sqlalchemy.orm import Session
from apps.User.models import User
from apps.Idea.models import Direction, Idea
# User
def create_user(db: Session, schema: schemas.UserCreate):
db_user = User(**schema.model_dump())
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_users(db: Session):
return db.query(User).all()
def get_user(db: Session, user_id: int):
return db.query(User).filter_by(id=user_id).first()
def update_user(db: Session, user_id: int, data: schemas.UserUpdate):
db_user = db.query(User).filter_by(id=user_id).first()
data = data if isinstance(data, dict) else data.model_dump()
if db_user:
for key, value in data.items():
if hasattr(db_user, key):
setattr(db_user, key, value)
db.commit()
db.refresh(db_user)
return db_user
return None
def delete_user(db: Session, user_id: int):
db_user = db.query(User).filter_by(id=user_id).first()
if db_user:
db.delete(db_user)
db.commit()
return True
return False
# Direction
def create_direction(db: Session, schema: schemas.DirectionCreate):
db_direction = Direction(**schema.model_dump())
db.add(db_direction)
db.commit()
db.refresh(db_direction)
return db_direction
def get_directions(db: Session):
return db.query(Direction).all()
def get_direction(db: Session, direction_id: int):
return db.query(Direction).filter_by(id=direction_id).first()
def update_direction(db: Session, direction_id: int, data: schemas.DirectionUpdate | dict):
db_direction = db.query(Direction).filter_by(id=direction_id).first()
data = data if isinstance(data, dict) else data.model_dump()
if db_direction:
for key, value in data.items():
if hasattr(db_direction, key):
setattr(db_direction, key, value)
db.commit()
db.refresh(db_direction)
return db_direction
def delete_direction(db: Session, direction_id: int):
db_direction = db.query(Direction).filter_by(id=direction_id).first()
if db_direction:
db.delete(db_direction)
db.commit()
return True
return False
# Idea
def create_idea(db: Session, schema: schemas.IdeaCreate):
db_idea = Idea(**schema.model_dump())
db.add(db_idea)
db.commit()
db.refresh(db_idea)
return db_idea
def get_ideas(db: Session):
return db.query(Idea).all()
def get_idea(db: Session, idea_id: int):
return db.query(Idea).filter_by(id=idea_id).first()
def update_idea(db: Session, idea_id: int, data: schemas.IdeaUpdate | dict):
db_idea = db.query(Idea).filter_by(id=idea_id).first()
data = data if isinstance(data, dict) else data.model_dump()
if db_idea:
for key, value in data.items():
if hasattr(db_idea, key):
setattr(db_idea, key, value)
db.commit()
db.refresh(db_idea)
return db_idea
return None
def delete_idea(db: Session, idea_id: int):
db_idea = db.query(Idea).filter_by(id=idea_id).first()
if db_idea:
db.delete(db_idea)
db.commit()
return True
return False