添加chapter和lesson
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
# 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
|
||||
@@ -18,3 +23,82 @@ def user_selected_course_briefs(user_obj):
|
||||
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
|
||||
Reference in New Issue
Block a user