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

1from app.controllers.database import client 

2 

3def insert_new_class(data): 

4 """ 

5 Inserts a new class into the 'classes' table. 

6 

7 Args: 

8 data (dict): Should contain 'classTitle', 'classCode', 'instructorId', 'classHexColor', 'classImageCover' 

9 

10 Returns: 

11 dict: The newly created class with its generated ID 

12 """ 

13 

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 } 

22 

23 response = client.table("classes").insert(new_class).execute() 

24 

25 if response.data: 

26 return response.data[0] 

27 else: 

28 raise Exception("Failed to insert new class into the database.") 

29 

30def fetch_classes_by_instructor(instructor_id): 

31 """ 

32 Fetches all classes taught by a given instructor. 

33 

34 Args: 

35 instructor_id (str) 

36 

37 Returns: 

38 list[dict]: List of class records 

39 """ 

40 response = client.table("classes").select("*").eq("instructor_id", instructor_id).execute() 

41 

42 if response.data is not None: 

43 return response.data 

44 else: 

45 raise Exception("Failed to retrieve classes for the given instructor.") 

46 

47 

48def insert_class_user(student_id, class_id): 

49 """ 

50 Inserts a record into class_users table to join student and class. 

51 

52 Args: 

53 student_id (str) 

54 class_id (str) 

55 

56 Returns: 

57 dict: The inserted row 

58 """ 

59 new_entry = { 

60 'student_id': student_id, 

61 'class_id': class_id, 

62 } 

63 

64 response = client.table("class_users").insert(new_entry).execute() 

65 

66 if response.data: 

67 return response.data[0] 

68 else: 

69 raise Exception("Failed to register user to class.")