Add database and location query functionality
This commit is contained in:
parent
e5ac7b13b7
commit
d3d4f2a405
3 changed files with 51 additions and 8 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -1838,6 +1838,7 @@ checksum = "ee6798b1838b6a0f69c007c133b8df5866302197e404e8b6ee8ed3e3a5e68dc6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
"base64",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"chrono",
|
||||||
"crc",
|
"crc",
|
||||||
"crossbeam-queue",
|
"crossbeam-queue",
|
||||||
"either",
|
"either",
|
||||||
|
|
@ -1915,6 +1916,7 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"chrono",
|
||||||
"crc",
|
"crc",
|
||||||
"digest",
|
"digest",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
|
@ -1956,6 +1958,7 @@ dependencies = [
|
||||||
"base64",
|
"base64",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
"chrono",
|
||||||
"crc",
|
"crc",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"etcetera",
|
"etcetera",
|
||||||
|
|
@ -1990,6 +1993,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c2d12fe70b2c1b4401038055f90f151b78208de1f9f89a7dbfd41587a10c3eea"
|
checksum = "c2d12fe70b2c1b4401038055f90f151b78208de1f9f89a7dbfd41587a10c3eea"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"atoi",
|
"atoi",
|
||||||
|
"chrono",
|
||||||
"flume",
|
"flume",
|
||||||
"futures-channel",
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,13 @@ async-graphql = "7"
|
||||||
async-graphql-axum = "7"
|
async-graphql-axum = "7"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
sqlx = { version = "0.8", features = ["runtime-tokio", "tls-rustls-aws-lc-rs", "postgres"] }
|
sqlx = { version = "0.8", features = [
|
||||||
|
"runtime-tokio",
|
||||||
|
"tls-rustls-aws-lc-rs",
|
||||||
|
"postgres",
|
||||||
|
"chrono",
|
||||||
|
"json"
|
||||||
|
] }
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,55 @@ use sqlx::PgPool;
|
||||||
|
|
||||||
pub struct QueryRoot;
|
pub struct QueryRoot;
|
||||||
|
|
||||||
|
fn get_pg_pool<'a>(ctx: &'a Context<'_>) -> &'a PgPool {
|
||||||
|
ctx.data::<PgPool>()
|
||||||
|
.expect("A database connection does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
#[Object]
|
#[Object]
|
||||||
impl QueryRoot {
|
impl QueryRoot {
|
||||||
pub async fn datasets(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Dataset>> {
|
pub async fn datasets(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Dataset>> {
|
||||||
let pool = ctx
|
let pool = get_pg_pool(ctx);
|
||||||
.data::<PgPool>()
|
|
||||||
.expect("A database connection does not exist");
|
|
||||||
|
|
||||||
let rows: Vec<DatasetRow> = sqlx::query_as("SELECT * FROM datasets")
|
let rows: Vec<DatasetRow> = sqlx::query_as("SELECT * FROM datasets")
|
||||||
.fetch_all(pool)
|
.fetch_all(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let result = rows.into_iter().map(|r| Dataset::from(r)).collect();
|
return Ok(rows.into_iter().map(|r| Dataset::from(r)).collect());
|
||||||
return Ok(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn query_dataset(&self, id: i32) -> anyhow::Result<Record> {
|
pub async fn query_dataset(
|
||||||
unimplemented!("This doesn't work yet");
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
id: i32,
|
||||||
|
) -> anyhow::Result<Option<Record>> {
|
||||||
|
let pool = get_pg_pool(ctx);
|
||||||
|
|
||||||
|
let row: Option<RecordRow> = sqlx::query_as(
|
||||||
|
r#"SELECT * FROM records
|
||||||
|
WHERE dataset_id = $1
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
LIMIT 1;"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.map(Into::into))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_location(&self, ctx: &Context<'_>, id: i32) -> anyhow::Result<Location> {
|
||||||
|
let pool = get_pg_pool(ctx);
|
||||||
|
|
||||||
|
let row: LocationRow = sqlx::query_as(
|
||||||
|
r#"SELECT * FROM locations
|
||||||
|
WHERE id = $1
|
||||||
|
LIMIT 1;"#,
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(row.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue