- Added API routes for fetching connected users, keywords, missions, and stations. - Created a new QueryProvider component for managing query states and socket events. - Introduced connection stores for dispatch and pilot, managing socket connections and states. - Updated Prisma schema for connected aircraft model. - Enhanced UI with toast notifications for status updates and chat interactions. - Implemented query functions for fetching connected users and keywords with error handling.
84 lines
1.9 KiB
TypeScript
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("missions").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("missions").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("missions").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;
|