Skip to content

presentCountByDate and absentCountByDate #130

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
29 changes: 18 additions & 11 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ tracing = "0.1.41"
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "time", "fmt", "std"] }
dotenv = "0.15.0"
time = { version = "0.3.37", features = ["formatting"] }
anyhow = "1.0.98"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to be used anywhere?

2 changes: 2 additions & 0 deletions src/graphql/queries/attendance_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ impl AttendanceQueries {

Ok(records)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you run clippy/linting tools to avoid unnecessary additions like this.


}
86 changes: 86 additions & 0 deletions src/graphql/queries/member_queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use async_graphql::{ComplexObject, Context, Object, Result};
use sqlx::PgPool;
use std::sync::Arc;
use chrono::NaiveDate;


use crate::models::{
attendance::{AttendanceInfo, AttendanceSummaryInfo},
Expand Down Expand Up @@ -106,5 +108,89 @@ impl Member {
.fetch_all(pool.as_ref())
.await
.unwrap_or_default()


}

async fn present_count_by_date(
&self,
ctx: &Context<'_>,
start_date: NaiveDate,
end_date:NaiveDate,
) -> Result<i64>{

if end_date < start_date {
return Err("end_date must be >= start_date".into());
}

let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

let records : i64 = sqlx::query_scalar(
"
SELECT COUNT(att.is_present)
FROM attendance att
INNER JOIN member m ON att.member_id = m.member_id
WHERE att.member_id = $3
AND att.is_present = true
AND att.date BETWEEN $1 AND $2"
)
.bind(start_date)
.bind(end_date)
.bind(self.member_id)
.fetch_one(pool.as_ref())
.await?;

Ok(records)

}

async fn absent_count_by_date(
&self,
ctx: &Context<'_>,
start_date: NaiveDate,
end_date:NaiveDate,
) -> Result<i64>{

if end_date < start_date {
return Err("end_date must be >= start_date".into());
}

let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

let working_days : i64 = sqlx::query_scalar(
"
SELECT COUNT(*) AS working_days
FROM (
SELECT date
FROM attendance
where date between $1 and $2 GROUP BY date
HAVING BOOL_or(is_present = true)
) AS working_dates;
"
)

.bind(start_date)
.bind(end_date)
.bind(self.member_id)
.fetch_one(pool.as_ref())
.await?;

let present : i64 = sqlx::query_scalar(
"
SELECT COUNT(att.is_present)
FROM attendance att
INNER JOIN member m ON att.member_id = m.member_id
WHERE att.member_id = $3
AND att.is_present = true
AND att.date BETWEEN $1 AND $2"
)
.bind(start_date)
.bind(end_date)
.bind(self.member_id)
.fetch_one(pool.as_ref())
.await?;

Ok(working_days-present)

}
}
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub mod graphql;
pub mod models;
pub mod routes;

use dotenv::dotenv;


/// Handles all over environment variables in one place.
// TODO: Replace with `Config.rs` crate.
struct Config {
Expand All @@ -39,6 +42,11 @@ impl Config {

#[tokio::main]
async fn main() {
dotenv().ok();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think this is necessary?


// Fetch the DATABASE_URL env var


let config = Config::from_env();
setup_tracing(&config.env);

Expand All @@ -53,7 +61,7 @@ async fn main() {
let router = setup_router(schema, cors, config.env == "development");

info!("Starting Root...");
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.port))
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}",config.port))
.await
.unwrap();
axum::serve(listener, router).await.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/models/attendance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ pub struct AttendanceWithMember {
pub year: i32,
pub group_id: i32,
}