Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/graphql/mutations/member_mutations.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::sync::Arc;

use crate::models::member::{CreateMemberInput, Member, UpdateMemberInput};
use async_graphql::{Context, Object, Result};
use chrono::Local;
use chrono_tz::Asia::Kolkata;
use sqlx::PgPool;

use crate::models::member::{CreateMemberInput, Member};
use std::sync::Arc;

#[derive(Default)]
pub struct MemberMutations;
Expand All @@ -15,8 +13,8 @@ impl MemberMutations {
#[graphql(name = "createMember")]
async fn create_member(&self, ctx: &Context<'_>, input: CreateMemberInput) -> Result<Member> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

let now = Local::now().with_timezone(&Kolkata).date_naive();

let member = sqlx::query_as::<_, Member>(
"INSERT INTO Member (roll_no, name, email, sex, year, hostel, mac_address, discord_id, group_id, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *"
Expand All @@ -36,4 +34,38 @@ impl MemberMutations {

Ok(member)
}

#[graphql(name = "updateMember")]
async fn update_member(&self, ctx: &Context<'_>, input: UpdateMemberInput) -> Result<Member> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

let member = sqlx::query_as::<_, Member>(
"UPDATE Member SET
roll_no = COALESCE($1, roll_no),
name = COALESCE($2, name),
email = COALESCE($3, email),
sex = COALESCE($4, sex),
year = COALESCE($5, year),
hostel = COALESCE($6, hostel),
mac_address = COALESCE($7, mac_address),
discord_id = COALESCE($8, discord_id),
group_id = COALESCE($9, group_id)
WHERE member_id = $10
RETURNING *",
)
.bind(input.roll_no)
.bind(input.name)
.bind(input.email)
.bind(input.sex)
.bind(input.year)
.bind(input.hostel)
.bind(input.mac_address)
.bind(input.discord_id)
.bind(input.group_id)
.bind(input.member_id)
.fetch_one(pool.as_ref())
.await?;

Ok(member)
}
}
14 changes: 14 additions & 0 deletions src/models/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ pub struct CreateMemberInput {
pub discord_id: String,
pub group_id: i32,
}

#[derive(InputObject)]
pub struct UpdateMemberInput {
pub member_id: i32,
pub roll_no: Option<String>,
pub name: Option<String>,
pub email: Option<String>,
pub sex: Option<Sex>,
pub year: Option<i32>,
pub hostel: Option<String>,
pub mac_address: Option<String>,
pub discord_id: Option<String>,
pub group_id: Option<i32>,
}
Loading