Made Mission marker DB compatible
This commit is contained in:
79
apps/dispatch-server/routes/mission.ts
Normal file
79
apps/dispatch-server/routes/mission.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { prisma } from "@repo/db";
|
||||
import { Router } from "express";
|
||||
|
||||
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,
|
||||
});
|
||||
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,
|
||||
});
|
||||
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) },
|
||||
});
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: "Failed to delete mission" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user