From a5700b88c8daac0f2e5caed5ee6cd3ada7261be7 Mon Sep 17 00:00:00 2001 From: Paul Horn Date: Wed, 12 Feb 2025 13:40:18 +0100 Subject: [PATCH] Update movie example to latest versions of neo4rs and axum --- Cargo.toml | 22 +++++++++++----------- src/main.rs | 37 ++++++++++++++----------------------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index acf8025..fa7bb35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,14 +5,14 @@ edition = "2021" publish = false [dependencies] -axum = "0.7.4" -color-eyre = "0.6.2" -futures = "0.3.30" -neo4rs = "0.7.1" -serde = { version = "1.0.195", features = ["derive"] } -tokio = { version = "1.35.1", features = ["full"] } -tower = { version = "0.4.13", features = ["util"] } -tower-http = { version = "0.5.1", features = ["fs", "trace"] } -tracing = "0.1.40" -tracing-error = "0.2.0" -tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +axum = "0.8.1" +color-eyre = "0.6.3" +futures = "0.3.31" +neo4rs = { version = "0.9.0-rc.4", features = ["unstable-v1"] } +serde = { version = "1.0.217", features = ["derive"] } +tokio = { version = "1.43.0", features = ["full"] } +tower = { version = "0.5.2", features = ["util"] } +tower-http = { version = "0.6.2", features = ["fs", "trace"] } +tracing = "0.1.41" +tracing-error = "0.2.1" +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } diff --git a/src/main.rs b/src/main.rs index 7f24b24..2df2300 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,15 +26,15 @@ async fn main() -> Result<()> { .with(ErrorLayer::default()) .init(); - let db = db().await?; + let db = db()?; let service = Service { db }; let assets_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/assets"); let app = Router::new() .route("/", get(|| async { Redirect::temporary("/index.html") })) - .route("/movie/:title", get(movie)) - .route("/movie/vote/:title", post(vote)) + .route("/movie/{title}", get(movie)) + .route("/movie/vote/{title}", post(vote)) .route("/search", get(search)) .route("/graph", get(graph)) .fallback_service(ServeDir::new(assets_dir)) @@ -55,7 +55,7 @@ async fn main() -> Result<()> { Ok(()) } -async fn db() -> Result { +fn db() -> Result { const DEFAULT_URL: &str = "neo4j+s://demo.neo4jlabs.com"; const DEFAULT_DATABASE: &str = "movies"; const DEFAULT_USER: &str = "movies"; @@ -90,7 +90,7 @@ async fn db() -> Result { .unwrap_or(DEFAULT_DATABASE)) .build()?; - Ok(Graph::connect(config).await?) + Ok(Graph::connect(config)?) } async fn movie( @@ -146,19 +146,10 @@ impl Service { .execute(neo4rs::query(FIND_MOVIE).param("title", title)) .await?; - // TODO: next_as::()? - let movie = rows - .next() - .await? - .map(|r| r.to::()) - .transpose()? - .unwrap_or_default(); + let movie = rows.single_as::().await?; + let summary = rows.finish().await?; - // TODO: make this possible - // TODO: let summary = rows.finish().await?; - // TODO: debug!(?summary); - - debug!(?movie); + debug!(?summary, ?movie); Ok(movie) } @@ -170,14 +161,14 @@ impl Service { SET movie.votes = coalesce(movie.votes, 0) + 1 RETURN movie.votes"; - self.db + let summary = self + .db .run(neo4rs::query(VOTE_IN_MOVIE).param("title", title)) .await?; - // TODO: - // let summary = self.db.run(...).await?; + let updates = summary.stats().properties_set; - Ok(Voted { updates: 1 }) + Ok(Voted { updates }) } #[instrument(skip(self))] @@ -187,7 +178,7 @@ impl Service { WHERE toLower(movie.title) CONTAINS toLower($part) RETURN movie"; - let rows = self + let mut rows = self .db .execute(neo4rs::query(SEARCH_MOVIES).param("part", search.q)) .await?; @@ -284,7 +275,7 @@ struct Person { #[derive(Debug, Clone, Serialize, Deserialize)] struct Voted { - updates: usize, + updates: u64, } #[derive(Debug, Clone, Serialize, Deserialize)]