Skip to content

Commit

Permalink
feat(Coordinate): New API End-Point. Controller and Router
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcarvalhoj committed Mar 4, 2024
1 parent c50a16e commit 7215786
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from "express";
import userRouter from "./routes/user.routes";
import postRouter from "./routes/post.routes";
import coordRouter from "./routes/coordinate.routes";
import dotenv from "dotenv";
dotenv.config();
const app = express();
Expand All @@ -12,6 +13,7 @@ app.get("/", (req, res) => {
});
app.use("/users", userRouter);
app.use("/posts", postRouter);
app.use("/coords", coordRouter);

app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`);
Expand Down
45 changes: 45 additions & 0 deletions src/routes/coordinate.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Router, Request, Response } from "express";
import { v4 as uuidv4 } from "uuid";
import * as coordController from "../controllers/coordinate.controller";

interface Coordinate {
id: string;
createdAt: string;
lat: number;
lng: number;
}

const coordRouter = Router();

coordRouter.get("/", async (req: Request, res: Response) => {
try {
const data = await coordController.getCoordinates();
res.status(200).send(data);
} catch (error) {
res.status(500).send(error);
}
});

coordRouter.post("/", async (req: Request, res: Response) => {
try {
const Cord = req.body;
Cord.id = uuidv4();
Cord.createdAt = new Date().toISOString();
const data = await coordController.createCoordinate(Cord);
res.status(200).send(data);
} catch (error) {
res.status(500).send(error);
}
});

coordRouter.delete("/:id", async (req: Request, res: Response) => {
try {
const id = req.params.id;
const data = await coordController.deleteCoordinate(id);
res.status(200).send(data);
} catch (error) {
res.status(500).send(error);
}
});

export default coordRouter;

0 comments on commit 7215786

Please sign in to comment.