Skip to content
Extraits de code Groupes Projets
Valider 2bc81b1c rédigé par Maxime DESMARCHELIER's avatar Maxime DESMARCHELIER
Parcourir les fichiers

Add insert new node / new relation endpoint

Add Form on the add node page
parent 39c2f70e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -64,20 +64,24 @@ class Neo4j:
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)
return results.data()
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))
def relation_insert(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 (a.name = '%s' OR a.name = '%s') return a"
% (insert_data.software1, insert_data.software2))
if len(results.data()) != 2:
return {"Error": "%s/%s NOT FOUND" % (insert_data.software1, insert_data.software2)}
results = session.run(
"MATCH (a:Software), (b:Software) WHERE a.name = '%s' AND b.name = '%s' CREATE (a)-[r:%s]->(b) "
"RETURN type(r)" % (insert_data.software1, insert_data.software2, insert_data.relationType))
return results.data()
......@@ -16,8 +16,12 @@ class SoftwareSearch(BaseModel):
class SoftwareInsert(BaseModel):
newNodeName: str
class RelationInsert(BaseModel):
software1: str
software2: str
relationType: str
nodeId: str
driver = Neo4j(os.environ.get("NEO4J_URI"), os.environ.get("NEO4J_USER"), os.environ.get("NEO4J_PASSWORD"))
......@@ -103,3 +107,9 @@ async def software_search(search: SoftwareSearch):
async def software_insert(insert_data: SoftwareInsert):
res = driver.insert_software(insert_data)
return res
@app.post("/relation-insert")
async def software_insert(insert_data: RelationInsert):
res = driver.relation_insert(insert_data)
return res
<script>
import SoftwareSelect from "./SoftwareSelect.svelte";
import {onMount} from "svelte";
let softwares = [],
software1,
software2,
result,
node;
const BACKEND_URL = import.meta.env.VITE_BACKEND_URL;
onMount(async () => {
await fetch(BACKEND_URL + "/software")
.then((response) => response.json())
.then((data) => {
console.log(data);
let tempSoft = [];
data["softwares"].forEach((element) => {
node = Object.values(Object.values(element))[0];
delete Object.assign(node, {["label"]: node["name"]})["name"];
tempSoft.push(node);
});
softwares = tempSoft;
console.log(softwares);
})
.catch((error) => {
console.log(error);
return [];
});
});
</script>
<form>
<SoftwareSelect items="{softwares}" bind:selectedSoftware="{software1}"/>
<div>
<label class="radio-inline">
<input type="radio" id="SIMILAR" name="relationType" checked>SIMILAR
</label>
<label class="radio-inline">
<input type="radio" id="HAS_SAME_CREATOR" name="relationType">HAS SAME CREATOR
</label>
</div>
<SoftwareSelect items="{softwares}" bind:selectedSoftware="{software2}"/>
<button type="submit">Add</button>
</form>
<style>
/* Style the form - display items horizontally */
.form-inline {
display: flex;
flex-flow: row wrap;
align-items: center;
}
/* Add some margins for each label */
.form-inline label {
margin: 5px 10px 5px 0;
}
p {
display: none !important;
}
/* Style the input fields */
.form-inline input {
vertical-align: middle;
margin: 5px 10px 5px 0;
padding: 10px;
background-color: #fff;
border: 1px solid #ddd;
}
/* Add responsiveness - display the form controls vertically instead of horizontally on screens that are less than 800px wide */
@media (max-width: 800px) {
.form-inline input {
margin: 10px 0;
}
.form-inline {
flex-direction: column;
align-items: stretch;
}
}
form {
max-width: 400px;
background: #f4f4f4;
padding: 20px;
border-radius: 4px;
color: black;
margin: auto;
}
label {
margin: 0 0 10px;
}
</style>
<script>
import Select from "svelte-select";
</script>
<form class="form-inline">
<label for="softwareName">Software Name :</label>
<input type="text" id="softwareName" placeholder="" name="softwareName">
<button type="submit">Submit</button>
</form>
<style>
/* Style the form - display items horizontally */
.form-inline {
display: flex;
flex-flow: row wrap;
align-items: center;
}
/* Add some margins for each label */
.form-inline label {
margin: 5px 10px 5px 0;
}
/* Style the input fields */
.form-inline input {
vertical-align: middle;
margin: 5px 10px 5px 0;
padding: 10px;
background-color: #fff;
border: 1px solid #ddd;
}
/* Add responsiveness - display the form controls vertically instead of horizontally on screens that are less than 800px wide */
@media (max-width: 800px) {
.form-inline input {
margin: 10px 0;
}
.form-inline {
flex-direction: column;
align-items: stretch;
}
}
form {
max-width: 400px;
background: #f4f4f4;
padding: 20px;
border-radius: 4px;
color: black;
margin: auto;
}
label {
margin: 0 0 10px;
}
</style>
<script lang="ts">
import {onMount} from 'svelte';
import SotfwareInsert from "../components/SoftwareInsert.svelte";
import RelationInsert from "../components/RelationInsert.svelte";
onMount(() => {
fetch("http://localhost:8000/add-node")
......@@ -18,4 +20,7 @@
</script>
<div id="app-base">
<h1>Add new node</h1>
<SotfwareInsert></SotfwareInsert>
<h1>Add new relation</h1>
<RelationInsert></RelationInsert>
</div>
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter