relation between drafts and courseDrafts

This commit is contained in:
austinkelsay 2024-08-21 22:33:19 -05:00
parent 8555895617
commit e212f2f4f0
4 changed files with 18 additions and 27 deletions

View File

@ -71,6 +71,7 @@ CREATE TABLE "Draft" (
"topics" TEXT[],
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"courseDraftId" TEXT,
CONSTRAINT "Draft_pkey" PRIMARY KEY ("id")
);
@ -91,12 +92,6 @@ CREATE TABLE "CourseDraft" (
CONSTRAINT "CourseDraft_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "_CourseDraftToDraft" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "User_pubkey_key" ON "User"("pubkey");
@ -112,12 +107,6 @@ CREATE UNIQUE INDEX "Resource_noteId_key" ON "Resource"("noteId");
-- CreateIndex
CREATE UNIQUE INDEX "CourseDraft_courseId_key" ON "CourseDraft"("courseId");
-- CreateIndex
CREATE UNIQUE INDEX "_CourseDraftToDraft_AB_unique" ON "_CourseDraftToDraft"("A", "B");
-- CreateIndex
CREATE INDEX "_CourseDraftToDraft_B_index" ON "_CourseDraftToDraft"("B");
-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE SET NULL ON UPDATE CASCADE;
@ -145,14 +134,11 @@ ALTER TABLE "Resource" ADD CONSTRAINT "Resource_courseDraftId_fkey" FOREIGN KEY
-- AddForeignKey
ALTER TABLE "Draft" ADD CONSTRAINT "Draft_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Draft" ADD CONSTRAINT "Draft_courseDraftId_fkey" FOREIGN KEY ("courseDraftId") REFERENCES "CourseDraft"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "CourseDraft" ADD CONSTRAINT "CourseDraft_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "CourseDraft" ADD CONSTRAINT "CourseDraft_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "_CourseDraftToDraft" ADD CONSTRAINT "_CourseDraftToDraft_A_fkey" FOREIGN KEY ("A") REFERENCES "CourseDraft"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "_CourseDraftToDraft" ADD CONSTRAINT "_CourseDraftToDraft_B_fkey" FOREIGN KEY ("B") REFERENCES "Draft"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -82,7 +82,8 @@ model Draft {
topics String[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
courseDrafts CourseDraft[]
courseDraft CourseDraft? @relation(fields: [courseDraftId], references: [id])
courseDraftId String?
}
model CourseDraft {

View File

@ -36,10 +36,6 @@ const CourseForm = ({ draft = null }) => {
}
}, [resources, workshops, drafts, resourcesLoading, workshopsLoading, draftsLoading]);
useEffect(() => {
console.log("lessons", lessons);
}, [lessons]);
const handleSubmit = async (e) => {
e.preventDefault();

View File

@ -7,6 +7,7 @@ export const getAllCourseDraftsByUserId = async (userId) => {
include: {
user: true, // Include the related user
resources: true, // Include related resources
drafts: true, // Include related drafts
},
});
};
@ -29,27 +30,34 @@ export const createCourseDraft = async (data) => {
data: {
...data,
resources: {
connect: data.resources.map((resource) => ({ id: resource.id })),
connect: data.resources?.map((resource) => ({ id: resource.id })) || [],
},
drafts: {
connect: data.drafts?.map((draft) => ({ id: draft.id })) || [],
},
},
include: {
resources: true,
drafts: true,
}
});
};
// Update an existing CourseDraft by its ID
export const updateCourseDraft = async (id, data) => {
const { resourceIds, ...otherData } = data;
const { resourceIds, draftIds, ...otherData } = data;
return await prisma.courseDraft.update({
where: { id },
data: {
...otherData,
resources: {
set: resourceIds?.map((resourceId) => ({ id: resourceId })),
set: resourceIds?.map((resourceId) => ({ id: resourceId })) || [],
},
drafts: {
set: draftIds?.map((draftId) => ({ id: draftId })) || [],
},
},
include: { resources: true }
include: { resources: true, drafts: true }
});
};