Exemple #1
0
        public IHttpActionResult GetMovieByTitle(string title)
        {
            //query = ("MATCH (movie:Movie {title:{title}}) "
            // "OPTIONAL MATCH (movie)<-[r]-(person:Person) "
            // "RETURN movie.title as title,"
            // "collect([person.name, "
            // "         head(split(lower(type(r)), '_')), r.roles]) as cast "
            // "LIMIT 1")

            var data = WebApiConfig.GraphClient.Cypher
                       .Match("(movie:Movie {title:{title}})")
                       .OptionalMatch("(movie)<-[r]-(person:Person)")
                       .WithParam("title", title)
                       .Return((movie, a) => new
            {
                movie = movie.As <Movie>().title,
                cast  = Return.As <IEnumerable <string> >("collect([person.name, head(split(lower(type(r)), '_')), r.roles])")
            })
                       .Limit(1)
                       .Results.FirstOrDefault();

            var result = new MovieResult();

            result.title = data.movie;

            var castresults = new List <CastResult>();

            foreach (var item in data.cast)
            {
                var tempData   = JsonConvert.DeserializeObject <dynamic>(item);
                var roles      = tempData[2] as JArray;
                var castResult = new CastResult
                {
                    name = tempData[0],
                    job  = tempData[1],
                };
                if (roles != null)
                {
                    castResult.role = roles.Select(c => c.Value <string>());
                }
                castresults.Add(castResult);
            }
            result.cast = castresults;

            return(Ok(result));
        }
        public IHttpActionResult GetMovieByTitle(string title)
        {
            //query = ("MATCH (movie:Movie {title:{title}}) "
            // "OPTIONAL MATCH (movie)<-[r]-(person:Person) "
            // "RETURN movie.title as title,"
            // "collect([person.name, "
            // "         head(split(lower(type(r)), '_')), r.roles]) as cast "
            // "LIMIT 1")

            var data = WebApiConfig.GraphClient.Cypher
               .Match("(movie:Movie {title:{title}})")
               .OptionalMatch("(movie)<-[r]-(person:Person)")
               .WithParam("title", title)
               .Return((movie, a) => new
               {
                   movie = movie.As<Movie>().title,
                   cast = Return.As<IEnumerable<string>>("collect([person.name, head(split(lower(type(r)), '_')), r.roles])")
               })
               .Limit(1)
               .Results.FirstOrDefault();

            var result = new MovieResult();
            result.title = data.movie;

            var castresults = new List<CastResult>();
            foreach (var item in data.cast)
            {
                var tempData = JsonConvert.DeserializeObject<dynamic>(item);
                var roles = tempData[2] as JArray;
                var castResult = new CastResult
                {
                    name = tempData[0],
                    job = tempData[1],
                };
                if (roles != null)
                {
                    castResult.role = roles.Select(c => c.Value<string>());
                }
                castresults.Add(castResult);
            }
            result.cast = castresults;

            return Ok(result);
        }
        public IHttpActionResult GetMovieByTitle(string title)
        {
            //query = ("MATCH (movie:Movie {title:{title}}) "
            // "OPTIONAL MATCH (movie)<-[r]-(person:Person) "
            // "RETURN movie.title as title,"
            // "collect([person.name, "
            // "         head(split(lower(type(r)), '_')), r.roles]) as cast "
            // "LIMIT 1")

            var statementTemplate   = "MATCH (movie:Movie {title:{title}}) OPTIONAL MATCH (movie)<-[r]-(person:Person) RETURN movie.title as title, collect([person.name, head(split(lower(type(r)), '_')), r.roles]) as cast LIMIT 1";
            var statementParameters = new Dictionary <string, object> {
                { "title", title }
            };

            var result = new MovieResult();

            using (var session = WebApiConfig.Neo4jDriver.Session())
            {
                var statementResult = session.Run(statementTemplate, statementParameters);

                var record = statementResult.Single();

                result.title = record["title"].As <string>();

                var           castResults = new List <CastResult>();
                List <object> cast        = record["cast"].As <List <object> >();
                foreach (IList <object> castMember in cast)
                {
                    var castResult = new CastResult
                    {
                        name = castMember[0].As <string>(),
                        job  = castMember[1].As <string>(),
                        role = castMember[2]?.As <List <string> >()
                    };
                    castResults.Add(castResult);
                }
                result.cast = castResults;
            }

            return(Ok(result));
        }
        public IHttpActionResult GetMovieByTitle(string title)
        {
            //query = ("MATCH (movie:Movie {title:{title}}) "
            // "OPTIONAL MATCH (movie)<-[r]-(person:Person) "
            // "RETURN movie.title as title,"
            // "collect([person.name, "
            // "         head(split(lower(type(r)), '_')), r.roles]) as cast "
            // "LIMIT 1")

            var statementTemplate = "MATCH (movie:Movie {title:{title}}) OPTIONAL MATCH (movie)<-[r]-(person:Person) RETURN movie.title as title, collect([person.name, head(split(lower(type(r)), '_')), r.roles]) as cast LIMIT 1";
            var statementParameters = new Dictionary<string, object> {{"title", title}};

            var result = new MovieResult();
            using (var session = WebApiConfig.Neo4jDriver.Session())
            {
                var statementResult = session.Run(statementTemplate, statementParameters);

                var record = statementResult.Single();

                result.title = record["title"].As<string>();

                var castResults = new List<CastResult>();
                List<object> cast = record["cast"].As<List<object>>();
                foreach (IList<object> castMember in cast)
                {
                    var castResult = new CastResult
                    {
                        name = castMember[0].As<string>(),
                        job = castMember[1].As<string>(),
                        role = castMember[2]?.As<List<string>>()
                    };
                    castResults.Add(castResult);
                }
                result.cast = castResults;
            }

            return Ok(result);
        }