From daf84309ffe7f76bbfff14a022cc5d431f3695cf Mon Sep 17 00:00:00 2001 From: Wreck-X Date: Tue, 2 Jul 2024 20:40:25 +0530 Subject: [PATCH 1/3] Add: Update mutation for attendance record --- src/db/attendance.rs | 1 + src/graphql/mutations.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/db/attendance.rs b/src/db/attendance.rs index 5f4b80d..724a42b 100644 --- a/src/db/attendance.rs +++ b/src/db/attendance.rs @@ -9,4 +9,5 @@ pub struct Attendance { pub date: NaiveDate, pub timein: NaiveTime, pub timeout: NaiveTime, + pub present: bool, } diff --git a/src/graphql/mutations.rs b/src/graphql/mutations.rs index 337fdd1..9661bf2 100644 --- a/src/graphql/mutations.rs +++ b/src/graphql/mutations.rs @@ -39,6 +39,7 @@ impl MutationRoot { Ok(member) } + //Mutation for adding Attendance to the Attendance table async fn add_attendance( &self, @@ -62,4 +63,25 @@ impl MutationRoot { Ok(attendance) } + + async fn update_attendance_present( + &self, + ctx: &Context<'_>, + id: i32, + date: NaiveDate, + present: bool, + ) -> Result { + let pool = ctx.data::>().expect("Pool not found in context"); + + let attendance = sqlx::query_as::<_, Attendance>( + "UPDATE Attendance SET present = $1 WHERE id = $2 AND date = $3 RETURNING *" + ) + .bind(present) + .bind(id) + .bind(date) + .fetch_one(pool.as_ref()) + .await?; + + Ok(attendance) + } } From 390e43f3415b7ed43754f969d784a13e5559078f Mon Sep 17 00:00:00 2001 From: Wreck-X Date: Tue, 13 Aug 2024 18:31:24 +0530 Subject: [PATCH 2/3] renames update_attendance_present to mark_attendance, fixes documentation issues --- migrations/20240711150547_add_mac.sql | 1 + .../20240813125650_rename_present_to_ispresent.sql | 1 + .../20240813130838_rename_present_to_isPresent.sql | 1 + src/db/attendance.rs | 2 +- src/graphql/mutations.rs | 10 +++++----- src/graphql/query.rs | 4 ++-- 6 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 migrations/20240711150547_add_mac.sql create mode 100644 migrations/20240813125650_rename_present_to_ispresent.sql create mode 100644 migrations/20240813130838_rename_present_to_isPresent.sql diff --git a/migrations/20240711150547_add_mac.sql b/migrations/20240711150547_add_mac.sql new file mode 100644 index 0000000..3808373 --- /dev/null +++ b/migrations/20240711150547_add_mac.sql @@ -0,0 +1 @@ +ALTER TABLE Member ADD COLUMN macaddress TEXT; \ No newline at end of file diff --git a/migrations/20240813125650_rename_present_to_ispresent.sql b/migrations/20240813125650_rename_present_to_ispresent.sql new file mode 100644 index 0000000..79aab75 --- /dev/null +++ b/migrations/20240813125650_rename_present_to_ispresent.sql @@ -0,0 +1 @@ +ALTER TABLE Attendance RENAME COLUMN present TO ispresent; diff --git a/migrations/20240813130838_rename_present_to_isPresent.sql b/migrations/20240813130838_rename_present_to_isPresent.sql new file mode 100644 index 0000000..4c2936f --- /dev/null +++ b/migrations/20240813130838_rename_present_to_isPresent.sql @@ -0,0 +1 @@ +ALTER TABLE Attendance RENAME COLUMN ispresent TO is_present; diff --git a/src/db/attendance.rs b/src/db/attendance.rs index 724a42b..5ba3a3f 100644 --- a/src/db/attendance.rs +++ b/src/db/attendance.rs @@ -9,5 +9,5 @@ pub struct Attendance { pub date: NaiveDate, pub timein: NaiveTime, pub timeout: NaiveTime, - pub present: bool, + pub is_present: bool, } diff --git a/src/graphql/mutations.rs b/src/graphql/mutations.rs index 9661bf2..d001672 100644 --- a/src/graphql/mutations.rs +++ b/src/graphql/mutations.rs @@ -40,7 +40,7 @@ impl MutationRoot { } - //Mutation for adding Attendance to the Attendance table + //Mutation for adding attendance to the Attendance table async fn add_attendance( &self, ctx: &Context<'_>, @@ -64,19 +64,19 @@ impl MutationRoot { Ok(attendance) } - async fn update_attendance_present( + async fn mark_attendance( &self, ctx: &Context<'_>, id: i32, date: NaiveDate, - present: bool, + is_present: bool, ) -> Result { let pool = ctx.data::>().expect("Pool not found in context"); let attendance = sqlx::query_as::<_, Attendance>( - "UPDATE Attendance SET present = $1 WHERE id = $2 AND date = $3 RETURNING *" + "UPDATE Attendance SET is_present = $1 WHERE id = $2 AND date = $3 RETURNING *" ) - .bind(present) + .bind(is_present) .bind(id) .bind(date) .fetch_one(pool.as_ref()) diff --git a/src/graphql/query.rs b/src/graphql/query.rs index c249783..d8ebfb7 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -11,7 +11,7 @@ pub struct QueryRoot; #[Object] impl QueryRoot { - //Query for retrieving the Members + //Query for retrieving the members async fn get_member(&self, ctx: &Context<'_>) -> Result, sqlx::Error> { let pool = ctx.data::>().expect("Pool not found in context"); let users = sqlx::query_as::<_, Member>("SELECT * FROM Member") @@ -20,7 +20,7 @@ impl QueryRoot { Ok(users) } - //Query for retrieving the Attendance based on date + //Query for retrieving the attendance based on date async fn get_attendance( &self, ctx: &Context<'_>, From 453bdd4496448ef4591f7511f4f6613ca1049c16 Mon Sep 17 00:00:00 2001 From: Wreck-X Date: Wed, 14 Aug 2024 15:59:21 +0530 Subject: [PATCH 3/3] add-macaddress-field-to-members-table --- migrations/20240813130838_rename_present_to_isPresent.sql | 2 +- migrations/20240813132052_rename_present_to_is_present.sql | 2 +- src/db/member.rs | 1 + src/graphql/mutations.rs | 6 ++++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/migrations/20240813130838_rename_present_to_isPresent.sql b/migrations/20240813130838_rename_present_to_isPresent.sql index 4c2936f..06284bc 100644 --- a/migrations/20240813130838_rename_present_to_isPresent.sql +++ b/migrations/20240813130838_rename_present_to_isPresent.sql @@ -1 +1 @@ -ALTER TABLE Attendance RENAME COLUMN ispresent TO is_present; +ALTER TABLE Attendance RENAME COLUMN ispresent TO is_prresent; diff --git a/migrations/20240813132052_rename_present_to_is_present.sql b/migrations/20240813132052_rename_present_to_is_present.sql index 4c2936f..8b13789 100644 --- a/migrations/20240813132052_rename_present_to_is_present.sql +++ b/migrations/20240813132052_rename_present_to_is_present.sql @@ -1 +1 @@ -ALTER TABLE Attendance RENAME COLUMN ispresent TO is_present; + diff --git a/src/db/member.rs b/src/db/member.rs index 3d3b16b..52970f1 100644 --- a/src/db/member.rs +++ b/src/db/member.rs @@ -13,4 +13,5 @@ pub struct Member { pub email: String, pub sex: String, pub year: i32, + pub macaddress: String, } diff --git a/src/graphql/mutations.rs b/src/graphql/mutations.rs index d001672..db8d3d5 100644 --- a/src/graphql/mutations.rs +++ b/src/graphql/mutations.rs @@ -20,12 +20,13 @@ impl MutationRoot { hostel: String, email: String, sex: String, - year: i32 + year: i32, + macaddress: String, ) -> Result { let pool = ctx.data::>().expect("Pool not found in context"); let member = sqlx::query_as::<_, Member>( - "INSERT INTO Member (rollno, name, hostel, email, sex, year) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *" + "INSERT INTO Member (rollno, name, hostel, email, sex, year, macaddress) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *" ) .bind(rollno) .bind(name) @@ -33,6 +34,7 @@ impl MutationRoot { .bind(email) .bind(sex) .bind(year) + .bind(macaddress) .fetch_one(pool.as_ref()) .await?;