markobinario commited on
Commit
c6af53f
·
verified ·
1 Parent(s): 61b18b3

Update database_connection.py

Browse files
Files changed (1) hide show
  1. database_connection.py +87 -57
database_connection.py CHANGED
@@ -1,57 +1,87 @@
1
- import requests
2
- import pandas as pd
3
- from typing import Dict, List, Optional
4
- import json
5
-
6
- class DatabaseConnection:
7
- def __init__(self, base_url: str = "https://database-dhe2.onrender.com"):
8
- self.base_url = base_url
9
- self.session = requests.Session()
10
-
11
- def get_student_feedback_counts(self) -> pd.DataFrame:
12
- """Fetch student feedback data from the database"""
13
- try:
14
- url = f"{self.base_url}/student_feedback_counts"
15
- response = self.session.get(url)
16
- response.raise_for_status()
17
-
18
- data = response.json()
19
- if isinstance(data, list):
20
- return pd.DataFrame(data)
21
- else:
22
- return pd.DataFrame([data])
23
- except Exception as e:
24
- print(f"Error fetching data: {e}")
25
- return pd.DataFrame()
26
-
27
- def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
28
- rating: int, hobbies: str) -> bool:
29
- """Add new feedback to the database"""
30
- try:
31
- url = f"{self.base_url}/student_feedback_counts"
32
- data = {
33
- "course": course,
34
- "stanine": stanine,
35
- "gwa": gwa,
36
- "strand": strand,
37
- "rating": rating,
38
- "hobbies": hobbies
39
- }
40
- response = self.session.post(url, json=data)
41
- response.raise_for_status()
42
- return True
43
- except Exception as e:
44
- print(f"Error adding feedback: {e}")
45
- return False
46
-
47
- def update_feedback_count(self, feedback_id: int, count: int) -> bool:
48
- """Update the count for existing feedback"""
49
- try:
50
- url = f"{self.base_url}/student_feedback_counts/{feedback_id}"
51
- data = {"count": count}
52
- response = self.session.put(url, json=data)
53
- response.raise_for_status()
54
- return True
55
- except Exception as e:
56
- print(f"Error updating feedback count: {e}")
57
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pandas as pd
3
+ from typing import Dict, List, Optional
4
+ import json
5
+
6
+ class DatabaseConnection:
7
+ def __init__(self, base_url: str = "https://database-dhe2.onrender.com"):
8
+ self.base_url = base_url
9
+ self.session = requests.Session()
10
+
11
+ def get_student_feedback_counts(self) -> pd.DataFrame:
12
+ """Fetch student feedback data from the database"""
13
+ try:
14
+ url = f"{self.base_url}/student_feedback_counts"
15
+ response = self.session.get(url)
16
+ response.raise_for_status()
17
+
18
+ data = response.json()
19
+ if isinstance(data, list):
20
+ return pd.DataFrame(data)
21
+ elif isinstance(data, dict) and 'feedback_counts' in data:
22
+ # Handle nested structure
23
+ feedback_data = data['feedback_counts']
24
+ if isinstance(feedback_data, list):
25
+ return pd.DataFrame(feedback_data)
26
+ else:
27
+ return pd.DataFrame([feedback_data])
28
+ else:
29
+ return pd.DataFrame([data])
30
+ except Exception as e:
31
+ print(f"Error fetching data: {e}")
32
+ return pd.DataFrame()
33
+
34
+ def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
35
+ rating: int, hobbies: str) -> bool:
36
+ """Add new feedback to the database"""
37
+ try:
38
+ url = f"{self.base_url}/student_feedback_counts"
39
+ data = {
40
+ "course": course,
41
+ "stanine": stanine,
42
+ "gwa": gwa,
43
+ "strand": strand,
44
+ "rating": rating,
45
+ "hobbies": hobbies
46
+ }
47
+ response = self.session.post(url, json=data)
48
+ response.raise_for_status()
49
+ return True
50
+ except Exception as e:
51
+ print(f"Error adding feedback: {e}")
52
+ return False
53
+
54
+ def update_feedback_count(self, feedback_id: int, count: int) -> bool:
55
+ """Update the count for existing feedback"""
56
+ try:
57
+ url = f"{self.base_url}/student_feedback_counts/{feedback_id}"
58
+ data = {"count": count}
59
+ response = self.session.put(url, json=data)
60
+ response.raise_for_status()
61
+ return True
62
+ except Exception as e:
63
+ print(f"Error updating feedback count: {e}")
64
+ return False
65
+
66
+ def get_available_courses(self) -> List[str]:
67
+ """Fetch available courses from the database"""
68
+ try:
69
+ url = f"{self.base_url}/courses"
70
+ response = self.session.get(url)
71
+ response.raise_for_status()
72
+
73
+ data = response.json()
74
+ if isinstance(data, list):
75
+ # Extract course names from the data
76
+ courses = []
77
+ for item in data:
78
+ if isinstance(item, dict) and 'name' in item:
79
+ courses.append(item['name'])
80
+ elif isinstance(item, str):
81
+ courses.append(item)
82
+ return courses
83
+ else:
84
+ return []
85
+ except Exception as e:
86
+ print(f"Error fetching courses: {e}")
87
+ return []