Skip to content

Commit

Permalink
Merge pull request #2 from ivinjabraham/develop
Browse files Browse the repository at this point in the history
Implement GraphQL Mutation for Adding Attendance Records & Updates the getAttendance Query
  • Loading branch information
Ivin authored Jun 29, 2024
2 parents aef23aa + 0dad153 commit 4cc2d3f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ serde = { version = "1.0.188", features = ["derive"] }
shuttle-axum = "0.46.0"
shuttle-runtime = "0.46.0"
shuttle-shared-db = { version = "0.46.0", features = ["postgres", "sqlx"] }
sqlx = "0.7.1"
sqlx = { version = "0.7.1", features = ["chrono"] }
tokio = "1.28.2"
7 changes: 4 additions & 3 deletions src/db/attendance.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use chrono::{NaiveDate, NaiveTime};
use sqlx::FromRow;
use async_graphql::SimpleObject;

//Struct for the Attendance table
#[derive(FromRow, SimpleObject)]
pub struct Attendance {
pub id: i32,
pub date: String,
pub timein: String,
pub timeout: String,
pub date: NaiveDate,
pub timein: NaiveTime,
pub timeout: NaiveTime,
}
27 changes: 26 additions & 1 deletion src/graphql/mutations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use async_graphql::{Context, Object};
use chrono::{NaiveDate, NaiveTime};
use sqlx::PgPool;
use sqlx::types::chrono;
use std::sync::Arc;

use crate::db::member::Member;
use crate::db::{member::Member, attendance::Attendance};

pub struct MutationRoot;

Expand Down Expand Up @@ -34,4 +36,27 @@ impl MutationRoot {

Ok(member)
}

async fn add_attendance(
&self,
ctx: &Context<'_>,
id: i32,
date: NaiveDate,
timein: NaiveTime,
timeout: NaiveTime,
) -> Result<Attendance, sqlx::Error> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");

let attendance = sqlx::query_as::<_, Attendance>(
"INSERT INTO Attendance (id, date, timein, timeout) VALUES ($1, $2, $3, $4) RETURNING *"
)
.bind(id)
.bind(date)
.bind(timein)
.bind(timeout)
.fetch_one(pool.as_ref())
.await?;

Ok(attendance)
}
}
24 changes: 17 additions & 7 deletions src/graphql/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use async_graphql::{Context, Object};
use sqlx::PgPool;
use std::sync::Arc;
use chrono::NaiveDate;


use crate::db::{member::Member, attendance::Attendance};

Expand All @@ -16,12 +18,20 @@ impl QueryRoot {
Ok(users)
}

async fn get_attendance(&self, ctx: &Context<'_>, date: String) -> Result<Vec<Attendance>, sqlx::Error> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
let attendance_list = sqlx::query_as::<_, Attendance>("SELECT id, date, timein, timeout FROM Attendance WHERE date = $1")
.bind(date)
.fetch_all(pool.as_ref())
.await?;
Ok(attendance_list)
async fn get_attendance(
&self,
ctx: &Context<'_>,
date: NaiveDate,
) -> Result<Vec<Attendance>, sqlx::Error> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");

let attendance_list = sqlx::query_as::<_, Attendance>(
"SELECT id, date, timein, timeout FROM Attendance WHERE date = $1"
)
.bind(date)
.fetch_all(pool.as_ref())
.await?;

Ok(attendance_list)
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::graphql::mutations::MutationRoot;
use crate::graphql::query::QueryRoot;
use crate::routes::graphiql;


mod db;
mod graphql;
mod routes;
Expand Down

0 comments on commit 4cc2d3f

Please sign in to comment.