Skip to content

Instantly share code, notes, and snippets.

@cskardon
Created February 26, 2014 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cskardon/9232034 to your computer and use it in GitHub Desktop.
Save cskardon/9232034 to your computer and use it in GitHub Desktop.
using System;
using Neo4jClient;
using Neo4jClient.Cypher;
internal class Question_22041696
{
private class CityEntity { public int Id { get; set; } }
private class CompanyEntity { public int Id { get; set; } }
private class ProfessionEntity { public int Id { get; set; } }
private class WorkEntity
{
public string Id { get; set; }
public string Description { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
}
public Question_22041696()
{
var client = new GraphClient(new Uri("http://localhost:7474/db/data/"));
client.Connect();
Client = client;
}
private IGraphClient Client { get; set; }
public void Run()
{
Setup();
ICypherFluentQuery query = CreateQuery();
Console.WriteLine("Press ENTER to execute change query");
Console.ReadLine();
query.ExecuteWithoutResults();
}
private void Setup()
{
var workEntity = new WorkEntity {Id = "id-1", Description = "fishsticks", EndDate = DateTime.MinValue.ToString(), StartDate = DateTime.MinValue.ToString()};
var city = new {Id = 1, Name = "Foo"};
var oldCity = new {Id = 5, Name = "Bar"};
var profession = new {Id = 2, Name = "Profoo"};
var oldProfession = new {Id = 6, Name = "Probar"};
var company = new {Id = 3, Name = "Compfoo"};
var oldCompany = new {Id = 7, Name = "Compbar"};
var user = new {Id = "user/2", Name = "Jeff"};
Client.Cypher.Create("(u:User {param})").WithParam("param", user).ExecuteWithoutResults();
Client.Cypher.Create("(w:Work {param})").WithParam("param", workEntity).ExecuteWithoutResults();
Client.Cypher.Create("(c:City {param})").WithParam("param", city).ExecuteWithoutResults();
Client.Cypher.Create("(c:City {param})").WithParam("param", oldCity).ExecuteWithoutResults();
Client.Cypher.Create("(p:Profession {param})").WithParam("param", profession).ExecuteWithoutResults();
Client.Cypher.Create("(p:Profession {param})").WithParam("param", oldProfession).ExecuteWithoutResults();
Client.Cypher.Create("(c:Company {param})").WithParam("param", company).ExecuteWithoutResults();
Client.Cypher.Create("(c:Company {param})").WithParam("param", oldCompany).ExecuteWithoutResults();
Client.Cypher.Match("(user:User {Id:'user/2'}), (work:Work {Id:'id-1'})").CreateUnique("(user)-[:CURRENT]->(work)").ExecuteWithoutResults();
Client.Cypher.Match("(work:Work {Id:'id-1'}), (p:Profession {Id:6})").CreateUnique("(work)-[:WORK_AS_PROFESSION]->(p)").ExecuteWithoutResults();
Client.Cypher.Match("(work:Work {Id:'id-1'}), (c:Company {Id:7})").CreateUnique("(work)-[:WORK_AT_COMPANY]->(c)").ExecuteWithoutResults();
Client.Cypher.Match("(work:Work {Id:'id-1'}), (c:City {Id:5})").CreateUnique("(work)-[:WORK_IN_CITY]->(c)").ExecuteWithoutResults();
}
private ICypherFluentQuery CreateQuery()
{
var model = new {Id = "id-1", CityId = 1, ProfessionId = 2, CompanyId = 3, UserId = 4, EndDate = DateTime.MinValue};
var oldModel = new {Id = "id-2", CityId = 5, ProfessionId = 6, CompanyId = 7, UserId = 8, EndDate = DateTime.MinValue};
var updatedEntity = new WorkEntity
{
Description = null,
EndDate = DateTime.Now.AddDays(4).ToString(),
StartDate = DateTime.Now.ToString(),
Id = "id-1"
};
ICypherFluentQuery query = Client.Cypher
.Match("(work:Work)", "(city:City)", "(profession:Profession)", "(company:Company)", "(oldCompany:Company)", "(oldProfession:Profession)", "(oldCity:City)",
"(user:User)-[r1]->work", "work-[r2]->oldProfession", "work-[r3]->oldCompany", "work-[r4]->oldCity")
.Where((WorkEntity work) => work.Id == model.Id)
.AndWhere((CityEntity city) => city.Id == model.CityId)
.AndWhere((CityEntity oldCity) => oldCity.Id == oldModel.CityId)
.AndWhere((ProfessionEntity profession) => profession.Id == model.ProfessionId)
.AndWhere((ProfessionEntity oldProfession) => oldProfession.Id == oldModel.ProfessionId)
.AndWhere((CompanyEntity company) => company.Id == model.CompanyId)
.AndWhere((CompanyEntity oldCompany) => oldCompany.Id == oldModel.CompanyId)
.AndWhere("type(r1) = 'CURRENT'")
.AndWhere("type(r2) = 'WORK_AS_PROFESSION'")
.AndWhere("type(r3) = 'WORK_AT_COMPANY'")
.AndWhere("type(r4) = 'WORK_IN_CITY'")
.Set("work = {updatedWork}")
.WithParam("updatedWork", updatedEntity);
//If Date has been set delete current relationships
if (oldModel.EndDate == DateTime.MinValue && model.EndDate > DateTime.MinValue)
{
query = query.Delete("r1");
}
if (oldModel.ProfessionId != model.ProfessionId)
{
query = query.Delete("r2").CreateUnique("work-[:WORK_AS_PROFESSION]->profession");
}
if (oldModel.CompanyId != model.CompanyId)
{
query = query.Delete("r3").CreateUnique("work-[:WORK_AT_COMPANY]->company");
}
if (oldModel.CityId != model.CityId)
{
query = query.Delete("r4").CreateUnique("work-[:WORK_IN_CITY]->city");
}
return query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment