106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
import os
|
|
from tinydb import TinyDB, Query, where
|
|
|
|
class CourseList:
|
|
def __init__(self, db_file='db/data/course/course_list.json'):
|
|
"""初始化课程列表,自动打开并加载 JSON 数据文件。"""
|
|
self.db_file = db_file
|
|
self.db = TinyDB(self.db_file)
|
|
self.course_query = Query()
|
|
|
|
def add_course(self, course_data):
|
|
"""添加新的课程到课程列表。"""
|
|
if not self.db.search(self.course_query.course_id == course_data['course_id']):
|
|
self.db.insert(course_data)
|
|
else:
|
|
print(f"Course with ID {course_data['course_id']} already exists.")
|
|
|
|
def get_course(self, course_id):
|
|
"""获取指定 course_id 的课程信息。"""
|
|
result = self.db.search(self.course_query.course_id == course_id)
|
|
return result[0] if result else None
|
|
|
|
def update_course(self, course_id, updated_data):
|
|
"""更新指定 course_id 的课程信息。"""
|
|
self.db.update(updated_data, self.course_query.course_id == course_id)
|
|
|
|
def delete_course(self, course_id):
|
|
"""删除指定 course_id 的课程。"""
|
|
self.db.remove(self.course_query.course_id == course_id)
|
|
|
|
def add_lesson_to_course(self, course_id, lesson_data):
|
|
"""为指定课程添加课时。"""
|
|
course = self.get_course(course_id)
|
|
if course:
|
|
if 'lessons' not in course:
|
|
course['lessons'] = []
|
|
course['lessons'].append(lesson_data)
|
|
self.update_course(course_id, {'lessons': course['lessons']})
|
|
else:
|
|
print(f"Course with ID {course_id} not found.")
|
|
|
|
def get_lessons_of_course(self, course_id):
|
|
"""获取指定课程的所有课时信息。"""
|
|
course = self.get_course(course_id)
|
|
return course['lessons'] if course else None
|
|
|
|
def get_all_courses(self):
|
|
"""获取所有课程。"""
|
|
return self.db.all()
|
|
|
|
|
|
# # 课程数据结构示例
|
|
# course_data = {
|
|
# 'course_id': 'CS101',
|
|
# 'course_name': 'Computer Science 101',
|
|
# 'course_img_path': 'images/cs101.png',
|
|
# 'lessons': [
|
|
# {
|
|
# 'lesson_id': 'L001',
|
|
# 'lesson_name': 'Introduction to Computer Science',
|
|
# 'markdown': 'This is the introduction lesson.',
|
|
# 'markdown_prompt': 'What is Computer Science?',
|
|
# 'score_prompt': 'Please provide an overview of CS.'
|
|
# },
|
|
# {
|
|
# 'lesson_id': 'L002',
|
|
# 'lesson_name': 'Data Structures',
|
|
# 'markdown': 'In this lesson, we cover Data Structures.',
|
|
# 'markdown_prompt': 'What is an array?',
|
|
# 'score_prompt': 'Explain how linked lists work.'
|
|
# }
|
|
# ]
|
|
# }
|
|
|
|
# lesson_data = {
|
|
# 'lesson_id': 'L003',
|
|
# 'lesson_name': 'Algorithms',
|
|
# 'markdown': 'In this lesson, we will discuss algorithms.',
|
|
# 'markdown_prompt': 'What is a sorting algorithm?',
|
|
# 'score_prompt': 'Explain the difference between merge sort and quicksort.'
|
|
# }
|
|
|
|
# # 创建 CourseList 实例
|
|
# course_list = CourseList()
|
|
|
|
# # 添加课程
|
|
# course_list.add_course(course_data)
|
|
|
|
# # 添加课时到现有课程
|
|
# course_list.add_lesson_to_course('CS101', lesson_data)
|
|
|
|
# # 获取并显示所有课程
|
|
# courses = course_list.get_all_courses()
|
|
# print(courses)
|
|
|
|
# # 获取某个课程的课时
|
|
# lessons = course_list.get_lessons_of_course('CS101')
|
|
# print(lessons)
|
|
|
|
# # 更新课程信息
|
|
# updated_data = {'course_name': 'Computer Science 101 - Updated'}
|
|
# course_list.update_course('CS101', updated_data)
|
|
|
|
# # 删除课程
|
|
# course_list.delete_course('CS101')
|