Skip to content

Commit e02e138

Browse files
committed
feat: add ascents type and simple getters
This _does not_ add the date window support, yet. This is because there is not a great native rust-postgres way of getting PostgreSQLs DATERANGE type. Looks like there's rust-postgres/rust-postgres-range#18, but it hasn't been merged in a year and the general state of rust-postgres-range seems unmaintained.. perhaps @tkbrigham has a good point in rust-postgres/rust-postgres#601.. it'd be great if all the PostgreSQL features were supported in rust-postgres directly.
1 parent 22c1573 commit e02e138

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

src/schema/mod.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,71 @@ impl Climber {
201201
}
202202
}
203203

204+
struct Ascent(pub i32);
205+
206+
#[Object]
207+
impl Ascent {
208+
async fn id(&self) -> ID {
209+
self.0.into()
210+
}
211+
212+
async fn climb<'a>(
213+
&self,
214+
ctx: &Context<'a>,
215+
) -> Result<Climb> {
216+
let data = ctx.data::<AppData>()?;
217+
let client = match &data.pg_pool {
218+
Some(pool) => pool.get().await?,
219+
None => {
220+
return Err("Database connection is not available".into());
221+
}
222+
};
223+
224+
let result = client
225+
.query_one(
226+
"
227+
SELECT climb_id
228+
FROM ascents
229+
WHERE id = $1
230+
",
231+
&[&self.0],
232+
)
233+
.await?;
234+
235+
let climb_id: i32 = result.try_get(0)?;
236+
237+
Ok(Climb(climb_id))
238+
}
239+
240+
async fn climber<'a>(
241+
&self,
242+
ctx: &Context<'a>,
243+
) -> Result<Climber> {
244+
let data = ctx.data::<AppData>()?;
245+
let client = match &data.pg_pool {
246+
Some(pool) => pool.get().await?,
247+
None => {
248+
return Err("Database connection is not available".into());
249+
}
250+
};
251+
252+
let result = client
253+
.query_one(
254+
"
255+
SELECT climber_id
256+
FROM ascents
257+
WHERE id = $1
258+
",
259+
&[&self.0],
260+
)
261+
.await?;
262+
263+
let climber_id: i32 = result.try_get(0)?;
264+
265+
Ok(Climber(climber_id))
266+
}
267+
}
268+
204269
#[Object]
205270
impl QueryRoot {
206271
async fn areas<'a>(
@@ -299,6 +364,52 @@ impl QueryRoot {
299364
Ok(Area(id))
300365
}
301366

367+
async fn ascents<'a>(
368+
&self,
369+
ctx: &Context<'a>,
370+
) -> Result<Vec<Ascent>> {
371+
let data = ctx.data::<AppData>()?;
372+
let client = match &data.pg_pool {
373+
Some(pool) => pool.get().await?,
374+
None => {
375+
return Err("Database connection is not available".into());
376+
}
377+
};
378+
379+
let result = client
380+
.query("SELECT ascents.id FROM ascents", &[])
381+
.await?;
382+
383+
let ascents = result
384+
.into_iter()
385+
.map(|row| row.try_get(0).map(Ascent))
386+
.collect::<Result<Vec<Ascent>, _>>()?;
387+
388+
Ok(ascents)
389+
}
390+
391+
async fn ascent<'a>(
392+
&self,
393+
ctx: &Context<'a>,
394+
#[graphql(desc = "Ascent id")] id: ID,
395+
) -> Result<Ascent> {
396+
let id: i32 = id.0.parse().map_err(|_| "Invalid ID format")?;
397+
let data = ctx.data::<AppData>()?;
398+
let client = match &data.pg_pool {
399+
Some(pool) => pool.get().await?,
400+
None => {
401+
return Err("Database connection is not available".into());
402+
}
403+
};
404+
405+
// Just check for existence
406+
client
407+
.query_one("SELECT 1 FROM ascents WHERE id = $1", &[&id])
408+
.await?;
409+
410+
Ok(Ascent(id))
411+
}
412+
302413
async fn climbs<'a>(
303414
&self,
304415
ctx: &Context<'a>,

0 commit comments

Comments
 (0)