From 1f9d888365b99afc3d2c5b7d0eef96513aa0268e Mon Sep 17 00:00:00 2001 From: Weslley Barros Date: Sun, 10 Dec 2023 23:41:39 -0300 Subject: [PATCH] deletar imagens --- .../src/controllers/classPlanController.ts | 42 +++++++++++++++++-- backend/src/controllers/drillController.ts | 28 ++++++++++++- frontend/src/pages/Drill.tsx | 10 +---- frontend/src/service/drillService.ts | 4 ++ 4 files changed, 71 insertions(+), 13 deletions(-) diff --git a/backend/src/controllers/classPlanController.ts b/backend/src/controllers/classPlanController.ts index 86505d9..8f6de9d 100644 --- a/backend/src/controllers/classPlanController.ts +++ b/backend/src/controllers/classPlanController.ts @@ -81,12 +81,48 @@ export default class classPlanController { delete = async (req: Request, res: Response) => { try { const { id } = req.params + const data = await prisma.classPlan.findMany({ + where: { + id, + }, + select: { + id: true, + drills: { + select: { + id: true, + drillElements: { + select: { + id: true, + }, + }, + }, + }, + }, + }) - const classPlan = await prisma.classPlan.deleteMany({ - where: { id }, + data.map(async (item) => { + item.drills.map(async (drill) => { + drill.drillElements.map(async (drillElement) => { + await prisma.drillElement.delete({ + where: { + id: drillElement.id, + }, + }) + }) + await prisma.drill.delete({ + where: { + id: drill.id, + }, + }) + }) }) - res.status(200).json(classPlan) + const plan = await prisma.classPlan.delete({ + where: { + id, + }, + }) + res.status(204).json(plan) } catch (err) { err as Prisma.PrismaClientKnownRequestError res.status(500).json({ errors: { server: "Server error" } }) diff --git a/backend/src/controllers/drillController.ts b/backend/src/controllers/drillController.ts index 821c203..721995f 100644 --- a/backend/src/controllers/drillController.ts +++ b/backend/src/controllers/drillController.ts @@ -93,12 +93,36 @@ export default class drillController { deleteById = async (req: Request, res: Response) => { try { const id = req.params.id - const deletedDrill = await prisma.drill.deleteMany({ + + const drills = await prisma.drill.findMany({ where: { id, }, + select: { + drillElements: { + select: { + id: true, + }, + }, + }, + }) + + drills.map(async (drill) => { + drill.drillElements.map(async (drillElement) => { + await prisma.drillElement.delete({ + where: { + id: drillElement.id, + }, + }) + }) + await prisma.drill.delete({ + where: { + id, + }, + }) }) - res.status(204).json(deletedDrill) + + res.status(204).json({ message: "Drill deleted" }) } catch (err) { console.log(err) res.status(500).json({ error: "Internal Server Error" }) diff --git a/frontend/src/pages/Drill.tsx b/frontend/src/pages/Drill.tsx index fd105b7..7cf1616 100644 --- a/frontend/src/pages/Drill.tsx +++ b/frontend/src/pages/Drill.tsx @@ -117,15 +117,9 @@ const Drill = () => { async function saveDrillState(){ updateImagen() let data = { - id: drillUpdated.id, - title: titleAux, image: imagemBase64, - description: drillUpdated.description, - observations: drillUpdated.observations, - classPlanId: drillUpdated.classPlanId, }; - console.log(data) - await drill.updateById(id as string, data); + await drill.updateImage(id as string, data); for(const [idNewItem, indexNewItem] of newItems){ try{ const element = document.getElementById(idNewItem); @@ -181,7 +175,7 @@ const Drill = () => { setDrillUpdated(response.data); } } - console.log("id: " + id) + useEffect(() => { loadData(); setTitle(drillUpdated.title); diff --git a/frontend/src/service/drillService.ts b/frontend/src/service/drillService.ts index 58db84e..894b499 100644 --- a/frontend/src/service/drillService.ts +++ b/frontend/src/service/drillService.ts @@ -17,6 +17,10 @@ class DrillService extends Apiservice { return this.put('/' + id, data); } + async updateImage(id: string, data: any) { + return this.put('/image/' + id, data); + } + async deleteById(id: string) { return this.delete('/' + id); }