Files
var-monorepo/apps/dispatch-server/routes/mission.ts

84 lines
1.9 KiB
TypeScript

import { prisma } from "@repo/db";
import { Router } from "express";
import { io } from "../index";
const router = Router();
// Get all missions
router.post("/", async (req, res) => {
try {
const filter = req.body?.filter || {};
const missions = await prisma.mission.findMany({
where: filter,
});
res.json(missions);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to fetch missions" });
}
});
// Get a single mission by ID
router.get("/:id", async (req, res) => {
const { id } = req.params;
try {
const mission = await prisma.mission.findUnique({
where: { id: Number(id) },
});
if (mission) {
res.json(mission);
} else {
res.status(404).json({ error: "Mission not found" });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to fetch mission" });
}
});
// Create a new mission
router.put("/", async (req, res) => {
try {
const newMission = await prisma.mission.create({
data: req.body,
});
io.to("dispatchers").emit("new-mission", newMission);
res.status(201).json(newMission);
} catch (error) {
res.status(500).json({ error: "Failed to create mission" });
}
});
// Update a mission by ID
router.patch("/:id", async (req, res) => {
const { id } = req.params;
try {
const updatedMission = await prisma.mission.update({
where: { id: Number(id) },
data: req.body,
});
io.to("dispatchers").emit("update-mission", updatedMission);
res.json(updatedMission);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to update mission" });
}
});
// Delete a mission by ID
router.delete("/:id", async (req, res) => {
const { id } = req.params;
try {
await prisma.mission.delete({
where: { id: Number(id) },
});
io.to("dispatchers").emit("delete-mission", id);
res.status(204).send();
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to delete mission" });
}
});
export default router;