From 0572368a323542190efba46446f966c9dcd19923 Mon Sep 17 00:00:00 2001 From: swve Date: Fri, 4 Nov 2022 23:46:55 +0100 Subject: [PATCH] feat: init CS + SS course chapter deletion --- front/components/drags/chapter.tsx | 2 +- .../course/[courseid]/edit/index.tsx | 13 +++++++++++-- front/services/chapters.ts | 19 +++++++++++++++++++ src/services/chapters.py | 9 ++++++--- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/front/components/drags/chapter.tsx b/front/components/drags/chapter.tsx index 56de2aac..d8058c8b 100644 --- a/front/components/drags/chapter.tsx +++ b/front/components/drags/chapter.tsx @@ -22,7 +22,7 @@ function Chapter(props: any) { {(provided, snapshot) => ( -

{props.info.list.chapter.name}

+

{props.info.list.chapter.name}

{(provided) => ( diff --git a/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx b/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx index eb0bd83d..56b3496b 100644 --- a/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx +++ b/front/pages/org/[orgslug]/course/[courseid]/edit/index.tsx @@ -7,7 +7,7 @@ import { Title } from "../../../../../../components/ui/styles/title"; import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"; import { initialData, initialData2 } from "../../../../../../components/drags/data"; import Chapter from "../../../../../../components/drags/chapter"; -import { createChapter, getCourseChaptersMetadata } from "../../../../../../services/chapters"; +import { createChapter, deleteChapter, getCourseChaptersMetadata } from "../../../../../../services/chapters"; import { useRouter } from "next/router"; import NewChapterModal from "../../../../../../components/modals/chapters/new"; @@ -59,6 +59,15 @@ function CourseEdit() { setNewChapterModal(false); }; + const deleteChapterUI = async (chapterId: any) => { + console.log("deleteChapter", chapterId); + await deleteChapter(chapterId); + + getCourseChapters(); + }; + + + // Close new chapter modal const closeModal = () => { setNewChapterModal(false); @@ -177,7 +186,7 @@ function CourseEdit() { {(provided) => (
{getChapters().map((info: any, index: any) => ( - + ))} {provided.placeholder}
diff --git a/front/services/chapters.ts b/front/services/chapters.ts index 9495a189..3c16240b 100644 --- a/front/services/chapters.ts +++ b/front/services/chapters.ts @@ -41,3 +41,22 @@ export async function createChapter(data: any, course_id: any) { return result; } + +export async function deleteChapter (coursechapter_id: any) { + const HeadersConfig = new Headers({ "Content-Type": "application/json" }); + + const requestOptions: any = { + method: "DELETE", + headers: HeadersConfig, + redirect: "follow", + credentials: "include", + }; + + const result: any = await fetch(`${getAPIUrl()}chapters/${coursechapter_id}`, requestOptions) + .then((result) => result.json()) + .catch((error) => console.log("error", error)); + + console.log("result", result); + + return result; +} diff --git a/src/services/chapters.py b/src/services/chapters.py index 93d68a94..522f1fc1 100644 --- a/src/services/chapters.py +++ b/src/services/chapters.py @@ -99,10 +99,10 @@ async def get_coursechapters_meta(course_id: str, current_user: PublicUser): for coursechapter in coursechapters: coursechapter = CourseChapterInDB(**coursechapter) coursechapter_elementIds = [] - + for element in coursechapter.elements: coursechapter_elementIds.append(element.element_id) - + chapters[coursechapter.coursechapter_id] = { "id": coursechapter.coursechapter_id, "name": coursechapter.name, "elementIds": coursechapter_elementIds } @@ -164,6 +164,7 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser) await check_database() coursechapters = learnhouseDB["coursechapters"] + courses = learnhouseDB["courses"] coursechapter = coursechapters.find_one( {"coursechapter_id": coursechapter_id}) @@ -175,7 +176,9 @@ async def delete_coursechapter(coursechapter_id: str, current_user: PublicUser) isDeleted = coursechapters.delete_one( {"coursechapter_id": coursechapter_id}) - # TODO : delete coursechapter from course using $pull https://www.mongodb.com/docs/v4.2/reference/operator/update/pull/ + # Remove coursechapter from course + courses.update_one({"course_id": coursechapter["course_id"]}, { + "$pull": {"chapters": coursechapter_id}}) if isDeleted: return {"detail": "coursechapter deleted"}