LocationHub/server/src/controller/lpTtnEndDeviceUplinksController.ts

76 lines
2.1 KiB
TypeScript

import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { LpTtnEndDeviceUplinksService } from "../services/lpTtnEndDeviceUplinksService";
const lpTtnEndDeviceUplinksService = container.resolve(
LpTtnEndDeviceUplinksService
);
const router = express.Router();
router.get("/", async (req: Request, res: Response) => {
try {
const uplinks = await lpTtnEndDeviceUplinksService.getAllUplinks();
res.status(200).json(uplinks);
} catch (error) {
console.log(error);
res.status(500).json({ error: "Error retrieving uplinks" });
}
});
router.get("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const uplink = await lpTtnEndDeviceUplinksService.getUplinkById(id);
if (!uplink) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(200).json(uplink);
} catch (error) {
res.status(500).json({ error: "Error retrieving uplink" });
}
});
router.post("/", async (req: Request, res: Response) => {
try {
const newUplink = await lpTtnEndDeviceUplinksService.createUplink(req.body);
res.status(201).json(newUplink);
} catch (error) {
console.log(error);
res.status(500).json({ error: "Error creating uplink" });
}
});
router.put("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const updatedUplink = await lpTtnEndDeviceUplinksService.updateUplink(
id,
req.body
);
if (!updatedUplink) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(200).json(updatedUplink);
} catch (error) {
res.status(500).json({ error: "Error updating uplink" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const deleted = await lpTtnEndDeviceUplinksService.deleteUplink(id);
if (!deleted) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Error deleting uplink" });
}
});
export default router;