Skip to content

Commit 7d7b40c

Browse files
committed
feat: add addAscent mutation
1 parent 34e5ba2 commit 7d7b40c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/schema/mod.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use grade::GradeInput;
77
use postgres_types::ToSql;
88
use mutation_root::image::ImageMutationRoot;
99
use mutation_root::topo::TopoMutationRoot;
10+
use scalars::date_range::DateRange;
1011
use types::ascent::Ascent;
12+
use types::ascent_party_input::AscentPartyInput;
1113
use types::climb::Climb;
1214
use types::climber::Climber;
1315
use types::crag::Crag;
@@ -555,6 +557,54 @@ pub struct MutationRoot;
555557

556558
#[Object]
557559
impl MutationRoot {
560+
async fn add_ascent(
561+
&self,
562+
ctx: &Context<'_>,
563+
#[graphql(desc = "Climb ID")]
564+
climb_id: ID,
565+
#[graphql(desc = "Ascent date window")]
566+
date_window: Option<DateRange>,
567+
#[graphql(desc = "Ascent party")]
568+
party: AscentPartyInput,
569+
#[graphql(desc = "Whether this is a first ascent")]
570+
first_ascent: bool,
571+
#[graphql(desc = "Whether this ascent is verified")]
572+
verified: bool,
573+
) -> Result<Ascent> {
574+
let mut client = ctx.db_client().await?;
575+
let tx = client.transaction().await?;
576+
577+
let climb_id = climb_id.parse::<i32>().map_err(|_| "Invalid climb ID")?;
578+
let ascent_window = date_window.as_ref().map(|r| r.to_string());
579+
580+
let row = tx.query_one(
581+
"
582+
INSERT INTO climb.ascents
583+
(climb_id, ascent_window, ascent_duration, first_ascent, members_complete, verified)
584+
VALUES
585+
($1, COALESCE($2, NULL)::DATERANGE, NULL, $3, $4, $5)
586+
RETURNING id
587+
",
588+
&[&climb_id, &ascent_window, &first_ascent, &party.complete, &verified],
589+
).await?;
590+
591+
let ascent_id: i32 = row.get(0);
592+
593+
for member_id in &party.member_ids {
594+
let climber_id: i32 = member_id.parse().map_err(|_| "Invalid climber ID")?;
595+
tx.execute(
596+
"
597+
INSERT INTO climb.ascent_members (ascent_id, climber_id)
598+
VALUES ($1, $2)
599+
",
600+
&[&ascent_id, &climber_id],
601+
).await?;
602+
}
603+
604+
tx.commit().await?;
605+
Ok(Ascent(ascent_id))
606+
}
607+
558608
async fn add_crag(
559609
&self,
560610
ctx: &Context<'_>,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use async_graphql::{InputObject, ID};
2+
3+
#[derive(InputObject)]
4+
pub struct AscentPartyInput {
5+
pub complete: bool,
6+
pub member_ids: Vec<ID>,
7+
}

src/schema/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod ascent;
22
pub mod ascent_party;
3+
pub mod ascent_party_input;
34
pub mod climber;
45
pub mod climb;
56
pub mod crag;

0 commit comments

Comments
 (0)