Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix - Corrigir erro ao deletar elementos com filhos #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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