diff --git a/sg-backend/db.py b/sg-backend/db.py index f5a1c7b67b66b1e0d2f9031fba66b4fd0466cf06..1e5f3a04335faf0a3fd14dc06d94ac7fe92cc89e 100644 --- a/sg-backend/db.py +++ b/sg-backend/db.py @@ -62,3 +62,22 @@ class Neo4j: results = session.run("MATCH (a) return Id(a) as key, a.name as name") return results.data() + def insert_software(self, insert_data): + with self.driver.session() as session: + # On check si la node avec laquelle on se relie existe + results = session.run("match (a:Software) where ID(a) = %d return a" % int(insert_data.nodeId)) + if not results.data(): + return {"Error": "%d ID not found" % int(insert_data.nodeId)} + + # On check si le logiciel n'existe pas déjà + results = session.run("match (a:Software) where a.name = '%s' return a" % insert_data.newNodeName) + if results.data(): + return {"Error": "%s already exist" % insert_data.newNodeName} + + session.run("CREATE (n:Software {name: '%s'})" % insert_data.newNodeName) + + results = session.run( + "MATCH (a:Software), (b:Software) WHERE a.name = '%s' AND ID(b) = %d CREATE (a)-[r:%s]->(b) " + "RETURN type(r)" % (insert_data.newNodeName, int(insert_data.nodeId), insert_data.relationType)) + + return results.data() diff --git a/sg-backend/main.py b/sg-backend/main.py index 756d205f2bdcccce171bf83328864ce39a8f398c..54561d521db37dc07a43062e8dfe2aac9188cd34 100644 --- a/sg-backend/main.py +++ b/sg-backend/main.py @@ -14,6 +14,12 @@ class SoftwareSearch(BaseModel): software2: str +class SoftwareInsert(BaseModel): + newNodeName: str + relationType: str + nodeId: str + + driver = Neo4j(os.environ.get("NEO4J_URI"), os.environ.get("NEO4J_USER"), os.environ.get("NEO4J_PASSWORD")) app = FastAPI() @@ -70,7 +76,7 @@ async def software(): for idx, relation in enumerate(jsondata["edges"]): relation['key'] = str(idx) relation['attributes'] = {} - relation['attributes']['label'] = relation['relation_type'][1] + relation['label'] = relation['relation_type'][1] relation['source'] = findNodeIdWithName(jsondata["nodes"], relation["source"]) relation['target'] = findNodeIdWithName(jsondata["nodes"], relation["target"]) del relation['relation_type'] @@ -91,3 +97,9 @@ async def software_search(search: SoftwareSearch): print(search.software1, search.software2) res = driver.get_softwares_shortest_path(search) return {"softwares": res} + + +@app.post("/software-insert") +async def software_insert(insert_data: SoftwareInsert): + res = driver.insert_software(insert_data) + return res diff --git a/sg-backend/test.json b/sg-backend/test.json deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/sg-frontend/index.html b/sg-frontend/index.html index 987317889a8841ec2a28bb9b64624beed6eb4bbd..fb6e4814765868099fd669c66f3cfd9e98126985 100644 --- a/sg-frontend/index.html +++ b/sg-frontend/index.html @@ -12,6 +12,7 @@ <div class="topnav"> <a id="home" class="active" href="/">Home</a> <a id="sigma" href="#/sigma-custom">Software graph</a> + <a id="add" href="#/add-node">Software graph</a> </div> </div> <script type="module" src="/src/main.ts"></script> diff --git a/sg-frontend/src/App.svelte b/sg-frontend/src/App.svelte index 1dc59f05b6b743164a08e73ccdd8ed545739e2bd..1ffb7ff7af9939f4e675697c57b62525817d9805 100644 --- a/sg-frontend/src/App.svelte +++ b/sg-frontend/src/App.svelte @@ -6,6 +6,7 @@ import NotFound from "./routes/NotFound.svelte"; import Sigma from "./routes/Sigma.svelte"; import SigmaCustom from "./routes/SigmaCustom.svelte"; + import AddNode from "./routes/AddNode.svelte"; let routes = { "/": Index, @@ -13,6 +14,7 @@ "/graph": Graph, "/sigma": Sigma, "/sigma-custom": SigmaCustom, + "/add-node": AddNode, "*": NotFound } </script> diff --git a/sg-frontend/src/routes/AddNode.svelte b/sg-frontend/src/routes/AddNode.svelte new file mode 100644 index 0000000000000000000000000000000000000000..6720a65385cd9c915e9c269604308f10d70ae694 --- /dev/null +++ b/sg-frontend/src/routes/AddNode.svelte @@ -0,0 +1,21 @@ +<script lang="ts"> + + import {onMount} from 'svelte'; + + onMount(() => { + fetch("http://localhost:8000/add-node") + .then(response => response.json()) + .then(data => { + // Retrieve some useful DOM elements: + const link = document.getElementById("sigma") as HTMLElement; + const home = document.getElementById("home") as HTMLElement; + const add = document.getElementById("add") as HTMLElement; + add.classList.add("active"); + home.classList.remove("active"); + link.classList.remove("active"); + }) + }); +</script> +<div id="app-base"> +<h1>Add new node</h1> +</div> \ No newline at end of file diff --git a/sg-frontend/src/routes/Index.svelte b/sg-frontend/src/routes/Index.svelte index 0214683d4d4f03b5e63100b46c88f111485ec2f0..53f779416021abf9da45ec54eadf62c159743d11 100644 --- a/sg-frontend/src/routes/Index.svelte +++ b/sg-frontend/src/routes/Index.svelte @@ -2,8 +2,10 @@ import SoftwareMerger from "../components/SoftwareMerger.svelte"; const link = document.getElementById("sigma"); const home = document.getElementById("home"); + const add = document.getElementById("add"); home.classList.add("active"); link.classList.remove("active"); + add.classList.remove("active"); </script> <div id="app-base"> diff --git a/sg-frontend/src/routes/SigmaCustom.svelte b/sg-frontend/src/routes/SigmaCustom.svelte index 99d63de5293eaf784ea99d1e08937975fad61c67..430516090a0c42783bc1e0066f89681b0d399851 100644 --- a/sg-frontend/src/routes/SigmaCustom.svelte +++ b/sg-frontend/src/routes/SigmaCustom.svelte @@ -19,8 +19,10 @@ const container = document.getElementById("sigma-container") as HTMLElement; const link = document.getElementById("sigma") as HTMLElement; const home = document.getElementById("home") as HTMLElement; + const add = document.getElementById("add") as HTMLElement; link.classList.add("active"); home.classList.remove("active"); + add.classList.remove("active"); // Instantiate sigma: const graph = new Graph();