@@ -7,7 +7,9 @@ use grade::GradeInput;
77use postgres_types:: ToSql ;
88use mutation_root:: image:: ImageMutationRoot ;
99use mutation_root:: topo:: TopoMutationRoot ;
10+ use scalars:: date_range:: DateRange ;
1011use types:: ascent:: Ascent ;
12+ use types:: ascent_party_input:: AscentPartyInput ;
1113use types:: climb:: Climb ;
1214use types:: climber:: Climber ;
1315use types:: crag:: Crag ;
@@ -555,6 +557,54 @@ pub struct MutationRoot;
555557
556558#[ Object ]
557559impl 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 < ' _ > ,
0 commit comments