Skip to content

Commit 4cc2d3f

Browse files
author
Ivin
authored
Merge pull request #2 from ivinjabraham/develop
Implement GraphQL Mutation for Adding Attendance Records & Updates the getAttendance Query
2 parents aef23aa + 0dad153 commit 4cc2d3f

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ serde = { version = "1.0.188", features = ["derive"] }
1212
shuttle-axum = "0.46.0"
1313
shuttle-runtime = "0.46.0"
1414
shuttle-shared-db = { version = "0.46.0", features = ["postgres", "sqlx"] }
15-
sqlx = "0.7.1"
15+
sqlx = { version = "0.7.1", features = ["chrono"] }
1616
tokio = "1.28.2"

src/db/attendance.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use chrono::{NaiveDate, NaiveTime};
12
use sqlx::FromRow;
23
use async_graphql::SimpleObject;
34

45
//Struct for the Attendance table
56
#[derive(FromRow, SimpleObject)]
67
pub struct Attendance {
78
pub id: i32,
8-
pub date: String,
9-
pub timein: String,
10-
pub timeout: String,
9+
pub date: NaiveDate,
10+
pub timein: NaiveTime,
11+
pub timeout: NaiveTime,
1112
}

src/graphql/mutations.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use async_graphql::{Context, Object};
2+
use chrono::{NaiveDate, NaiveTime};
23
use sqlx::PgPool;
4+
use sqlx::types::chrono;
35
use std::sync::Arc;
46

5-
use crate::db::member::Member;
7+
use crate::db::{member::Member, attendance::Attendance};
68

79
pub struct MutationRoot;
810

@@ -34,4 +36,27 @@ impl MutationRoot {
3436

3537
Ok(member)
3638
}
39+
40+
async fn add_attendance(
41+
&self,
42+
ctx: &Context<'_>,
43+
id: i32,
44+
date: NaiveDate,
45+
timein: NaiveTime,
46+
timeout: NaiveTime,
47+
) -> Result<Attendance, sqlx::Error> {
48+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
49+
50+
let attendance = sqlx::query_as::<_, Attendance>(
51+
"INSERT INTO Attendance (id, date, timein, timeout) VALUES ($1, $2, $3, $4) RETURNING *"
52+
)
53+
.bind(id)
54+
.bind(date)
55+
.bind(timein)
56+
.bind(timeout)
57+
.fetch_one(pool.as_ref())
58+
.await?;
59+
60+
Ok(attendance)
61+
}
3762
}

src/graphql/query.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use async_graphql::{Context, Object};
22
use sqlx::PgPool;
33
use std::sync::Arc;
4+
use chrono::NaiveDate;
5+
46

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

@@ -16,12 +18,20 @@ impl QueryRoot {
1618
Ok(users)
1719
}
1820

19-
async fn get_attendance(&self, ctx: &Context<'_>, date: String) -> Result<Vec<Attendance>, sqlx::Error> {
20-
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
21-
let attendance_list = sqlx::query_as::<_, Attendance>("SELECT id, date, timein, timeout FROM Attendance WHERE date = $1")
22-
.bind(date)
23-
.fetch_all(pool.as_ref())
24-
.await?;
25-
Ok(attendance_list)
21+
async fn get_attendance(
22+
&self,
23+
ctx: &Context<'_>,
24+
date: NaiveDate,
25+
) -> Result<Vec<Attendance>, sqlx::Error> {
26+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
27+
28+
let attendance_list = sqlx::query_as::<_, Attendance>(
29+
"SELECT id, date, timein, timeout FROM Attendance WHERE date = $1"
30+
)
31+
.bind(date)
32+
.fetch_all(pool.as_ref())
33+
.await?;
34+
35+
Ok(attendance_list)
2636
}
2737
}

src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::graphql::mutations::MutationRoot;
99
use crate::graphql::query::QueryRoot;
1010
use crate::routes::graphiql;
1111

12-
1312
mod db;
1413
mod graphql;
1514
mod routes;

0 commit comments

Comments
 (0)