Skip to content

add FXIOS-123 {Return member object} Returns member object in attendance and status update history #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
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
22 changes: 11 additions & 11 deletions Cargo.lock

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

41 changes: 24 additions & 17 deletions src/graphql/queries/attendance_queries.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
use std::sync::Arc;

use crate::models::attendance::{Attendance, AttendanceWithMember};
use async_graphql::{Context, Object, Result};
use crate::models::attendance::Attendance;
use crate::models::member::Member;
use async_graphql::{ComplexObject, Context, Object, Result};
use chrono::NaiveDate;
use sqlx::PgPool;
use std::sync::Arc;

#[derive(Default)]
pub struct AttendanceQueries;

#[ComplexObject]
impl Attendance {
async fn member(&self, ctx: &Context<'_>) -> Result<Member> {
let pool = ctx.data::<Arc<PgPool>>()?;
let member = sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE member_id = $1")
.bind(self.member_id)
.fetch_one(pool.as_ref())
.await?;

Ok(member)
}
}

#[Object]
impl AttendanceQueries {
async fn attendance(&self, ctx: &Context<'_>, member_id: i32) -> Result<Vec<Attendance>> {
Expand All @@ -25,20 +38,14 @@ impl AttendanceQueries {
&self,
ctx: &Context<'_>,
date: NaiveDate,
) -> Result<Vec<AttendanceWithMember>> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");
) -> Result<Vec<Attendance>> {
let pool = ctx.data::<Arc<PgPool>>()?;

let records = sqlx::query_as::<_, AttendanceWithMember>(
"SELECT att.attendance_id, att.member_id, att.date, att.is_present,
att.time_in, att.time_out, mem.name, mem.year, mem.group_id
FROM Attendance att
JOIN Member mem ON att.member_id = mem.member_id
WHERE att.date = $1",
)
.bind(date)
.fetch_all(pool.as_ref())
.await?;
let rows = sqlx::query_as::<_, Attendance>("SELECT * FROM Attendance WHERE date = $1")
.bind(date)
.fetch_all(pool.as_ref())
.await?;

Ok(records)
Ok(rows)
}
}
31 changes: 22 additions & 9 deletions src/graphql/queries/streak_queries.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
use std::sync::Arc;

use crate::models::member::Member;
use crate::models::status_update::StatusUpdateHistory;
use crate::models::status_update::StatusUpdateStreak as Streak;
use async_graphql::{Context, Object, Result};
use async_graphql::{ComplexObject, Context, Object, Result};
use sqlx::PgPool;
use std::sync::Arc;

#[derive(Default)]
pub struct StreakQueries;

#[ComplexObject]
impl StatusUpdateHistory {
async fn member(&self, ctx: &Context<'_>) -> Result<Member> {
let pool = ctx.data::<Arc<PgPool>>()?;
let member = sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE member_id = $1")
.bind(self.member_id)
.fetch_one(pool.as_ref())
.await?;

Ok(member)
}
}

#[Object]
impl StreakQueries {
async fn streak(&self, ctx: &Context<'_>, member_id: i32) -> Result<Streak> {
Expand Down Expand Up @@ -47,12 +60,12 @@ impl StreakQueries {
}

async fn status_update_history(&self, ctx: &Context<'_>) -> Result<Vec<StatusUpdateHistory>> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");
let pool = ctx.data::<Arc<PgPool>>()?;

Ok(
sqlx::query_as::<_, StatusUpdateHistory>("SELECT * FROM StatusUpdateHistory")
.fetch_all(pool.as_ref())
.await?,
)
let rows = sqlx::query_as::<_, StatusUpdateHistory>("SELECT * FROM StatusUpdateHistory")
.fetch_all(pool.as_ref())
.await?;

Ok(rows)
}
}
15 changes: 2 additions & 13 deletions src/models/attendance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use sqlx::FromRow;

#[derive(SimpleObject, FromRow)]
#[graphql(complex)]
pub struct Attendance {
pub attendance_id: i32,
#[graphql(skip)]
pub member_id: i32,
pub date: NaiveDate,
pub is_present: bool,
Expand Down Expand Up @@ -45,16 +47,3 @@ pub struct MarkAttendanceInput {
pub date: NaiveDate,
pub hmac_signature: String,
}

#[derive(SimpleObject, FromRow)]
pub struct AttendanceWithMember {
pub attendance_id: i32,
pub member_id: i32,
pub date: NaiveDate,
pub is_present: bool,
pub time_in: Option<NaiveTime>,
pub time_out: Option<NaiveTime>,
pub name: String,
pub year: i32,
pub group_id: i32,
}
2 changes: 2 additions & 0 deletions src/models/status_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ pub struct StreakInput {
}

#[derive(SimpleObject, FromRow)]
#[graphql(complex)]
pub struct StatusUpdateHistory {
pub update_id: i32,
#[graphql(skip)]
pub member_id: i32,
pub is_updated: bool,
pub date: NaiveDate,
Expand Down