69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import express, { Request, Response } from "express";
|
|
import { container } from "tsyringe";
|
|
import { LocationService } from "../services/locationService";
|
|
|
|
const locationService = container.resolve(LocationService);
|
|
const router = express.Router();
|
|
|
|
router.get("/", async (req: Request, res: Response) => {
|
|
try {
|
|
const locations = await locationService.getAllLocations();
|
|
res.status(200).json(locations);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error retrieving locations" });
|
|
}
|
|
});
|
|
|
|
router.get("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const location = await locationService.getLocationById(id);
|
|
if (!location) {
|
|
res.status(404).json({ error: "Location not found" });
|
|
return;
|
|
}
|
|
res.status(200).json(location);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error retrieving location" });
|
|
}
|
|
});
|
|
|
|
router.post("/", async (req: Request, res: Response) => {
|
|
try {
|
|
const newLocation = await locationService.createLocation(req.body);
|
|
res.status(201).json(newLocation);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error creating location" });
|
|
}
|
|
});
|
|
|
|
router.put("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const updatedLocation = await locationService.updateLocation(id, req.body);
|
|
if (!updatedLocation) {
|
|
res.status(404).json({ error: "Location not found" });
|
|
return;
|
|
}
|
|
res.status(200).json(updatedLocation);
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error updating location" });
|
|
}
|
|
});
|
|
|
|
router.delete("/:id", async (req: Request, res: Response) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const deleted = await locationService.deleteLocation(id);
|
|
if (!deleted) {
|
|
res.status(404).json({ error: "Location not found" });
|
|
return;
|
|
}
|
|
res.status(204).send();
|
|
} catch (error) {
|
|
res.status(500).json({ error: "Error deleting location" });
|
|
}
|
|
});
|
|
|
|
export default router;
|