1+ use std:: fmt:: Display ;
2+
13use async_graphql:: { Context , Enum , Object , OneofObject , Result , SimpleObject , Union , ID } ;
24
35use area:: Area ;
46use climb:: Climb ;
57use fontainebleau_grade:: FontainebleauGrade ;
68use formation:: { Coordinate , Formation } ;
9+ use postgres_types:: ToSql ;
710use vermin_grade:: VerminGrade ;
811use yosemite_decimal_grade:: YosemiteDecimalGrade ;
912
@@ -18,13 +21,56 @@ pub mod yosemite_decimal_grade;
1821
1922pub struct QueryRoot ;
2023
21- #[ derive( OneofObject ) ]
24+ #[ derive( Debug , OneofObject ) ]
2225enum GradeInput {
2326 Vermin ( VerminGrade ) ,
2427 Fontainebleau ( FontainebleauGrade ) ,
2528 YosemiteDecimal ( YosemiteDecimalGrade ) ,
2629}
2730
31+ impl Display for GradeInput {
32+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
33+ match self {
34+ GradeInput :: Vermin ( vermin_grade) => write ! ( f, "{}" , vermin_grade) ,
35+ GradeInput :: Fontainebleau ( fontainebleau_grade) => write ! ( f, "{}" , fontainebleau_grade) ,
36+ GradeInput :: YosemiteDecimal ( yosemite_decimal_grade) => write ! ( f, "{}" , yosemite_decimal_grade) ,
37+ }
38+ }
39+ }
40+
41+ impl ToSql for GradeInput {
42+ fn encode_format ( & self , _ty : & postgres_types:: Type ) -> postgres_types:: Format {
43+ postgres_types:: Format :: Text
44+ }
45+
46+ fn to_sql ( & self , _: & postgres_types:: Type , out : & mut bytes:: BytesMut ) -> std:: result:: Result < postgres_types:: IsNull , Box < dyn std:: error:: Error + Sync + Send > >
47+ where
48+ Self : Sized
49+ {
50+ let encoded = self . to_string ( ) ;
51+ out. extend_from_slice ( encoded. as_bytes ( ) ) ;
52+ Ok ( postgres_types:: IsNull :: No )
53+ }
54+
55+ fn accepts ( ty : & postgres_types:: Type ) -> bool
56+ where
57+ Self : Sized
58+ {
59+ matches ! ( ty. name( ) , "grade" )
60+ }
61+
62+ fn to_sql_checked (
63+ & self ,
64+ ty : & postgres_types:: Type ,
65+ out : & mut bytes:: BytesMut ,
66+ ) -> std:: result:: Result < postgres_types:: IsNull , Box < dyn std:: error:: Error + Sync + Send > > {
67+ if !Self :: accepts ( ty) {
68+ return Err ( "Unsupported PostgreSQL type" . into ( ) ) ;
69+ }
70+ self . to_sql ( ty, out)
71+ }
72+ }
73+
2874#[ derive( Enum , Clone , Copy , PartialEq , Eq ) ]
2975enum GradeOperation {
3076 Add ,
@@ -888,44 +934,15 @@ impl MutationRoot {
888934 }
889935 } ;
890936
891- match grade {
892- GradeInput :: Vermin ( VerminGrade ( value) ) => {
893- client
894- . execute (
895- "
896- INSERT INTO climb_verm_grades (climb_id, value)
897- VALUES ($1, $2) ON CONFLICT DO NOTHING
898- " ,
899- & [ & id, & ( value as i32 ) ] ,
900- )
901- . await ?;
902- }
903- GradeInput :: Fontainebleau ( FontainebleauGrade {
904- number,
905- letter,
906- plus,
907- } ) => {
908- client
909- . execute (
910- "
911- INSERT INTO climb_font_grades (climb_id, value, letter, plus)
912- VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING
913- " ,
914- & [ & id, & ( number as i32 ) , & letter, & plus] ,
915- )
916- . await ?;
917- }
918- GradeInput :: YosemiteDecimal ( YosemiteDecimalGrade { grade, letter } ) => {
919- client
920- . execute (
921- "INSERT INTO climb_yds_grades (climb_id, value, letter)
922- VALUES ($1, $2, $3) ON CONFLICT DO NOTHING
923- " ,
924- & [ & id, & ( grade as i32 ) , & letter] ,
925- )
926- . await ?;
927- }
928- }
937+ client
938+ . execute (
939+ "
940+ INSERT INTO climb_grades (climb_id, grade)
941+ VALUES ($1, $2) ON CONFLICT DO NOTHING
942+ " ,
943+ & [ & id, & ( grade) ] ,
944+ )
945+ . await ?;
929946
930947 Ok ( Climb ( id) )
931948 }
@@ -945,45 +962,15 @@ impl MutationRoot {
945962 }
946963 } ;
947964
948- match grade {
949- GradeInput :: Vermin ( VerminGrade ( number) ) => {
950- client
951- . execute (
952- "
953- DELETE FROM climb_verm_grades
954- WHERE climb_id = $1 AND value = $2
955- " ,
956- & [ & id, & ( number as i32 ) ] ,
957- )
958- . await ?;
959- }
960- GradeInput :: Fontainebleau ( FontainebleauGrade {
961- number,
962- letter,
963- plus,
964- } ) => {
965- client
966- . execute (
967- "
968- DELETE FROM climb_font_grades
969- WHERE climb_id = $1 AND value = $2 AND (letter IS NOT DISTINCT FROM $3) AND plus = $4
970- " ,
971- & [ & id, & ( number as i32 ) , & letter, & plus] ,
972- )
973- . await ?;
974- }
975- GradeInput :: YosemiteDecimal ( YosemiteDecimalGrade { grade, letter } ) => {
976- client
977- . execute (
978- "
979- DELETE FROM climb_yds_grades
980- WHERE climb_id = $1 AND value = $2 AND (letter IS NOT DISTINCT FROM $3)
981- " ,
982- & [ & id, & ( grade as i32 ) , & letter] ,
983- )
984- . await ?;
985- }
986- }
965+ client
966+ . execute (
967+ "
968+ DELETE FROM climb_grades
969+ WHERE climb_id = $1 AND grade = $2
970+ " ,
971+ & [ & id, & ( grade) ] ,
972+ )
973+ . await ?;
987974
988975 Ok ( Climb ( id) )
989976 }
0 commit comments