Skip to content

Commit

Permalink
Update movie example to latest versions of neo4rs and axum
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Feb 12, 2025
1 parent ede6bb7 commit a5700b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
37 changes: 14 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -55,7 +55,7 @@ async fn main() -> Result<()> {
Ok(())
}

async fn db() -> Result<Graph> {
fn db() -> Result<Graph> {
const DEFAULT_URL: &str = "neo4j+s://demo.neo4jlabs.com";
const DEFAULT_DATABASE: &str = "movies";
const DEFAULT_USER: &str = "movies";
Expand Down Expand Up @@ -90,7 +90,7 @@ async fn db() -> Result<Graph> {
.unwrap_or(DEFAULT_DATABASE))
.build()?;

Ok(Graph::connect(config).await?)
Ok(Graph::connect(config)?)
}

async fn movie(
Expand Down Expand Up @@ -146,19 +146,10 @@ impl Service {
.execute(neo4rs::query(FIND_MOVIE).param("title", title))
.await?;

// TODO: next_as::<Movie>()?
let movie = rows
.next()
.await?
.map(|r| r.to::<Movie>())
.transpose()?
.unwrap_or_default();
let movie = rows.single_as::<Movie>().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)
}
Expand All @@ -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))]
Expand All @@ -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?;
Expand Down Expand Up @@ -284,7 +275,7 @@ struct Person {

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Voted {
updates: usize,
updates: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down

0 comments on commit a5700b8

Please sign in to comment.