78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import express, { Request, Response } from "express";
|
|
import { container } from "tsyringe";
|
|
import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService";
|
|
|
|
const ttnGatewayReceptionService = container.resolve(
|
|
TtnGatewayReceptionService
|
|
);
|
|
const router = express.Router();
|
|
|
|
router.get("/", async (req: Request, res: Response) => {
|
|
try {
|
|
const gatewayReceptions =
|
|
await ttnGatewayReceptionService.getAllGatewayReceptions();
|
|
res.status(200).json(gatewayReceptions);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error retrieving gateway receptions" });
|
|
}
|
|
});
|
|
|
|
router.get("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const gatewayReception =
|
|
await ttnGatewayReceptionService.getGatewayReceptionById(id);
|
|
if (!gatewayReception) {
|
|
res.status(404).json({ error: "Gateway reception not found" });
|
|
return;
|
|
}
|
|
res.status(200).json(gatewayReception);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error retrieving gateway reception" });
|
|
}
|
|
});
|
|
|
|
router.post("/", async (req: Request, res: Response) => {
|
|
try {
|
|
const newGatewayReception =
|
|
await ttnGatewayReceptionService.createTtnGatewayReception(req.body);
|
|
res.status(201).json(newGatewayReception);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error creating gateway reception" });
|
|
}
|
|
});
|
|
|
|
router.put("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const updatedGatewayReception =
|
|
await ttnGatewayReceptionService.updateGatewayReception({
|
|
...req.body,
|
|
ttn_gateway_reception_id: id,
|
|
});
|
|
if (!updatedGatewayReception) {
|
|
res.status(404).json({ error: "Gateway reception not found" });
|
|
return;
|
|
}
|
|
res.status(200).json(updatedGatewayReception);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error updating gateway reception" });
|
|
}
|
|
});
|
|
|
|
router.delete("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const deleted = await ttnGatewayReceptionService.deleteGatewayReception(id);
|
|
if (!deleted) {
|
|
res.status(404).json({ error: "Gateway reception not found" });
|
|
return;
|
|
}
|
|
res.status(204).send();
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error deleting gateway reception" });
|
|
}
|
|
});
|
|
|
|
export default router;
|