mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
refactor: optimize chapter and activity reordering logic
This commit is contained in:
parent
dbfd05edde
commit
543f6ea86a
1 changed files with 59 additions and 144 deletions
|
|
@ -366,182 +366,97 @@ async def reorder_chapters_and_activities(
|
||||||
# Chapters
|
# Chapters
|
||||||
###########
|
###########
|
||||||
|
|
||||||
# Delete CourseChapters that are not linked to chapter_id and activity_id and org_id and course_id
|
# Get all existing course chapters
|
||||||
statement = (
|
statement = select(CourseChapter).where(
|
||||||
select(CourseChapter)
|
CourseChapter.course_id == course.id,
|
||||||
.where(
|
CourseChapter.org_id == course.org_id
|
||||||
CourseChapter.course_id == course.id, CourseChapter.org_id == course.org_id
|
|
||||||
)
|
|
||||||
.order_by(CourseChapter.order)
|
|
||||||
)
|
)
|
||||||
course_chapters = db_session.exec(statement).all()
|
existing_course_chapters = db_session.exec(statement).all()
|
||||||
|
|
||||||
chapter_ids_to_keep = [
|
# Create a map of existing chapters for faster lookup
|
||||||
chapter_order.chapter_id
|
existing_chapter_map = {cc.chapter_id: cc for cc in existing_course_chapters}
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids
|
|
||||||
]
|
|
||||||
for course_chapter in course_chapters:
|
|
||||||
if course_chapter.chapter_id not in chapter_ids_to_keep:
|
|
||||||
db_session.delete(course_chapter)
|
|
||||||
db_session.commit()
|
|
||||||
|
|
||||||
# Delete Chapters that are not in the list of chapters_order
|
# Update or create course chapters based on new order
|
||||||
statement = select(Chapter).where(Chapter.course_id == course.id)
|
for index, chapter_order in enumerate(chapters_order.chapter_order_by_ids):
|
||||||
chapters = db_session.exec(statement).all()
|
if chapter_order.chapter_id in existing_chapter_map:
|
||||||
|
# Update existing chapter order
|
||||||
chapter_ids_to_keep = [
|
course_chapter = existing_chapter_map[chapter_order.chapter_id]
|
||||||
chapter_order.chapter_id
|
course_chapter.order = index
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids
|
db_session.add(course_chapter)
|
||||||
]
|
else:
|
||||||
|
# Create new course chapter
|
||||||
for chapter in chapters:
|
|
||||||
if chapter.id not in chapter_ids_to_keep:
|
|
||||||
db_session.delete(chapter)
|
|
||||||
db_session.commit()
|
|
||||||
|
|
||||||
# If links do not exists, create them
|
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids:
|
|
||||||
statement = (
|
|
||||||
select(CourseChapter)
|
|
||||||
.where(
|
|
||||||
CourseChapter.chapter_id == chapter_order.chapter_id,
|
|
||||||
CourseChapter.course_id == course.id,
|
|
||||||
)
|
|
||||||
.order_by(CourseChapter.order)
|
|
||||||
)
|
|
||||||
course_chapter = db_session.exec(statement).first()
|
|
||||||
|
|
||||||
if not course_chapter:
|
|
||||||
# Add CourseChapter link
|
|
||||||
course_chapter = CourseChapter(
|
course_chapter = CourseChapter(
|
||||||
chapter_id=chapter_order.chapter_id,
|
chapter_id=chapter_order.chapter_id,
|
||||||
course_id=course.id, # type: ignore
|
course_id=course.id, # type: ignore
|
||||||
org_id=course.org_id,
|
org_id=course.org_id,
|
||||||
creation_date=str(datetime.now()),
|
creation_date=str(datetime.now()),
|
||||||
update_date=str(datetime.now()),
|
update_date=str(datetime.now()),
|
||||||
order=chapter_order.chapter_id,
|
order=index,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Insert CourseChapter link in DB
|
|
||||||
db_session.add(course_chapter)
|
db_session.add(course_chapter)
|
||||||
db_session.commit()
|
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
# Update order of chapters
|
# Remove chapters that are no longer in the order
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids:
|
chapter_ids_to_keep = {co.chapter_id for co in chapters_order.chapter_order_by_ids}
|
||||||
statement = (
|
for cc in existing_course_chapters:
|
||||||
select(CourseChapter)
|
if cc.chapter_id not in chapter_ids_to_keep:
|
||||||
.where(
|
db_session.delete(cc)
|
||||||
CourseChapter.chapter_id == chapter_order.chapter_id,
|
db_session.commit()
|
||||||
CourseChapter.course_id == course.id,
|
|
||||||
)
|
|
||||||
.order_by(CourseChapter.order)
|
|
||||||
)
|
|
||||||
course_chapter = db_session.exec(statement).first()
|
|
||||||
|
|
||||||
if course_chapter:
|
|
||||||
# Get the order from the index of the chapter_order_by_ids list
|
|
||||||
course_chapter.order = chapters_order.chapter_order_by_ids.index(
|
|
||||||
chapter_order
|
|
||||||
)
|
|
||||||
db_session.commit()
|
|
||||||
|
|
||||||
###########
|
###########
|
||||||
# Activities
|
# Activities
|
||||||
###########
|
###########
|
||||||
|
|
||||||
# Delete ChapterActivities that are no longer part of the new order
|
# Get all existing chapter activities
|
||||||
statement = (
|
statement = select(ChapterActivity).where(
|
||||||
select(ChapterActivity)
|
ChapterActivity.course_id == course.id,
|
||||||
.where(
|
ChapterActivity.org_id == course.org_id
|
||||||
ChapterActivity.course_id == course.id,
|
|
||||||
ChapterActivity.org_id == course.org_id,
|
|
||||||
)
|
|
||||||
.order_by(ChapterActivity.order)
|
|
||||||
)
|
)
|
||||||
chapter_activities = db_session.exec(statement).all()
|
existing_chapter_activities = db_session.exec(statement).all()
|
||||||
|
|
||||||
activity_ids_to_delete = []
|
# Create a map for faster lookup
|
||||||
for chapter_activity in chapter_activities:
|
existing_activity_map = {
|
||||||
if (
|
(ca.chapter_id, ca.activity_id): ca
|
||||||
chapter_activity.chapter_id not in chapter_ids_to_keep
|
for ca in existing_chapter_activities
|
||||||
or chapter_activity.activity_id not in activity_ids_to_delete
|
}
|
||||||
):
|
|
||||||
activity_ids_to_delete.append(chapter_activity.activity_id)
|
|
||||||
|
|
||||||
for activity_id in activity_ids_to_delete:
|
# Track which activities we want to keep
|
||||||
statement = (
|
activities_to_keep = set()
|
||||||
select(ChapterActivity)
|
|
||||||
.where(
|
|
||||||
ChapterActivity.activity_id == activity_id,
|
|
||||||
ChapterActivity.course_id == course.id,
|
|
||||||
)
|
|
||||||
.order_by(ChapterActivity.order)
|
|
||||||
)
|
|
||||||
chapter_activity = db_session.exec(statement).first()
|
|
||||||
|
|
||||||
db_session.delete(chapter_activity)
|
# Update or create chapter activities based on new order
|
||||||
db_session.commit()
|
|
||||||
|
|
||||||
# If links do not exist, create them
|
|
||||||
chapter_activity_map = {}
|
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids:
|
for chapter_order in chapters_order.chapter_order_by_ids:
|
||||||
for activity_order in chapter_order.activities_order_by_ids:
|
for index, activity_order in enumerate(chapter_order.activities_order_by_ids):
|
||||||
if (
|
activity_key = (chapter_order.chapter_id, activity_order.activity_id)
|
||||||
activity_order.activity_id in chapter_activity_map
|
activities_to_keep.add(activity_key)
|
||||||
and chapter_activity_map[activity_order.activity_id]
|
|
||||||
!= chapter_order.chapter_id
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
|
|
||||||
statement = (
|
if activity_key in existing_activity_map:
|
||||||
select(ChapterActivity)
|
# Update existing activity order
|
||||||
.where(
|
chapter_activity = existing_activity_map[activity_key]
|
||||||
ChapterActivity.chapter_id == chapter_order.chapter_id,
|
chapter_activity.order = index
|
||||||
ChapterActivity.activity_id == activity_order.activity_id,
|
db_session.add(chapter_activity)
|
||||||
)
|
else:
|
||||||
.order_by(ChapterActivity.order)
|
# Create new chapter activity
|
||||||
)
|
|
||||||
chapter_activity = db_session.exec(statement).first()
|
|
||||||
|
|
||||||
if not chapter_activity:
|
|
||||||
# Add ChapterActivity link
|
|
||||||
chapter_activity = ChapterActivity(
|
chapter_activity = ChapterActivity(
|
||||||
chapter_id=chapter_order.chapter_id,
|
chapter_id=chapter_order.chapter_id,
|
||||||
activity_id=activity_order.activity_id,
|
activity_id=activity_order.activity_id,
|
||||||
org_id=course.org_id,
|
org_id=course.org_id,
|
||||||
course_id=course.id, # type: ignore
|
course_id=course.id, # type: ignore
|
||||||
creation_date=str(datetime.now()),
|
creation_date=str(datetime.now()),
|
||||||
update_date=str(datetime.now()),
|
update_date=str(datetime.now()),
|
||||||
order=activity_order.activity_id,
|
order=index,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Insert ChapterActivity link in DB
|
|
||||||
db_session.add(chapter_activity)
|
db_session.add(chapter_activity)
|
||||||
db_session.commit()
|
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
chapter_activity_map[activity_order.activity_id] = chapter_order.chapter_id
|
# Remove activities that are no longer in any chapter
|
||||||
|
for ca in existing_chapter_activities:
|
||||||
|
if (ca.chapter_id, ca.activity_id) not in activities_to_keep:
|
||||||
|
db_session.delete(ca)
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
# Update order of activities
|
return {"detail": "Chapters and activities reordered successfully"}
|
||||||
for chapter_order in chapters_order.chapter_order_by_ids:
|
|
||||||
for activity_order in chapter_order.activities_order_by_ids:
|
|
||||||
statement = (
|
|
||||||
select(ChapterActivity)
|
|
||||||
.where(
|
|
||||||
ChapterActivity.chapter_id == chapter_order.chapter_id,
|
|
||||||
ChapterActivity.activity_id == activity_order.activity_id,
|
|
||||||
)
|
|
||||||
.order_by(ChapterActivity.order)
|
|
||||||
)
|
|
||||||
chapter_activity = db_session.exec(statement).first()
|
|
||||||
|
|
||||||
if chapter_activity:
|
|
||||||
# Get the order from the index of the chapter_order_by_ids list
|
|
||||||
chapter_activity.order = chapter_order.activities_order_by_ids.index(
|
|
||||||
activity_order
|
|
||||||
)
|
|
||||||
db_session.commit()
|
|
||||||
|
|
||||||
return {"detail": "Chapters reordered"}
|
|
||||||
|
|
||||||
|
|
||||||
## 🔒 RBAC Utils ##
|
## 🔒 RBAC Utils ##
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue