Skip to content

Commit 0e50702

Browse files
committed
feat: add images attribute to formation type
Allows the client to get images of a formation.
1 parent 32da138 commit 0e50702

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/schema/formation.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::schema::area::Area;
44
use crate::schema::climb::Climb;
55
use crate::AppData;
66

7+
use super::Image;
8+
79
#[derive(SimpleObject, InputObject)]
810
#[graphql(input_name = "CoordinateInput")]
911
pub struct Coordinate {
@@ -161,4 +163,28 @@ impl Formation {
161163

162164
Ok(climbs)
163165
}
166+
167+
async fn images(&self, ctx: &Context<'_>) -> Result<Vec<Image>> {
168+
let data = ctx.data::<AppData>()?;
169+
let client = match &data.pg_pool {
170+
Some(pool) => pool.get().await?,
171+
None => {
172+
return Err("Database connection is not available".into());
173+
}
174+
};
175+
176+
let result = client
177+
.query(
178+
"SELECT image_id FROM formations_in_image WHERE formation_id = $1",
179+
&[&self.0],
180+
)
181+
.await?;
182+
183+
let images = result
184+
.into_iter()
185+
.map(|row| row.try_get(0).map(Image))
186+
.collect::<Result<Vec<Image>, _>>()?;
187+
188+
Ok(images)
189+
}
164190
}

0 commit comments

Comments
 (0)