From 93954d5930925f5ee396e544af3b72a996a7e926 Mon Sep 17 00:00:00 2001 From: ArberHyseni Date: Sat, 28 Oct 2023 16:40:22 +0200 Subject: [PATCH] create post endpoint --- backend/src/controllers/index.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 1ddbba2..cb24c17 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,9 +1,12 @@ import express, { Express, Request, Response, Application } from "express"; import dotenv from "dotenv"; import neo4j from "neo4j-driver"; -//For env File dotenv.config(); +const app: Application = express(); +app.use(express.json()) +const port = process.env.PORT || 8000; + export const createDriver = async () => { console.log(process.env.NEO4J_URI, process.env.NEO4J_USER); const driver = neo4j.driver( @@ -17,9 +20,6 @@ export const createDriver = async () => { return driver; }; -const app: Application = express(); -const port = process.env.PORT || 8000; - app.get("/", async (req: Request, res: Response) => { const driver = await createDriver(); @@ -50,18 +50,24 @@ app.get("/login", async (req: Request, res: Response) => { res.status(200).send("Sucessfully logged in as " + username); }); -app.post("/query", async (req: Request, res: Response) => { +app.post("/history", async (req: Request, res: Response) => { + console.log(req); + const data = req.body; + + console.log({data}); + const driver = await createDriver(); const session = driver.session(); - const createdRec = await session.run( - "CREATE (p:User {surname : 'Doe', name: 'John'}) RETURN p.name AS name" + const createdUserHistoryRel = await session.run( + "MATCH (u:User {surname : 'Doe'}) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", + {history: data} ); - console.log(createdRec); + return res.json(createdUserHistoryRel) }); app.listen(port, () => { - console.log(`Server is Fire at http://localhost:${port}`); + console.log(`Server is running at http://localhost:${port}`); });