From 48df930d6ef8200fde07b67f484266cd2c67a9d4 Mon Sep 17 00:00:00 2001 From: ArberHyseni Date: Sat, 28 Oct 2023 15:20:12 +0200 Subject: [PATCH 01/16] finalize connection --- .../interfaces/neo4j-connection-instance.ts | 13 +++ backend/package-lock.json | 108 ++++++++++++++++++ backend/package.json | 1 + backend/src/controllers/index.ts | 22 +++- 4 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 backend/interfaces/neo4j-connection-instance.ts diff --git a/backend/interfaces/neo4j-connection-instance.ts b/backend/interfaces/neo4j-connection-instance.ts new file mode 100644 index 0000000..041006d --- /dev/null +++ b/backend/interfaces/neo4j-connection-instance.ts @@ -0,0 +1,13 @@ +import { Config } from "neo4j-driver"; + +export type Neo4jScheme = 'neo4j' | 'neo4j+s' | 'neo4j+scc' | 'bolt' | 'bolt+s' | 'bolt+scc' + +export interface Neo4jConnection { + scheme: Neo4jScheme; + host: string; + port: number | string; + username: string; + password: string; + database?: string; + config?: Config; +} \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index 4fb3a15..113bfa7 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -12,6 +12,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", + "neo4j-driver": "^5.14.0", "nodemon": "^3.0.1" }, "devDependencies": { @@ -266,6 +267,25 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/binary-extensions": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", @@ -317,6 +337,29 @@ "node": ">=8" } }, + "node_modules/buffer": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.2.1" + } + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -764,6 +807,25 @@ "node": ">=0.10.0" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/ignore-by-default": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", @@ -955,6 +1017,31 @@ "node": ">= 0.6" } }, + "node_modules/neo4j-driver": { + "version": "5.14.0", + "resolved": "https://registry.npmjs.org/neo4j-driver/-/neo4j-driver-5.14.0.tgz", + "integrity": "sha512-K8x3UtEHXJQtfHLdqc+DU+OJsujNen2I9schZWs3mtnIYXcP+dFvWLusaXxRDQO1MBnplM3nvR54eYUYMqhfnw==", + "dependencies": { + "neo4j-driver-bolt-connection": "5.14.0", + "neo4j-driver-core": "5.14.0", + "rxjs": "^7.8.1" + } + }, + "node_modules/neo4j-driver-bolt-connection": { + "version": "5.14.0", + "resolved": "https://registry.npmjs.org/neo4j-driver-bolt-connection/-/neo4j-driver-bolt-connection-5.14.0.tgz", + "integrity": "sha512-JocVRGGSTLtqcLwSsbodU6ZWVf4OPN5X1ozmy2OfRhmRR7CfhHaImjT9N0MBZ9NcahH9V5XqLroRK7DZdqKJEg==", + "dependencies": { + "buffer": "^6.0.3", + "neo4j-driver-core": "5.14.0", + "string_decoder": "^1.3.0" + } + }, + "node_modules/neo4j-driver-core": { + "version": "5.14.0", + "resolved": "https://registry.npmjs.org/neo4j-driver-core/-/neo4j-driver-core-5.14.0.tgz", + "integrity": "sha512-IpO2BOazTF2QLGE7kYjwbicBxGdsu/JkDhdSmsziBcqEiRb9TBXRILhvlxiC00p1QyqMi7iVmJtIsbROqi5e/w==" + }, "node_modules/nodemon": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", @@ -1185,6 +1272,14 @@ "rimraf": "bin.js" } }, + "node_modules/rxjs": { + "version": "7.8.1", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.1.tgz", + "integrity": "sha512-AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg==", + "dependencies": { + "tslib": "^2.1.0" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -1335,6 +1430,14 @@ "node": ">= 0.8" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", @@ -1504,6 +1607,11 @@ "strip-json-comments": "^2.0.0" } }, + "node_modules/tslib": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", + "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", diff --git a/backend/package.json b/backend/package.json index be4cd59..569e2c6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -15,6 +15,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", + "neo4j-driver": "^5.14.0", "nodemon": "^3.0.1" }, "devDependencies": { diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index c9cc1c4..acf9ef9 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,14 +1,30 @@ import express, { Express, Request, Response , Application } from 'express'; import dotenv from 'dotenv'; - +import neo4j from 'neo4j-driver' //For env File dotenv.config(); +export const createDriver = async () => { + const driver = neo4j.driver( + String(process.env.NEO4J_URI), + neo4j.auth.basic(String(process.env.NEO4J_USER), String(process.env.NEO4J_PASSWORD)) + ) + + return driver +} + const app: Application = express(); const port = process.env.PORT || 8000; -app.get('/', (req: Request, res: Response) => { - res.send('Welcome to Express & TypeScript Server'); +app.get('/', async (req: Request, res: Response) => { + const driver = await createDriver(); + + const session = driver.session() + + const {records} = await session.run('MATCH (p:User) return p'); + + const result = records.map(record => record.get('p')) + res.json(result) }); app.listen(port, () => { From 0aaf33c9a5df6f55559b6d258ff9696beaff4d0b Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sat, 28 Oct 2023 16:15:38 +0200 Subject: [PATCH 02/16] username search and user creation --- backend/src/controllers/index.ts | 65 ++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index acf9ef9..1ddbba2 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,32 +1,67 @@ -import express, { Express, Request, Response , Application } from 'express'; -import dotenv from 'dotenv'; -import neo4j from 'neo4j-driver' -//For env File +import express, { Express, Request, Response, Application } from "express"; +import dotenv from "dotenv"; +import neo4j from "neo4j-driver"; +//For env File dotenv.config(); export const createDriver = async () => { + console.log(process.env.NEO4J_URI, process.env.NEO4J_USER); const driver = neo4j.driver( - String(process.env.NEO4J_URI), - neo4j.auth.basic(String(process.env.NEO4J_USER), String(process.env.NEO4J_PASSWORD)) - ) + String(process.env.NEO4J_URI), + neo4j.auth.basic( + String(process.env.NEO4J_USER), + String(process.env.NEO4J_PASSWORD) + ) + ); - return driver -} + return driver; +}; const app: Application = express(); const port = process.env.PORT || 8000; -app.get('/', async (req: Request, res: Response) => { +app.get("/", async (req: Request, res: Response) => { const driver = await createDriver(); - const session = driver.session() + const session = driver.session(); - const {records} = await session.run('MATCH (p:User) return p'); + const { records } = await session.run("MATCH (p:User) return p"); - const result = records.map(record => record.get('p')) - res.json(result) + const result = records.map((record) => record.get("p")); + res.json(result); +}); + +app.get("/login", async (req: Request, res: Response) => { + const { username, password } = req.query; + const driver = await createDriver(); + + const session = driver.session(); + + const { records } = await session.run( + "MATCH (p:User {surname: $propertyValue, name: $password }) return p", + { propertyValue: username, password: password } + ); + + if (records.length <= 0) { + res.status(401).send("Login Failed!"); + return; + } + + res.status(200).send("Sucessfully logged in as " + username); +}); + +app.post("/query", async (req: Request, res: Response) => { + 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" + ); + + console.log(createdRec); }); app.listen(port, () => { console.log(`Server is Fire at http://localhost:${port}`); -}); \ No newline at end of file +}); From 93954d5930925f5ee396e544af3b72a996a7e926 Mon Sep 17 00:00:00 2001 From: ArberHyseni Date: Sat, 28 Oct 2023 16:40:22 +0200 Subject: [PATCH 03/16] 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}`); }); From bd6f5e1775548add29c006374f4d12d6da408312 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:29:28 +0200 Subject: [PATCH 04/16] added get for history and modified post --- backend/src/controllers/index.ts | 47 ++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index cb24c17..6af375a 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -4,7 +4,7 @@ import neo4j from "neo4j-driver"; dotenv.config(); const app: Application = express(); -app.use(express.json()) +app.use(express.json()); const port = process.env.PORT || 8000; export const createDriver = async () => { @@ -51,23 +51,52 @@ app.get("/login", async (req: Request, res: Response) => { }); app.post("/history", async (req: Request, res: Response) => { - console.log(req); const data = req.body; - - console.log({data}); + + console.log({ data }); const driver = await createDriver(); const session = driver.session(); + try { + 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 } + ); + return res.status(200).json({ + response: "Inserted into history", + }); + } catch (error) { + return res.status(500).json({ response: "Failed to insert into history!" }); + } +}); + +app.get("/history", async (req: Request, res: Response) => { + const { userID } = req.query; + const driver = await createDriver(); + const session = driver.session(); + console.log(userID); - 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} + const { records } = await session.run( + "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h", + { userIDDB: Number(userID) } ); - return res.json(createdUserHistoryRel) -}); + if (records.length <= 0) { + return res.status(200).json([]); + } + + const attributes = records.map((record) => { + const data = record.get("h"); + return { + title: data.properties.title, + description_uri: data.properties.description_uri, + language: data.properties.language, + }; + }); + res.json(attributes); +}); app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); From 787af85c5214a9d74a59ccbc78c0ff5a35261966 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:34:28 +0200 Subject: [PATCH 05/16] updated post with UserID --- backend/src/controllers/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 6af375a..f972c2e 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -51,6 +51,7 @@ app.get("/login", async (req: Request, res: Response) => { }); app.post("/history", async (req: Request, res: Response) => { + const { userID } = req.query; const data = req.body; console.log({ data }); @@ -60,8 +61,8 @@ app.post("/history", async (req: Request, res: Response) => { const session = driver.session(); try { 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 } + "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", + { history: data, userID: Number(userID) } ); return res.status(200).json({ response: "Inserted into history", From 28bf7ef698507c08ae005cb4f65b9eb34adecd51 Mon Sep 17 00:00:00 2001 From: MigiKTHZ <102576118+MigiKTHZ@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:45:14 +0200 Subject: [PATCH 06/16] Update Deleteroute --- backend/src/controllers/index.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index f972c2e..265c19a 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -50,15 +50,33 @@ app.get("/login", async (req: Request, res: Response) => { res.status(200).send("Sucessfully logged in as " + username); }); +app.delete("/history", async (req: Request, res: Response) => { + const { id } = req.query; + const driver = await createDriver(); + + const session = driver.session(); + + const result = await session.run( + "MATCH (h:History) WHERE ID(h) = $history DETACH DELETE h", + { history: Number(id) } + ); + + res.status(200).end(); +}); + app.post("/history", async (req: Request, res: Response) => { const { userID } = req.query; const data = req.body; +<<<<<<< Updated upstream console.log({ data }); +======= +>>>>>>> Stashed changes const driver = await createDriver(); const session = driver.session(); +<<<<<<< Updated upstream try { const createdUserHistoryRel = await session.run( "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", @@ -70,6 +88,15 @@ app.post("/history", async (req: Request, res: Response) => { } catch (error) { return res.status(500).json({ response: "Failed to insert into history!" }); } +======= + + 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 } + ); + + return res.json(createdUserHistoryRel) +>>>>>>> Stashed changes }); app.get("/history", async (req: Request, res: Response) => { From c7391efa6f86ef66b2196867ba799932c1983058 Mon Sep 17 00:00:00 2001 From: MigiKTHZ <102576118+MigiKTHZ@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:46:15 +0200 Subject: [PATCH 07/16] Update Deleteroute --- backend/src/controllers/index.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 265c19a..07eab6f 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -67,28 +67,10 @@ app.delete("/history", async (req: Request, res: Response) => { app.post("/history", async (req: Request, res: Response) => { const { userID } = req.query; const data = req.body; -<<<<<<< Updated upstream - - console.log({ data }); -======= ->>>>>>> Stashed changes const driver = await createDriver(); const session = driver.session(); -<<<<<<< Updated upstream - try { - const createdUserHistoryRel = await session.run( - "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", - { history: data, userID: Number(userID) } - ); - return res.status(200).json({ - response: "Inserted into history", - }); - } catch (error) { - return res.status(500).json({ response: "Failed to insert into history!" }); - } -======= const createdUserHistoryRel = await session.run( "MATCH (u:User {surname : 'Doe'}) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", @@ -96,7 +78,6 @@ app.post("/history", async (req: Request, res: Response) => { ); return res.json(createdUserHistoryRel) ->>>>>>> Stashed changes }); app.get("/history", async (req: Request, res: Response) => { From 2878bb66f6eac4c9dc7562adfc9709161ffbd828 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:03:44 +0200 Subject: [PATCH 08/16] added userID in Login --- backend/src/controllers/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index f972c2e..c5b2441 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -33,21 +33,25 @@ app.get("/", async (req: Request, res: Response) => { app.get("/login", async (req: Request, res: Response) => { const { username, password } = req.query; + + console.log(username, password); const driver = await createDriver(); const session = driver.session(); const { records } = await session.run( - "MATCH (p:User {surname: $propertyValue, name: $password }) return p", + "MATCH (p:User {surname: $propertyValue, name: $password }) RETURN ID(p) AS userId, p", { propertyValue: username, password: password } ); - if (records.length <= 0) { res.status(401).send("Login Failed!"); return; } - res.status(200).send("Sucessfully logged in as " + username); + res.status(200).json({ + message: "Successfully logged in!", + userID: records[0].get("userId").toInt(), + }); }); app.post("/history", async (req: Request, res: Response) => { From 2b80180742c68918be86f7127633275e9189647b Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:18:09 +0200 Subject: [PATCH 09/16] added conversation ID in get history --- backend/src/controllers/index.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index ba8bc67..148e71d 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -75,23 +75,26 @@ app.post("/history", async (req: Request, res: Response) => { const driver = await createDriver(); const session = driver.session(); - - 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 } - ); - - return res.json(createdUserHistoryRel) + try { + const createdUserHistoryRel = await session.run( + "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", + { history: data, userID: Number(userID) } + ); + return res.status(200).json({ + response: "Inserted into history", + }); + } catch (error) { + return res.status(500).json({ response: "Failed to insert into history!" }); + } }); app.get("/history", async (req: Request, res: Response) => { const { userID } = req.query; const driver = await createDriver(); const session = driver.session(); - console.log(userID); const { records } = await session.run( - "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h", + "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h, ID(h) as conversationID", { userIDDB: Number(userID) } ); @@ -101,9 +104,10 @@ app.get("/history", async (req: Request, res: Response) => { const attributes = records.map((record) => { const data = record.get("h"); + return { + conversation_id: record.get("conversationID").toInt(), title: data.properties.title, - description_uri: data.properties.description_uri, language: data.properties.language, }; }); From a610a687ad6889ea76016903c20368f9c58ecf7e Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sun, 29 Oct 2023 01:03:48 +0200 Subject: [PATCH 10/16] added conversation API Route Added a GET API-Endpoint to get all conversations for a single historyID Also Added a POST-Endpoint to post new conversations with mock data --- backend/src/controllers/index.ts | 82 +++++++++++++++++++++++++++++-- backend/src/types/conversation.ts | 18 +++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 backend/src/types/conversation.ts diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 148e71d..64851e9 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,6 +1,8 @@ import express, { Express, Request, Response, Application } from "express"; import dotenv from "dotenv"; import neo4j from "neo4j-driver"; +import { conversationResponse, conversation } from "../types/conversation"; + dotenv.config(); const app: Application = express(); @@ -8,7 +10,6 @@ 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( String(process.env.NEO4J_URI), neo4j.auth.basic( @@ -94,7 +95,7 @@ app.get("/history", async (req: Request, res: Response) => { const session = driver.session(); const { records } = await session.run( - "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h, ID(h) as conversationID", + "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h, ID(h) as historyID", { userIDDB: Number(userID) } ); @@ -106,7 +107,7 @@ app.get("/history", async (req: Request, res: Response) => { const data = record.get("h"); return { - conversation_id: record.get("conversationID").toInt(), + historyID: record.get("historyID").toInt(), title: data.properties.title, language: data.properties.language, }; @@ -114,6 +115,81 @@ app.get("/history", async (req: Request, res: Response) => { res.json(attributes); }); + +app.get("/converation", async (req: Request, res: Response) => { + const { historyID } = req.query; + if (!historyID) { + return res.status(404).json({ + response: "History not found!", + }); + } + const driver = await createDriver(); + const session = driver.session(); + const { records } = await session.run( + "MATCH (h:History where ID(h) = $historyID)-[r:HAS_CONVERSATION]->(c:Conversation) return c, h ORDER BY c.createdAt ASC", + { historyID: Number(historyID) } + ); + + if (records.length <= 0) { + return res.status(404).json({ + response: "No Conversation found!", + }); + } + + const historyRecord = records[0].get("h"); + const historyTitle = historyRecord.properties.title; + + const allConversations: conversation[] = records.map((record) => { + const singleRecord = record.get("c"); + const createdAt = singleRecord.properties.createdAt; + return { + prompt: singleRecord.properties.prompt, + request: singleRecord.properties.request, + docs: singleRecord.properties.docs, + createdAt: new Date( + createdAt.year.low, + createdAt.month.low - 1, + createdAt.day.low, + createdAt.hour.low, + createdAt.minute.low, + createdAt.second.low, + createdAt.nanosecond.low / 1000000 + ), + }; + }); + + const conversationResponse: conversationResponse = { + historyID: Number(historyID), + title: historyTitle, + conversations: allConversations, + }; + + return res.status(200).json(conversationResponse); +}); + +app.post("/conversation", async (req: Request, res: Response) => { + const { historyID } = req.query; + const convo = req.body; + + const driver = await createDriver(); + const session = driver.session(); + + try { + const createdConvo = await session.run( + "MATCH (h:History where ID(h) = $historyID) CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $convo CREATE (h)-[:HAS_CONVERSATION]->(c)", + { historyID: Number(historyID), convo: convo } + ); + } catch (error) { + return res.status(500).json({ + response: "Failed to create a new conversation", + }); + } + + return res.status(200).json({ + response: "Successfully created conversation", + }); +}); + app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); diff --git a/backend/src/types/conversation.ts b/backend/src/types/conversation.ts new file mode 100644 index 0000000..29ab31d --- /dev/null +++ b/backend/src/types/conversation.ts @@ -0,0 +1,18 @@ +import { DateTime } from "neo4j-driver"; + +export type docs = { + KBView_URL: string; + serviceNowNumber: number; +}; + +export type conversation = { + prompt: string; + request: string; + createdAt: Date; + docs: docs[]; +}; +export type conversationResponse = { + historyID: number; + title: string; + conversations: conversation[]; +}; From 001963f56533a17ddfd04e9e68f20f11ac5602b5 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sun, 29 Oct 2023 02:58:22 +0200 Subject: [PATCH 11/16] added template for the LLMData --- backend/src/controllers/index.ts | 66 ++++++++++++++++++++++++++------ backend/src/types/LLMData.ts | 0 2 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 backend/src/types/LLMData.ts diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 64851e9..618bca5 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -34,8 +34,6 @@ app.get("/", async (req: Request, res: Response) => { app.get("/login", async (req: Request, res: Response) => { const { username, password } = req.query; - - console.log(username, password); const driver = await createDriver(); const session = driver.session(); @@ -45,28 +43,30 @@ app.get("/login", async (req: Request, res: Response) => { { propertyValue: username, password: password } ); if (records.length <= 0) { - res.status(401).send("Login Failed!"); + res.status(401).json({ + response: "Login Failed!", + }); return; } res.status(200).json({ - message: "Successfully logged in!", + response: "Successfully logged in!", userID: records[0].get("userId").toInt(), }); }); app.delete("/history", async (req: Request, res: Response) => { - const { id } = req.query; + const { historyID, userID } = req.query; const driver = await createDriver(); const session = driver.session(); const result = await session.run( "MATCH (h:History) WHERE ID(h) = $history DETACH DELETE h", - { history: Number(id) } + { history: Number(historyID) } ); - res.status(200).end(); + res.status(200).json(await getHistoryByUser(Number(userID))); }); app.post("/history", async (req: Request, res: Response) => { @@ -91,7 +91,7 @@ app.post("/history", async (req: Request, res: Response) => { app.get("/history", async (req: Request, res: Response) => { const { userID } = req.query; - const driver = await createDriver(); + /* const driver = await createDriver(); const session = driver.session(); const { records } = await session.run( @@ -111,16 +111,16 @@ app.get("/history", async (req: Request, res: Response) => { title: data.properties.title, language: data.properties.language, }; - }); + }); */ - res.json(attributes); + res.status(200).json(await getHistoryByUser(Number(userID))); }); -app.get("/converation", async (req: Request, res: Response) => { +app.get("/conversation", async (req: Request, res: Response) => { const { historyID } = req.query; if (!historyID) { return res.status(404).json({ - response: "History not found!", + response: "historyID is required!", }); } const driver = await createDriver(); @@ -190,6 +190,48 @@ app.post("/conversation", async (req: Request, res: Response) => { }); }); +const getLLMData = async (request: string) => { + const requestURL: string = (process.env.LLM_URI as string) + "/search"; + const response = await fetch(requestURL, { + method: "GET", + mode: "cors", + cache: "no-cache", + headers: { + "Content-Type": "application/json", + }, + }); + if (!response.ok) { + //TODO Error Handling + } + + return await response.json(); +}; + +const getHistoryByUser = async (userID: Number) => { + const driver = await createDriver(); + const session = driver.session(); + + const { records } = await session.run( + "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h, ID(h) as historyID", + { userIDDB: Number(userID) } + ); + + if (records.length <= 0) { + return []; + } + + const attributes = records.map((record) => { + const data = record.get("h"); + + return { + historyID: record.get("historyID").toInt(), + title: data.properties.title, + language: data.properties.language, + }; + }); + return attributes; +}; + app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); diff --git a/backend/src/types/LLMData.ts b/backend/src/types/LLMData.ts new file mode 100644 index 0000000..e69de29 From b419509e7f7626c4ede17c6519f6a4c9a42ae048 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sun, 29 Oct 2023 07:02:03 +0100 Subject: [PATCH 12/16] added closing connection --- backend/src/controllers/index.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 618bca5..63b0aa1 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,6 +1,6 @@ import express, { Express, Request, Response, Application } from "express"; import dotenv from "dotenv"; -import neo4j from "neo4j-driver"; +import neo4j, { Driver, Session } from "neo4j-driver"; import { conversationResponse, conversation } from "../types/conversation"; dotenv.config(); @@ -29,6 +29,7 @@ app.get("/", async (req: Request, res: Response) => { const { records } = await session.run("MATCH (p:User) return p"); const result = records.map((record) => record.get("p")); + closeConnection(driver, session); res.json(result); }); @@ -46,9 +47,10 @@ app.get("/login", async (req: Request, res: Response) => { res.status(401).json({ response: "Login Failed!", }); + closeConnection(driver, session); return; } - + closeConnection(driver, session); res.status(200).json({ response: "Successfully logged in!", userID: records[0].get("userId").toInt(), @@ -65,7 +67,7 @@ app.delete("/history", async (req: Request, res: Response) => { "MATCH (h:History) WHERE ID(h) = $history DETACH DELETE h", { history: Number(historyID) } ); - + closeConnection(driver, session); res.status(200).json(await getHistoryByUser(Number(userID))); }); @@ -81,10 +83,12 @@ app.post("/history", async (req: Request, res: Response) => { "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", { history: data, userID: Number(userID) } ); + closeConnection(driver, session); return res.status(200).json({ response: "Inserted into history", }); } catch (error) { + closeConnection(driver, session); return res.status(500).json({ response: "Failed to insert into history!" }); } }); @@ -129,7 +133,7 @@ app.get("/conversation", async (req: Request, res: Response) => { "MATCH (h:History where ID(h) = $historyID)-[r:HAS_CONVERSATION]->(c:Conversation) return c, h ORDER BY c.createdAt ASC", { historyID: Number(historyID) } ); - + closeConnection(driver, session); if (records.length <= 0) { return res.status(404).json({ response: "No Conversation found!", @@ -180,11 +184,12 @@ app.post("/conversation", async (req: Request, res: Response) => { { historyID: Number(historyID), convo: convo } ); } catch (error) { + closeConnection(driver, session); return res.status(500).json({ response: "Failed to create a new conversation", }); } - + closeConnection(driver, session); return res.status(200).json({ response: "Successfully created conversation", }); @@ -215,7 +220,8 @@ const getHistoryByUser = async (userID: Number) => { "MATCH (p:User where ID(p) = $userIDDB)-[r:HAS_HISTORY]->(h:History) return h, ID(h) as historyID", { userIDDB: Number(userID) } ); - + closeConnection(driver, session); + if (records.length <= 0) { return []; } @@ -232,6 +238,11 @@ const getHistoryByUser = async (userID: Number) => { return attributes; }; +const closeConnection = (driver: Driver, session: Session) => { + session.close(); + driver.close(); +}; + app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); From 92678fced1b0eec81a25175ef4aff71197c216dd Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sun, 29 Oct 2023 07:25:42 +0100 Subject: [PATCH 13/16] modified history delete to delete related converations --- backend/src/controllers/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 63b0aa1..4a2b19d 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -62,9 +62,10 @@ app.delete("/history", async (req: Request, res: Response) => { const driver = await createDriver(); const session = driver.session(); - + //"MATCH (h:History WHERE ID(h) = $history)-[:HAS_CONVERSATION]->(c:Conversation)-[r]->(relatedNode) DETACH DELETE h, relatedNode", + const result = await session.run( - "MATCH (h:History) WHERE ID(h) = $history DETACH DELETE h", + "MATCH (h:History WHERE ID(h) = $history)-[:HAS_CONVERSATION]->(c:Conversation) detach delete h, c", { history: Number(historyID) } ); closeConnection(driver, session); @@ -221,7 +222,7 @@ const getHistoryByUser = async (userID: Number) => { { userIDDB: Number(userID) } ); closeConnection(driver, session); - + if (records.length <= 0) { return []; } From aaf98078d9a64b14da9da4861529bbb77702c299 Mon Sep 17 00:00:00 2001 From: Paul-Philippe Wortmann <65237023+TottySnowman@users.noreply.github.com> Date: Sun, 29 Oct 2023 08:15:54 +0100 Subject: [PATCH 14/16] created new func to add a convo --- backend/src/controllers/index.ts | 82 ++++++++++++++++++++++--------- backend/src/types/conversation.ts | 7 ++- 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index 4a2b19d..c9593eb 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,7 +1,12 @@ import express, { Express, Request, Response, Application } from "express"; import dotenv from "dotenv"; import neo4j, { Driver, Session } from "neo4j-driver"; -import { conversationResponse, conversation } from "../types/conversation"; +import { + conversationResponse, + conversation, + createdResponse, + docs, +} from "../types/conversation"; dotenv.config(); @@ -62,10 +67,8 @@ app.delete("/history", async (req: Request, res: Response) => { const driver = await createDriver(); const session = driver.session(); - //"MATCH (h:History WHERE ID(h) = $history)-[:HAS_CONVERSATION]->(c:Conversation)-[r]->(relatedNode) DETACH DELETE h, relatedNode", - const result = await session.run( - "MATCH (h:History WHERE ID(h) = $history)-[:HAS_CONVERSATION]->(c:Conversation) detach delete h, c", + "MATCH (h:History WHERE ID(h) = $history)-[:HAS_CONVERSATION]->(c:Conversation) detach delete h, c", { history: Number(historyID) } ); closeConnection(driver, session); @@ -84,13 +87,14 @@ app.post("/history", async (req: Request, res: Response) => { "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", { history: data, userID: Number(userID) } ); - closeConnection(driver, session); - return res.status(200).json({ + + res.status(200).json({ response: "Inserted into history", }); } catch (error) { + res.status(500).json({ response: "Failed to insert into history!" }); + } finally { closeConnection(driver, session); - return res.status(500).json({ response: "Failed to insert into history!" }); } }); @@ -174,26 +178,25 @@ app.get("/conversation", async (req: Request, res: Response) => { app.post("/conversation", async (req: Request, res: Response) => { const { historyID } = req.query; + //Search search from frontend const convo = req.body; + const prompt = "Prompt from MML"; + const results: docs[] = []; - const driver = await createDriver(); - const session = driver.session(); + const convoObj: conversation = { + prompt: prompt, + request: req.body, + docs: results, + }; - try { - const createdConvo = await session.run( - "MATCH (h:History where ID(h) = $historyID) CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $convo CREATE (h)-[:HAS_CONVERSATION]->(c)", - { historyID: Number(historyID), convo: convo } - ); - } catch (error) { - closeConnection(driver, session); - return res.status(500).json({ - response: "Failed to create a new conversation", - }); - } - closeConnection(driver, session); - return res.status(200).json({ - response: "Successfully created conversation", - }); + const addConvoResponse: createdResponse = await addConversation( + convo, + Number(historyID) + ); + + return res + .status(addConvoResponse.statusCode) + .json(addConvoResponse.response); }); const getLLMData = async (request: string) => { @@ -213,6 +216,37 @@ const getLLMData = async (request: string) => { return await response.json(); }; +const addConversation = async ( + conversationData: conversation, + historyID: number +) => { + const driver = await createDriver(); + const session = driver.session(); + try { + const createdConvo = await session.run( + "MATCH (h:History where ID(h) = $historyID) CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $convo CREATE (h)-[:HAS_CONVERSATION]->(c)", + { historyID: Number(historyID), convo: conversationData } + ); + closeConnection(driver, session); + const response: createdResponse = { + statusCode: 500, + response: { + response: "Successfully created conversation", + }, + }; + return response; + } catch (error) { + closeConnection(driver, session); + const response: createdResponse = { + statusCode: 500, + response: { + response: "Failed to create conversation!", + }, + }; + return response; + } +}; + const getHistoryByUser = async (userID: Number) => { const driver = await createDriver(); const session = driver.session(); diff --git a/backend/src/types/conversation.ts b/backend/src/types/conversation.ts index 29ab31d..433c56c 100644 --- a/backend/src/types/conversation.ts +++ b/backend/src/types/conversation.ts @@ -2,13 +2,12 @@ import { DateTime } from "neo4j-driver"; export type docs = { KBView_URL: string; - serviceNowNumber: number; }; export type conversation = { prompt: string; request: string; - createdAt: Date; + createdAt?: Date; docs: docs[]; }; export type conversationResponse = { @@ -16,3 +15,7 @@ export type conversationResponse = { title: string; conversations: conversation[]; }; +export type createdResponse = { + statusCode: number; + response: {}; +}; From 7ff1a3b655ce60ab0473cd0a62111f5a29400e64 Mon Sep 17 00:00:00 2001 From: MigiKTHZ <102576118+MigiKTHZ@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:09:02 +0100 Subject: [PATCH 15/16] Update index.ts --- backend/src/controllers/index.ts | 74 +++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index c9593eb..b563a2c 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -1,4 +1,4 @@ -import express, { Express, Request, Response, Application } from "express"; +import express, { Express, Request, Response, Application, request } from "express"; import dotenv from "dotenv"; import neo4j, { Driver, Session } from "neo4j-driver"; import { @@ -75,29 +75,73 @@ app.delete("/history", async (req: Request, res: Response) => { res.status(200).json(await getHistoryByUser(Number(userID))); }); -app.post("/history", async (req: Request, res: Response) => { - const { userID } = req.query; +app.post("/query", async (req: Request, res: Response) => { + const { userID, historyID } = req.query; const data = req.body; const driver = await createDriver(); const session = driver.session(); - try { - const createdUserHistoryRel = await session.run( - "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", - { history: data, userID: Number(userID) } - ); - res.status(200).json({ - response: "Inserted into history", - }); - } catch (error) { - res.status(500).json({ response: "Failed to insert into history!" }); - } finally { - closeConnection(driver, session); + const createdUserHistoryRel = await session.run( + "MATCH (u:User where ID(u) = $userID) CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h)", + { history: data, userID: Number(userID) } + ); + + if (!historyID) { + const returnedVal = createHistory(Number(userID), data) + + const hID = Number((await returnedVal).hID) + console.log(hID) + + const createdHistoryConversationRel = await session.run( + "MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", + { conversation: data, hID: Number(hID) } + ); + return res.json(createdHistoryConversationRel) + } else { + const appendHistoryConversationRel = await session.run( + "MATCH (h:History) WHERE ID(h) = $history CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", + { conversation: data, history: Number(historyID) } + ); + return res.json(appendHistoryConversationRel) } + }); +async function createHistory(userID: number, retrievedData: string) { + const data = retrievedData; + + const driver = await createDriver(); + + const session = driver.session(); + + const createdUserHistoryRel = await session.run( + "MATCH (u:User) WHERE ID(u) = $uID CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h) return ID(h) as nodeId", + { history: data, uID: userID } + ); + const historyID: number = createdUserHistoryRel.records[0].get("nodeId"); + + const returnData = { "response": createdUserHistoryRel, "hID": historyID } + return returnData +} + +// app.post("/history", async (req: Request, res: Response) => { +// const { userID } = req.query; +// const data = req.body; + +// const driver = await createDriver(); + +// const session = driver.session(); + +// 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 } +// ); + +// return res.json(createdUserHistoryRel) +// }); + app.get("/history", async (req: Request, res: Response) => { const { userID } = req.query; /* const driver = await createDriver(); From 978ea52092021a0e88f688a46d62616e0ad254b9 Mon Sep 17 00:00:00 2001 From: MigiKTHZ <102576118+MigiKTHZ@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:36:42 +0100 Subject: [PATCH 16/16] Update index.ts --- backend/src/controllers/index.ts | 69 ++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/backend/src/controllers/index.ts b/backend/src/controllers/index.ts index b563a2c..761a220 100644 --- a/backend/src/controllers/index.ts +++ b/backend/src/controllers/index.ts @@ -75,10 +75,20 @@ app.delete("/history", async (req: Request, res: Response) => { res.status(200).json(await getHistoryByUser(Number(userID))); }); -app.post("/query", async (req: Request, res: Response) => { +app.post("/search", async (req: Request, res: Response) => { const { userID, historyID } = req.query; const data = req.body; + const convo = req.body; + const prompt = "Prompt from MML"; + const results: docs[] = []; + + const convoObj: conversation = { + prompt: prompt, + request: req.body, + docs: results, + }; + const driver = await createDriver(); const session = driver.session(); @@ -91,14 +101,18 @@ app.post("/query", async (req: Request, res: Response) => { if (!historyID) { const returnedVal = createHistory(Number(userID), data) - const hID = Number((await returnedVal).hID) - console.log(hID) - - const createdHistoryConversationRel = await session.run( - "MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", - { conversation: data, hID: Number(hID) } - ); - return res.json(createdHistoryConversationRel) + const response = Number((await returnedVal).response.statusCode) + + if (response == 200) { + const hID = Number((await returnedVal).hID) + console.log(hID) + + const createdHistoryConversationRel = await session.run( + "MATCH (h:History) WHERE ID(h) = $hID CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", + { conversation: data, hID: Number(hID) } + ); + return res.json(createdHistoryConversationRel) + } } else { const appendHistoryConversationRel = await session.run( "MATCH (h:History) WHERE ID(h) = $history CREATE (c:Conversation {createdAt: localdatetime()}) SET c += $conversation CREATE (h)-[:HAS_CONVERSATION]->(c)", @@ -116,14 +130,35 @@ async function createHistory(userID: number, retrievedData: string) { const session = driver.session(); - const createdUserHistoryRel = await session.run( - "MATCH (u:User) WHERE ID(u) = $uID CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h) return ID(h) as nodeId", - { history: data, uID: userID } - ); - const historyID: number = createdUserHistoryRel.records[0].get("nodeId"); + try { + const createdUserHistoryRel = await session.run( + "MATCH (u:User) WHERE ID(u) = $uID CREATE (h:History {createdAt: localdatetime()}) SET h += $history CREATE (u)-[:HAS_HISTORY]->(h) return ID(h) as nodeId", + { history: data, uID: userID } + ); + const historyID: number = createdUserHistoryRel.records[0].get("nodeId"); + const response: createdResponse = { + statusCode: 200, + response: { + response: "Successfully created conversation", + } + } + const returnData = { "response": response, "hID": historyID } + return returnData + } catch (error) { + const response: createdResponse = { + statusCode: 500, + response: { + response: "Successfully created conversation", + } + } + const returnData = { "response": response, "hID": null} + return returnData + } + + + + - const returnData = { "response": createdUserHistoryRel, "hID": historyID } - return returnData } // app.post("/history", async (req: Request, res: Response) => { @@ -273,7 +308,7 @@ const addConversation = async ( ); closeConnection(driver, session); const response: createdResponse = { - statusCode: 500, + statusCode: 200, response: { response: "Successfully created conversation", },