1- use std:: sync:: Arc ;
2-
1+ use crate :: models:: member:: { CreateMemberInput , Member , UpdateMemberInput } ;
32use async_graphql:: { Context , Object , Result } ;
43use chrono:: Local ;
54use chrono_tz:: Asia :: Kolkata ;
65use sqlx:: PgPool ;
7-
8- use crate :: models:: member:: { CreateMemberInput , Member } ;
6+ use std:: sync:: Arc ;
97
108#[ derive( Default ) ]
119pub struct MemberMutations ;
@@ -15,8 +13,8 @@ impl MemberMutations {
1513 #[ graphql( name = "createMember" ) ]
1614 async fn create_member ( & self , ctx : & Context < ' _ > , input : CreateMemberInput ) -> Result < Member > {
1715 let pool = ctx. data :: < Arc < PgPool > > ( ) . expect ( "Pool must be in context." ) ;
18-
1916 let now = Local :: now ( ) . with_timezone ( & Kolkata ) . date_naive ( ) ;
17+
2018 let member = sqlx:: query_as :: < _ , Member > (
2119 "INSERT INTO Member (roll_no, name, email, sex, year, hostel, mac_address, discord_id, group_id, created_at)
2220 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *"
@@ -36,4 +34,38 @@ impl MemberMutations {
3634
3735 Ok ( member)
3836 }
37+
38+ #[ graphql( name = "updateMember" ) ]
39+ async fn update_member ( & self , ctx : & Context < ' _ > , input : UpdateMemberInput ) -> Result < Member > {
40+ let pool = ctx. data :: < Arc < PgPool > > ( ) . expect ( "Pool must be in context." ) ;
41+
42+ let member = sqlx:: query_as :: < _ , Member > (
43+ "UPDATE Member SET
44+ roll_no = COALESCE($1, roll_no),
45+ name = COALESCE($2, name),
46+ email = COALESCE($3, email),
47+ sex = COALESCE($4, sex),
48+ year = COALESCE($5, year),
49+ hostel = COALESCE($6, hostel),
50+ mac_address = COALESCE($7, mac_address),
51+ discord_id = COALESCE($8, discord_id),
52+ group_id = COALESCE($9, group_id)
53+ WHERE member_id = $10
54+ RETURNING *" ,
55+ )
56+ . bind ( input. roll_no )
57+ . bind ( input. name )
58+ . bind ( input. email )
59+ . bind ( input. sex )
60+ . bind ( input. year )
61+ . bind ( input. hostel )
62+ . bind ( input. mac_address )
63+ . bind ( input. discord_id )
64+ . bind ( input. group_id )
65+ . bind ( input. member_id )
66+ . fetch_one ( pool. as_ref ( ) )
67+ . await ?;
68+
69+ Ok ( member)
70+ }
3971}
0 commit comments