|
| 1 | +import { PrismaClient } from "@prisma/client"; |
| 2 | +import { Request, Response } from "express"; |
| 3 | + |
| 4 | +const prisma = new PrismaClient(); |
| 5 | + |
| 6 | +const addRoute = async (req: Request, res: Response) => { |
| 7 | + const { name, description } = req.body; |
| 8 | + const newRoute = await prisma.route.create({ |
| 9 | + data: { |
| 10 | + name, |
| 11 | + description, |
| 12 | + }, |
| 13 | + }); |
| 14 | + |
| 15 | + res.status(201).json(newRoute); |
| 16 | +}; |
| 17 | + |
| 18 | +const getAllRoutes = async (req: Request, res: Response) => { |
| 19 | + const routes = await prisma.route.findMany(); |
| 20 | + res.status(200).json(routes); |
| 21 | +}; |
| 22 | + |
| 23 | +const getRouteById = async (req: Request, res: Response) => { |
| 24 | + const { routeId } = req.params; |
| 25 | + const route = await prisma.route.findUnique({ |
| 26 | + where: { |
| 27 | + id: routeId, |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + if (!route) { |
| 32 | + res.status(404).json({ message: "Route not found" }); |
| 33 | + } |
| 34 | + |
| 35 | + res.status(200).json(route); |
| 36 | +}; |
| 37 | + |
| 38 | +const addArea = async (req: Request, res: Response) => { |
| 39 | + const { name, description } = req.body; |
| 40 | + const newArea = await prisma.area.create({ |
| 41 | + data: { |
| 42 | + name, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + res.status(201).json(newArea); |
| 47 | +}; |
| 48 | + |
| 49 | +const addRouteToArea = async (req: Request, res: Response) => { |
| 50 | + const { areaId, routeId } = req.params; |
| 51 | + |
| 52 | + const area = await prisma.area.findUnique({ |
| 53 | + where: { |
| 54 | + id: areaId, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + if (!area) { |
| 59 | + res.status(404).json({ message: "Area not found" }); |
| 60 | + } |
| 61 | + |
| 62 | + const route = await prisma.route.findUnique({ |
| 63 | + where: { |
| 64 | + id: routeId, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + if (!route) { |
| 69 | + res.status(404).json({ message: "Route not found" }); |
| 70 | + } |
| 71 | + |
| 72 | + const updatedArea = await prisma.area.update({ |
| 73 | + where: { |
| 74 | + id: areaId, |
| 75 | + }, |
| 76 | + data: { |
| 77 | + routes: { |
| 78 | + connect: { |
| 79 | + id: routeId, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + res.status(200).json(updatedArea); |
| 86 | +}; |
| 87 | + |
| 88 | +const getAllAreas = async (req: Request, res: Response) => { |
| 89 | + const areas = await prisma.area.findMany(); |
| 90 | + res.status(200).json(areas); |
| 91 | +}; |
| 92 | + |
| 93 | +export { |
| 94 | + addRoute, |
| 95 | + getAllRoutes, |
| 96 | + getRouteById, |
| 97 | + addArea, |
| 98 | + addRouteToArea, |
| 99 | + getAllAreas, |
| 100 | +}; |
0 commit comments