Skip to content

Commit 6f4e9b0

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 2bbbf01 commit 6f4e9b0

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
@@ -195,6 +195,71 @@ impl Climber {
195195
}
196196
}
197197

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

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

0 commit comments

Comments
 (0)