public async Task <IHttpActionResult> GetAreaInfo(string id) { var g = new GremlinHelper(); string name; var q = $"g.V().has('area', 'id', '{id}')"; var info = await g.getResultAsync(q); var n = info["properties"].Where(x => x.Path == "properties.name").FirstOrDefault(); if (n != null) { name = n.Values()["value"].First().ToString(); } q = $"g.V().has('area', 'id', '{id}').inE().outV()"; var infoes = await g.getResultsAsync(q); foreach (var i in info) { } return(Ok()); }
public async Task <IHttpActionResult> PostNewProperty(string id, keyValue info) { var g = new GremlinHelper(); string qry = $"g.V('{id}').property('{info.key.Replace("'", "''")}','{info.value.Replace("'", "''")}')"; await g.getResultAsync(qry); return(Ok()); }
public async Task <IHttpActionResult> PostNewSpouse(string id, newSpouseInfo info) { var g = new GremlinHelper(); if (info == null || (string.IsNullOrWhiteSpace(info.name) && string.IsNullOrWhiteSpace(info.id))) { return(BadRequest()); } var spID = ""; if (string.IsNullOrWhiteSpace(info.id)) { // do the insert var qry = $"g.addV('person').property('name', '{info.name.Replace("'","''")}')"; if (!string.IsNullOrWhiteSpace(info.gender)) { qry += $".property('gender', '{info.gender}')"; } var nc = await g.getResultAsync(qry); // get iD from result spID = (nc as JObject)["id"].ToString(); } else { spID = info.id; } // relate to parent await g.getResultAsync($"g.V('{id}').addE('married').to(g.V('{spID}'))"); if (info.children) { } return(Ok()); }
public async Task <IHttpActionResult> PostNewArea(string id, newAreaInfo info) { var g = new GremlinHelper(); string areaId, q; if (string.IsNullOrWhiteSpace(info.country)) { q = $"g.V().has('area', 'name', '{info.area}')"; var area = await g.getResultAsync(q); if (area == null) { q = $"g.addV('area').property('name', '{info.area}')"; areaId = (await g.getResultAsync(q))["id"].ToString(); } else { areaId = area["id"].ToString(); } } else { var res = await g.createVertexEdgeVertextByName("area", info.area, "isIn", "country", info.country); areaId = res.outId; } q = $"g.V'{id}').outE('lived').inV().has('area', 'id' '{areaId}')"; var isAlready = await g.getResultAsync(q); if (isAlready == null) { q = $"g.V('{id}').addE('lived').to(g.V('{areaId}'))"; await g.getResultAsync(q); } return(Ok()); }
public async Task <IHttpActionResult> PostAssignChild(string id, newChildInfo info) { var g = new GremlinHelper(); if (info == null || string.IsNullOrWhiteSpace(info.id) || (string.IsNullOrWhiteSpace(info.motherId) && string.IsNullOrWhiteSpace(info.fatherId))) { return(BadRequest()); } var ncID = info.id; // relate to parent await g.getResultAsync($"g.V('{id}').addE('parent').to(g.V('{ncID}'))"); // now other parent if (!string.IsNullOrWhiteSpace(info.motherId) && info.motherId != id) { await g.getResultAsync($"g.V('{info.motherId}').addE('parent').to(g.V('{ncID}'))"); } else if (!string.IsNullOrWhiteSpace(info.fatherId) && info.fatherId != id) { await g.getResultAsync($"g.V('{info.fatherId}').addE('parent').to(g.V('{ncID}'))"); } // add parent Edge type properties if (!string.IsNullOrWhiteSpace(info.fatherId)) { await g.getResultAsync($"g.V('{info.fatherId}').outE('parent').property('type', 'Father')"); } if (!string.IsNullOrWhiteSpace(info.motherId)) { await g.getResultAsync($"g.V('{info.motherId}').outE('parent').property('type', 'Mother')"); } // save age on Edge if (!string.IsNullOrWhiteSpace(info.fatherAge)) { await g.getResultAsync($"g.V('{info.fatherId}').outE('parent').inV('{ncID}').property('age', '{info.fatherAge}')"); } if (!string.IsNullOrWhiteSpace(info.motherAge)) { await g.getResultAsync($"g.V('{info.motherId}').outE('parent').inV('{ncID}').property('age', '{info.motherAge}')"); } return(Ok()); }
public async Task <IHttpActionResult> PostNewChild(string id, newChildInfo info) { var g = new GremlinHelper(); if (info == null) { return(BadRequest()); } string ncID; if (!string.IsNullOrWhiteSpace(info.name) && info.id == null) { // do the insert var qry = $"g.addV('person').property('name', '{info.name}')"; if (!string.IsNullOrWhiteSpace(info.gender)) { qry += $".property('gender', '{info.gender}')"; } var nc = await g.getResultAsync(qry); // get iD frmo result ncID = (nc as JObject)["id"].ToString(); } else { ncID = info.id; } // relate to parent await g.getResultAsync($"g.V('{id}').addE('parent').to(g.V('{ncID}'))"); // now other parent if (!string.IsNullOrWhiteSpace(info.motherId) && info.motherId != id) { await g.getResultAsync($"g.V('{info.motherId}').addE('parent').to(g.V('{ncID}'))"); } else if (!string.IsNullOrWhiteSpace(info.fatherId) && info.fatherId != id) { await g.getResultAsync($"g.V('{info.fatherId}').addE('parent').to(g.V('{ncID}'))"); } // add parent Edge type properties if (!string.IsNullOrWhiteSpace(info.fatherId)) { await g.getResultAsync($"g.V('{info.fatherId}').outE('parent').property('type', 'Father')"); } if (!string.IsNullOrWhiteSpace(info.motherId)) { await g.getResultAsync($"g.V('{info.motherId}').outE('parent').property('type', 'Mother')"); } // save age on Edge if (!string.IsNullOrWhiteSpace(info.fatherAge)) { await g.getResultAsync($"g.V('{ncID}').inE('parent').has('type', 'Father').property('age', '{info.fatherAge}')"); } if (!string.IsNullOrWhiteSpace(info.motherAge)) { await g.getResultAsync($"g.V('{ncID}').inE('parent').has('type', 'Mother').property('age', '{info.motherAge}')"); } return(Ok()); }
public async Task <person> getById(string id) { var grem = new GremlinHelper(); var qry = $"g.V('{id}')"; var info = await grem.getResultAsync(qry); var person = new person() { id = id, }; var n = info["properties"].Where(x => x.Path == "properties.name").FirstOrDefault(); if (n != null) { person.name = n.Values()["value"].First().ToString(); } n = info["properties"].Where(x => x.Path == "properties.gender").FirstOrDefault(); if (n != null) { person.gender = n.Values()["value"].First().ToString(); } person.properties = getProperties(info); List <Task> tasks = new List <Task>(); tasks.Add(Task.Run(async() => { // my spouse qry = $"g.V('{id}').as('o').bothE('married').bothV().where(neq('o'))"; var infoes = await grem.getResultsAsync(qry); foreach (var i in infoes) { var p = new person() { name = (i as JToken)["properties"].Where(x => x.Path == "properties.name").First().Values().First()["value"].ToString(), id = (i as JObject)["id"].ToString() }; n = (i as JToken)["properties"].Where(x => x.Path == "properties.gender").FirstOrDefault(); if (n != null) { p.gender = n.Values()["value"].First().ToString(); p.type = person.gender == "F" ? "Husband" : "Wife"; } person.spouse.Add(p); } })); tasks.Add(Task.Run(async() => { // where I lived qry = $"g.V('{id}').outE('lived').inV()"; var infoes = await grem.getResultsAsync(qry); foreach (var i in infoes) { var vId = (i as JObject)["id"].ToString(); var name = (i as JToken)["properties"].Where(x => x.Path == "properties.name").First().Values().First()["value"].ToString(); person.livedIn.Add(new keyValue(vId, name)); } })); tasks.Add(Task.Run(async() => { // my children qry = $"g.V('{id}').outE('parent').inV()"; var infoes = await grem.getResultsAsync(qry); var edges = await grem.getResultsAsync($"g.V('{id}').outE('parent')"); foreach (var i in infoes) { var c = new person() { name = (i as JToken)["properties"].Where(x => x.Path == "properties.name").First().Values().First()["value"].ToString(), id = (i as JObject)["id"].ToString() }; var g = (i as JToken)["properties"].Where(x => x.Path == "properties.gender").FirstOrDefault(); if (g != null) { c.gender = g.Values()["value"].First().ToString(); c.type = c.gender == "F" ? "Daughter" : "Son"; } var child = edges.Where(x => (x as JObject)["inV"].ToString() == c.id).FirstOrDefault(); if (child != null) { var prop = (child as JObject).Children().FirstOrDefault(z => z.Path == "properties"); if (prop != null) { foreach (var t in prop.Values().Where(z => z.Path != "properties.type").ToList()) { c.properties.Add(new keyValue((t as JProperty).Name, t.First().ToString())); } } } person.children.Add(c); } })); tasks.Add(Task.Run(async() => { // my parents qry = $"g.V('{id}').inE('parent').outV()"; var infoes = await grem.getResultsAsync(qry); var edges = await grem.getResultsAsync($"g.V('{id}').inE('parent')"); foreach (var i in infoes) { var p = new person() { name = (i as JToken)["properties"].Where(x => x.Path == "properties.name").First().Values().First()["value"].ToString(), id = (i as JObject)["id"].ToString() }; var par = edges.Where(x => (x as JObject)["outV"].ToString() == p.id).FirstOrDefault(); if (par != null) { var prop = (par as JObject).Children().FirstOrDefault(z => z.Path == "properties"); if (prop != null) { var t = prop.Values().FirstOrDefault(z => z.Path == "properties.type"); if (t != null) { p.type = t.First().ToString(); } } } person.parents.Add(p); } })); tasks.Add(Task.Run(async() => { // my siblings //qry = $"g.V('{id}').as('o').bothE('sibling').bothV().where(neq('o'))"; qry = $"g.V('{id}').as('o').inE('parent').outV().outE('parent').inV().where(neq('o')).dedup()"; var infoes = await grem.getResultsAsync(qry); foreach (var i in infoes) { var sibId = (i as JObject)["id"].ToString(); if (sibId != id && !person.siblings.Any(x => x.id == id)) { var s = new person() { name = (i as JToken)["properties"].Where(x => x.Path == "properties.name").First().Values().First()["value"].ToString(), id = sibId }; var g = (i as JToken)["properties"].Where(x => x.Path == "properties.gender").FirstOrDefault(); if (g != null) { if (g.Values()["value"].First().ToString() == "M") { s.type = "Brother"; } else { s.type = "Sister"; } } person.siblings.Add(s); } } })); Task.WaitAll(tasks.ToArray()); return(person); }
public async Task Bootstrap() { string adam; string eve; string cain; string seth; string abel; string enosh; string kenan; var g = new GremlinHelper(); List <string> initialQueries = new List <string> { { "g.V().drop()" }, { "g.addV('person').property('name', 'Adam').property('gender', 'M')" }, { "g.addV('person').property('name', 'Eve').property('gender', 'F')" }, { "g.addV('person').property('name', 'Cain').property('gender', 'M')" }, { "g.addV('person').property('name', 'Abel').property('gender', 'M')" }, { "g.addV('person').property('name', 'Seth').property('gender', 'M')" }, { "g.addV('person').property('name', 'Enosh').property('gender', 'M')" }, { "g.addV('person').property('name', 'Kenan').property('gender', 'M')" } }; foreach (var q in initialQueries) { await g.getResultAsync(q); } adam = (await g.getIdsByNameAsync("Adam"))[0]; eve = (await g.getIdsByNameAsync("Eve"))[0]; cain = (await g.getIdsByNameAsync("Cain"))[0]; seth = (await g.getIdsByNameAsync("Seth"))[0]; abel = (await g.getIdsByNameAsync("Abel"))[0]; enosh = (await g.getIdsByNameAsync("Enosh"))[0]; kenan = (await g.getIdsByNameAsync("Kenan"))[0]; await g.getResultAsync($"g.V('{adam}').addE('married').to(g.V('{eve}'))"); await g.getResultAsync($"g.V('{eve}').addE('parent').to(g.V('{cain}'))"); await g.getResultAsync($"g.V('{eve}').addE('parent').to(g.V('{abel}'))"); await g.getResultAsync($"g.V('{eve}').addE('parent').to(g.V('{seth}'))"); await g.getResultAsync($"g.V('{adam}').addE('parent').to(g.V('{cain}'))"); await g.getResultAsync($"g.V('{adam}').addE('parent').to(g.V('{abel}'))"); await g.getResultAsync($"g.V('{adam}').addE('parent').to(g.V('{seth}'))"); // only 1 child, so can update entire path for seth await g.getResultAsync($"g.V('{seth}').addE('parent').to(g.V('{enosh}'))"); await g.getResultAsync($"g.V('{seth}').outE('parent').property('type', 'Father').property('age', 105)"); await g.getResultAsync($"g.V('{enosh}').addE('parent').to(g.V('{kenan}'))"); await g.getResultAsync($"g.V('{kenan}').inE('parent').has('type', 'Father').property('age', 90)"); // where as multiple children, and we want to update only Seth -> parent.fathe await g.getResultAsync($"g.V('{seth}').inE('parent').has('type', 'Father').property('age', 130)"); // Adam -> parent -> Seth path // await g.getResultAsync($"g.V('{adam}').outE('parent').inV().has('person', 'name', 'Seth').as('s').inE().has('type', 'Father').property('age', 130)"); await g.getResultAsync($"g.V('{eve}').outE('parent').property('type', 'Mother')"); await g.getResultAsync($"g.V('{adam}').outE('parent').property('type', 'Father')"); }