104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
# myapp/services/course_service.py
|
|
from typing import Dict, List
|
|
from flask import current_app
|
|
from datetime import datetime
|
|
from bson import ObjectId
|
|
|
|
from ..models.material import Material
|
|
|
|
# 假设你已有这两个 loader
|
|
from db.course import load_course_from_json
|
|
|
|
def load_course(course_id: str):
|
|
return load_course_from_json(
|
|
course_id, course_data_dir=current_app.config["COURSE_DATA_DIR"]
|
|
)
|
|
|
|
def user_selected_course_briefs(user_obj):
|
|
"""根据用户对象的选课列表,拼装课程简要信息数组"""
|
|
course_list = current_app.extensions["course_list"]
|
|
briefs = []
|
|
for cid in getattr(user_obj, "select_course", []):
|
|
course_obj = load_course(cid)
|
|
brief = course_list.get_course_brief_info(cid, course_obj)
|
|
briefs.append(brief)
|
|
return briefs
|
|
|
|
def create_material(teacher_id, material_name, description, chapters, image_url):
|
|
material = Material(
|
|
name=material_name,
|
|
description=description,
|
|
teacher_id=teacher_id,
|
|
created_at=datetime.now(),
|
|
updated_at=datetime.now(),
|
|
chapters=chapters,
|
|
image_url=image_url
|
|
)
|
|
mongo = current_app.extensions["mongo"]
|
|
material_id = mongo.db.materials.insert_one(material.model_dump()).inserted_id
|
|
return str(material_id)
|
|
|
|
def load_material(material_id):
|
|
mongo = current_app.extensions["mongo"]
|
|
material = mongo.db.materials.find_one({"_id": ObjectId(material_id)})
|
|
return Material(**material)
|
|
|
|
def update_material(material_id, chapters):
|
|
mongo = current_app.extensions["mongo"]
|
|
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
|
if material:
|
|
mongo.db.materials.update_one(
|
|
{'_id': ObjectId(material_id)},
|
|
{'$set': {'chapters': chapters, 'updated_at': datetime.now()}}
|
|
)
|
|
return True
|
|
return False
|
|
|
|
def add_material_chapter(material_id, chapter_name, teacher_id):
|
|
mongo = current_app.extensions["mongo"]
|
|
material = mongo.db.materials.find_one({'_id': ObjectId(material_id)})
|
|
if material['teacher_id'] != teacher_id:
|
|
return False
|
|
if material:
|
|
mongo.db.materials.update_one(
|
|
{'_id': ObjectId(material_id)},
|
|
{'$push': {'chapters': {'chapter_name': chapter_name, 'lessons': []}}})
|
|
return True
|
|
|
|
def add_material_chapter_lesson(material_id, chapter_name, lesson_name, teacher_id):
|
|
mongo = current_app.extensions["mongo"]
|
|
|
|
lesson_doc = {
|
|
"lesson_name": lesson_name or "",
|
|
"markdown_lesson_file_link": "",
|
|
"markdown_prompt_file_name": "",
|
|
"markdown_score_prompt_file_link": "",
|
|
}
|
|
|
|
res = mongo.db.materials.update_one(
|
|
{
|
|
'_id': ObjectId(material_id),
|
|
'teacher_id': teacher_id,
|
|
},
|
|
{
|
|
'$push': {'chapters.$[c].lessons': lesson_doc},
|
|
'$set': {'updated_at': datetime.now()},
|
|
},
|
|
array_filters=[{'c.chapter_name': chapter_name}] # 命名占位符 c 指向命中的 chapter
|
|
)
|
|
return res.modified_count == 1
|
|
|
|
|
|
def get_materials_by_teacher(teacher_id)->List[Material]:
|
|
mongo = current_app.extensions["mongo"]
|
|
materials = mongo.db.materials.find({'teacher_id': teacher_id})
|
|
return [Material(**material) for material in materials]
|
|
|
|
def get_materials_by_teacher_dict(teacher_id)->List[Dict]:
|
|
mongo = current_app.extensions["mongo"]
|
|
materials = mongo.db.materials.find({'teacher_id': teacher_id})
|
|
result = []
|
|
for material in materials:
|
|
material['_id'] = str(material['_id'])
|
|
result.append(material)
|
|
return result |