Beispiel #1
0
        public async Task Setup()
        {
            client = new GraphClient(new Uri("http://localhost:7474"));
            await client.ConnectAsync();

            await client.Cypher.Match("(m:Movie)").DetachDelete("m").ExecuteWithoutResultsAsync();

            await client.Cypher.Match("(m:Person)").DetachDelete("m").ExecuteWithoutResultsAsync();

            await client.Cypher.Match("(m:Greeting)").DetachDelete("m").ExecuteWithoutResultsAsync();
        }
Beispiel #2
0
            public async Task <BasicResponseDto> Handle(Command request, CancellationToken cancellationToken)
            {
                await _client.ConnectAsync();

                await _client.ConnectAsync();

                var stops = await _client.Cypher.Match($"(stop: Stop) WHERE {request.LineNo} in stop.lines")
                            .Return(stop => stop.As <Stop>())
                            .ResultsAsync;

                if (!stops.Any())
                {
                    return(new ListResponseDto <int>(false, $"Line with number: {request.LineNo} does not exist."));
                }

                // Delete old line

                foreach (var stopToDelete in stops)
                {
                    if (stopToDelete.lines.Count() == 1)
                    {
                        await _client.Cypher.Match($"(stop: Stop {{ name: '{stopToDelete.name}' }})").DetachDelete("stop")
                        .ExecuteWithoutResultsAsync();
                    }
                    else
                    {
                        var stopLines = stopToDelete.lines.ToList();
                        stopLines.RemoveAll(x => x == request.LineNo);
                        var array = $"[{string.Join(",", stopLines.Distinct().ToArray())}]";
                        var query = $"stop.lines = {array}";
                        await _client.Cypher.Match($"(stop: Stop {{ name: '{stopToDelete.name}' }})").Set(query)
                        .ExecuteWithoutResultsAsync();
                    }
                }

                return(new BasicResponseDto(true));
            }
Beispiel #3
0
            public async Task <ListResponseDto <Stop> > Handle(Query request, CancellationToken cancellationToken)
            {
                await _client.ConnectAsync();

                var query  = $"(start: Stop {{ name: '{request.StartStop}' }}),(dest: Stop {{ name: '{request.DestStop}' }})";
                var result = await _client.Cypher.Match(query).Return <List <Stop> >("shortestPath((start) -[*]- (dest))").ResultsAsync;

                if (!result.Any())
                {
                    return(new ListResponseDto <Stop>(true, $"Cannot find connection between {request.StartStop} and {request.DestStop}."));
                }

                var stops = result.First().Where(s => s.name != null).ToList();

                return(new ListResponseDto <Stop>(stops));
            }
Beispiel #4
0
            public async Task <ListResponseDto <int> > Handle(Query request, CancellationToken cancellationToken)
            {
                await _client.ConnectAsync();

                var result = await _client.Cypher.Match("(stop: Stop)")
                             .Return(stop => stop.As <Stop>())
                             .ResultsAsync;

                if (!result.Any())
                {
                    return(new ListResponseDto <int>(true));
                }

                var lines = result.SelectMany(x => x.lines).Distinct().ToList();

                return(new ListResponseDto <int>(lines));
            }
Beispiel #5
0
            public async Task <ListResponseDto <LineDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                await _client.ConnectAsync();

                var query = $"(line: Line) WHERE line.lineNumber = {request.Number}";
                var lines = await _client.Cypher.Match(query)
                            .Return(line => line.As <Line>())
                            .ResultsAsync;

                if (!lines.Any())
                {
                    return(new ListResponseDto <LineDto>(true, "Line not found."));
                }

                var lineDto = new LineDto()
                {
                    lineNumber = lines.First().lineNumber,
                    stops      = lines.First().stops
                };

                return(new ListResponseDto <LineDto>(lineDto));
            }
 public GraphDbContext(IGraphDbSettings settings)
 {
     _client = new GraphClient(new Uri(settings.ConnectionString), settings.User, settings.Password);
     _client.ConnectAsync().Wait();
 }
Beispiel #7
0
            public async Task <BasicResponseDto> Handle(Command request, CancellationToken cancellationToken)
            {
                await _client.ConnectAsync();

                await _client.ConnectAsync();

                var line = await _client.Cypher.Match($"(stop: Stop) WHERE {request.lineNumber} in stop.lines")
                           .Return(stop => stop.As <Stop>())
                           .ResultsAsync;

                if (line.Any())
                {
                    return(new ListResponseDto <int>(false, $"Line with number: {request.lineNumber} arleady exists."));
                }

                // Create stops
                foreach (var stopName in request.stops)
                {
                    var entities = await _client.Cypher.Match($"(stop: Stop) WHERE stop.name = '{stopName}'")
                                   .Return(stop => stop.As <Stop>())
                                   .ResultsAsync;

                    var entity = entities.FirstOrDefault();

                    if (entity == null)
                    {
                        var stopQuery = $"(stop: Stop {{ name: '{stopName}', lines: [{request.lineNumber}]}})";
                        await _client.Cypher.Create(stopQuery)
                        .ExecuteWithoutResultsAsync();
                    }
                    else
                    {
                        var stopLines = entity.lines.ToList();
                        stopLines.Add(request.lineNumber);
                        var array = $"[{string.Join(",", stopLines.Distinct().ToArray())}]";
                        var query = $"stop.lines = {array}";
                        await _client.Cypher.Match($"(stop: Stop {{ name: '{stopName}' }})").Set(query)
                        .ExecuteWithoutResultsAsync();
                    }
                }

                // Create relations
                var stopsList = request.stops.ToList();

                for (int i = 0; i < stopsList.Count - 1; i++)
                {
                    var start = stopsList.ElementAt(i);
                    var dest  = stopsList.ElementAt(i + 1);

                    var relationQuery = $"(a)-[r:LEADS_TO]->(b)";
                    await _client.Cypher.Match($"(a:Stop),(b:Stop) WHERE a.name = '{start}' AND b.name = '{dest}'")
                    .Merge(relationQuery)
                    .ExecuteWithoutResultsAsync();

                    await _client.Cypher.Match($"(a:Stop),(b:Stop) WHERE a.name = '{dest}' AND b.name = '{start}'")
                    .Merge(relationQuery)
                    .ExecuteWithoutResultsAsync();
                }

                // Create line
                var arrayOfStops = String.Join(",", request.stops.Select(x => string.Format("\"{0}\"", x)));
                var lineQuery    = $"(line: Line {{ lineNumber: {request.lineNumber}, stops: [{arrayOfStops}]}})";
                await _client.Cypher.Create(lineQuery)
                .ExecuteWithoutResultsAsync();

                return(new BasicResponseDto(true));
            }