-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
132 lines (124 loc) · 4.89 KB
/
db.py
File metadata and controls
132 lines (124 loc) · 4.89 KB
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
127
128
129
130
131
132
import pyodbc
# פונקציה ליצירת חיבור חדש למסד הנתונים
def get_db_connection():
return pyodbc.connect(
"DRIVER={ODBC Driver 17 for SQL Server};"
"SERVER=localhost;"
"DATABASE=RecipeDB;"
"Trusted_Connection=yes;"
)
def get_all_recipes():
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM Recipes")
# חילוץ שמות העמודות כדי להפוך את התוצאה למילון
columns = [column[0] for column in cursor.description]
return [dict(zip(columns, row)) for row in cursor.fetchall()]
except pyodbc.Error as e:
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()
def get_recipe_by_id(recipe_id):
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM Recipes WHERE RecipeCode = ?", (recipe_id,))
columns = [column[0] for column in cursor.description]
row = cursor.fetchone()
if not row:
return {"error": "מתכון לא נמצא"}
return dict(zip(columns, row))
except pyodbc.Error as e:
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()
def get_recipe_by_category(category_id):
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM Recipes WHERE CategoryID = ?", (category_id,))
columns = [column[0] for column in cursor.description]
rows = cursor.fetchall()
if not rows:
return {"error": "לא נמצאו מתכונים בקטגוריה זו"}
return [dict(zip(columns, row)) for row in rows]
except pyodbc.Error as e:
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()
def add_recipe(recipe_data):
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute(
"""
INSERT INTO Recipes (RecipeName, CategoryID, PreparationMethod, Products, PrepTime, CommentCount, TotalPoints ,ImageUrl)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
recipe_data.get("RecipeName"),
recipe_data.get("CategoryID"),
recipe_data.get("PreparationMethod"),
recipe_data.get("Products"),
recipe_data.get("PrepTime"),
recipe_data.get("CommentCount"),
recipe_data.get("TotalPoints"),
recipe_data.get("ImageUrl")
)
)
conn.commit()
return {"success": "המתכון נוסף בהצלחה"}
except pyodbc.Error as e:
conn.rollback()
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()
def delete_recipe(recipe_id):
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute("DELETE FROM Recipes WHERE RecipeCode = ?", (recipe_id,))
conn.commit()
return {"success": "המתכון נמחק בהצלחה"}
except pyodbc.Error as e:
conn.rollback()
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()
def update_recipe(recipe_data,RecipeCode):
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute(
"""
UPDATE Recipes
SET RecipeName = ?,
CategoryID = ?,
PreparationMethod = ?,
Products = ?,
PrepTime = ?,
CommentCount = ?,
TotalPoints = ?,
ImageUrl = ?
WHERE RecipeCode = ?
""",
(
recipe_data.get("RecipeName"),
recipe_data.get("CategoryID"),
recipe_data.get("PreparationMethod"),
recipe_data.get("Products"),
recipe_data.get("PrepTime"),
recipe_data.get("CommentCount"),
recipe_data.get("TotalPoints"),
recipe_data.get("ImageUrl"),
RecipeCode
)
)
conn.commit()
return {"success": "המתכון עודכן בהצלחה"}
except pyodbc.Error as e:
conn.rollback()
return {"error": f"שגיאה בבסיס הנתונים: {str(e)}"}
finally:
conn.close()