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

60 visualizar planos #78

Merged
merged 6 commits into from
Dec 12, 2023
Merged
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
2 changes: 2 additions & 0 deletions backend/prisma/migrations/20231211235124_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Drill" ALTER COLUMN "image" SET DEFAULT 'abc';
2 changes: 2 additions & 0 deletions backend/prisma/migrations/20231211235631_/migration.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/prisma/schema.prisma

Large diffs are not rendered by default.

124 changes: 105 additions & 19 deletions backend/src/controllers/classPlanController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@ export default class classPlanController {
where: {
userId: userId,
},
include: {
user: {
select: {
id: true,
email: true,
name: true,
},
},
select: {
id: true,
title: true,
},
})
res.status(200).json(classPlan)
Expand All @@ -62,14 +57,12 @@ export default class classPlanController {
where: {
id: id,
},
include: {
user: {
select: {
id: true,
email: true,
name: true,
},
},
select: {
id: true,
title: true,
goals: true,
observations: true,
userId: true,
},
})
res.status(200).json(classPlan)
Expand All @@ -78,20 +71,113 @@ export default class classPlanController {
}
}

searchByCreatedAtOrTitle = async (req: Request, res: Response) => {
try {
const { userId, title, startDate, finalDate } = req.params
if (startDate == "_") {
const classPlan = await prisma.classPlan.findMany({
where: {
userId: userId,
title: { contains: title },
},
select: {
id: true,
title: true,
},
})
res.status(200).json(classPlan)
} else if (title == "_") {
const start = new Date(startDate)
const final = new Date(finalDate)
const classPlan = await prisma.classPlan.findMany({
where: {
userId: userId,
createdAt: {
lte: final,
gte: start,
},
},
select: {
id: true,
title: true,
},
})
res.status(200).json(classPlan)
} else {
const start = new Date(startDate)
const final = new Date(finalDate)
const classPlan = await prisma.classPlan.findMany({
where: {
userId: userId,
createdAt: {
lte: final,
gte: start,
},
title: { contains: title },
},
select: {
id: true,
title: true,
},
})
res.status(200).json(classPlan)
}
} catch (err) {
res.status(500).json({ error: "Internal Server Error" })
}
}

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(200).json(plan)
} catch (err) {
err as Prisma.PrismaClientKnownRequestError
res.status(500).json({ errors: { server: "Server error" } })
}
}

updateById = async (req: Request, res: Response) => {
try {
const id = req.params.id
Expand Down
29 changes: 27 additions & 2 deletions backend/src/controllers/drillController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class drillController {
image,
},
})
console.log("Foi")
res.status(204).json(updatedDrill)
} catch (err) {
res.status(500).json({ error: "Internal Server Error" })
Expand All @@ -93,12 +94,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
21 changes: 20 additions & 1 deletion backend/src/middleware/authUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sign } from "jsonwebtoken"
import { sign, verify } from "jsonwebtoken"
import { Request, Response, NextFunction } from "express"

interface Idata {
id: string
Expand All @@ -11,4 +12,22 @@ export default class AuthUser {
expiresIn: "30d",
})
}

async auth(
request: Request,
response: Response,
next: NextFunction,
): Promise<Response | null> {
let token
try {
token = request.headers.authorization?.split(" ")[1]
verify(token as string, process.env.AUTH_CONFIG_SECRET as string)
} catch (err) {
return response.status(401).json({
message: "Token inválido!",
})
}
next()
return null
}
}
15 changes: 11 additions & 4 deletions backend/src/routes/classPlanRouters.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Router } from "express"
import classPlanController from "../controllers/classPlanController"
import AuthUser from "../middleware/authUser"

const classPlanRouters = Router()
const authenticateUser = new AuthUser()
const classPlan = new classPlanController()

classPlanRouters.post("/", classPlan.create)
classPlanRouters.get("/:id", classPlan.show)
classPlanRouters.post("/", authenticateUser.auth, classPlan.create)
classPlanRouters.get("/:id", authenticateUser.auth, classPlan.show)
classPlanRouters.get(
"/pesquisa-data-titulo/:userId/:title/:startDate/:finalDate",
authenticateUser.auth,
classPlan.searchByCreatedAtOrTitle,
)
classPlanRouters.get("/planos-usuario/:userId", classPlan.list)
classPlanRouters.put("/:id", classPlan.updateById)
classPlanRouters.delete("/:id", classPlan.delete)
classPlanRouters.put("/:id", authenticateUser.auth, classPlan.updateById)
classPlanRouters.delete("/:id", authenticateUser.auth, classPlan.delete)

export default classPlanRouters
18 changes: 14 additions & 4 deletions backend/src/routes/drillElementRouters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Router } from "express"
import drillElementController from "@/controllers/drillElementController"
import AuthUser from "../middleware/authUser"

const drillElementRouters = Router()
const authenticateUser = new AuthUser()
const drillElement = new drillElementController()

drillElementRouters.post("/", drillElement.create)
drillElementRouters.get("/:drillId", drillElement.getManyByDrillId)
drillElementRouters.put("/:id", drillElement.updateById)
drillElementRouters.delete("/:id", drillElement.deleteById)
drillElementRouters.post("/", authenticateUser.auth, drillElement.create)
drillElementRouters.get(
"/:drillId",
authenticateUser.auth,
drillElement.getManyByDrillId,
)
drillElementRouters.put("/:id", authenticateUser.auth, drillElement.updateById)
drillElementRouters.delete(
"/:id",
authenticateUser.auth,
drillElement.deleteById,
)

export default drillElementRouters
18 changes: 12 additions & 6 deletions backend/src/routes/drillRouters.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Router } from "express"
import drillController from "../controllers/drillController"
import AuthUser from "../middleware/authUser"

const authenticateUser = new AuthUser()
const drillRouters = Router()
const drill = new drillController()

drillRouters.post("/", drill.create)
drillRouters.get("/:classPlanId", drill.getManyByClassPlanId)
drillRouters.get("/drill/:id", drill.show)
drillRouters.put("/:id", drill.updateById)
drillRouters.put("/image/:id", drill.updateImage)
drillRouters.delete("/:id", drill.deleteById)
drillRouters.post("/", authenticateUser.auth, drill.create)
drillRouters.get(
"/:classPlanId",
authenticateUser.auth,
drill.getManyByClassPlanId,
)
drillRouters.get("/drill/:id", authenticateUser.auth, drill.show)
drillRouters.put("/:id", authenticateUser.auth, drill.updateById)
drillRouters.put("/image/:id", authenticateUser.auth, drill.updateImage)
drillRouters.delete("/:id", authenticateUser.auth, drill.deleteById)

export default drillRouters
1 change: 0 additions & 1 deletion backend/src/zodSchemas/drill.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const drillSchema = z.object({
.string()
.max(500, "Observações deve ter no máximo 500 caracteres"),
classPlanId: z.string().uuid("Id do plano de aula inválido"),
image: z.string(),
})

export { drillSchema }
Loading