Coverage for app/services/class_service.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-02 02:49 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-02 02:49 +0000
1from app.controllers.database import client
3def insert_new_class(data):
4 """
5 Inserts a new class into the 'classes' table.
7 Args:
8 data (dict): Should contain 'classTitle', 'classCode', 'instructorId', 'classHexColor', 'classImageCover'
10 Returns:
11 dict: The newly created class with its generated ID
12 """
14 new_class = {
15 'class_title': data['classTitle'],
16 'class_code': data['classCode'],
17 'instructor_id': data['instructorId'],
18 'class_hex_color': data['classHexColor'],
19 'class_image_cover': data.get('classImageCover', None),
20 'class_description': data.get('classDescription', None)
21 }
23 response = client.table("classes").insert(new_class).execute()
25 if response.data:
26 return response.data[0]
27 else:
28 raise Exception("Failed to insert new class into the database.")
30def fetch_classes_by_instructor(instructor_id):
31 """
32 Fetches all classes taught by a given instructor.
34 Args:
35 instructor_id (str)
37 Returns:
38 list[dict]: List of class records
39 """
40 response = client.table("classes").select("*").eq("instructor_id", instructor_id).execute()
42 if response.data is not None:
43 return response.data
44 else:
45 raise Exception("Failed to retrieve classes for the given instructor.")
48def insert_class_user(student_id, class_id):
49 """
50 Inserts a record into class_users table to join student and class.
52 Args:
53 student_id (str)
54 class_id (str)
56 Returns:
57 dict: The inserted row
58 """
59 new_entry = {
60 'student_id': student_id,
61 'class_id': class_id,
62 }
64 response = client.table("class_users").insert(new_entry).execute()
66 if response.data:
67 return response.data[0]
68 else:
69 raise Exception("Failed to register user to class.")