Skip to content

Commit

Permalink
deletar imagens
Browse files Browse the repository at this point in the history
  • Loading branch information
weslley17w committed Dec 11, 2023
1 parent 5c9d2ce commit 1f9d888
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
42 changes: 39 additions & 3 deletions backend/src/controllers/classPlanController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" } })
Expand Down
28 changes: 26 additions & 2 deletions backend/src/controllers/drillController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/pages/Drill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -181,7 +175,7 @@ const Drill = () => {
setDrillUpdated(response.data);
}
}
console.log("id: " + id)

useEffect(() => {
loadData();
setTitle(drillUpdated.title);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/service/drillService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 1f9d888

Please sign in to comment.