Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ghcr-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Create and publish Docker image to GHCR
on:
workflow_dispatch:
push:
branches: ['master']
branches: ['main']

jobs:
build-and-push-image:
Expand Down Expand Up @@ -38,7 +38,7 @@ jobs:
images: ghcr.io/amfoss/root
tags: |
# set latest tag for master branch
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }},priority=2000
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }},priority=2000
type=schedule,pattern={{date 'YYYYMMDD'}}
type=ref,event=tag
type=ref,event=pr
Expand Down
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Build Stage
FROM rust:slim-bullseye AS builder
WORKDIR /build
WORKDIR /builder

RUN cargo init
# Compile deps in a separate layer (for caching)
COPY Cargo.toml Cargo.lock ./
# Cargo requires at least one source file for compiling dependencies
RUN mkdir src && echo "fn main() { println!(\"Hello, world!\"); }" > src/main.rs
RUN apt-get update
RUN apt install -y pkg-config libssl-dev
RUN cargo build --release
Expand All @@ -18,5 +17,5 @@ RUN cargo build --release

# Release Stage
FROM debian:bullseye-slim AS release
COPY --from=builder /build/target/release/root /usr/local/bin
COPY --from=builder /builder/target/release/root /usr/local/bin
CMD ["/usr/local/bin/root"]
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Root is our club's backend, responsible for collecting and distributing data fro

GraphQL playground should be available at `http://localhost:8000/graphiql` as long as it's in development mode.


# Deployment
The deployed instance can be accessed at [root.amfoss.in](https://root.amfoss.in).

The `main` branch is exclusively meant for production use and commits which get merged into it will make their way into the deployed instance. Active development should occur on the `develop` branch and when sufficient stability has been achieved, they can be merged into `main`. This will kick off the deployment workflow.

Further implementation details can be found at [bedrock](https://github.com/amfoss/bedrock).

# Documentation

See the [documentation](docs/docs.md) for the API reference, database schema and other detailed documentation.
Expand All @@ -43,9 +51,9 @@ See the [documentation](docs/docs.md) for the API reference, database schema and

If you encounter a bug, please check existing issues first to avoid duplicates. If none exist, create a new issue with the following details:

* Title: Concise summary.
* Title: Concise summary.
* Description: A detailed description of the issue.
* Steps to Reproduce: If it's a bug, include steps to reproduce.
* Steps to Reproduce: If it's a bug, include steps to reproduce.
* Expected and Actual Behavior: Describe what you expected and what actually happened.

## Suggesting Features
Expand All @@ -62,6 +70,7 @@ If you'd like to fix a bug, add a feature, or improve code quality:

* Check the open issues to avoid redundancy.
* Open a draft PR if you'd like feedback on an ongoing contribution.
* **Make sure to set the `develop` branch as your pull request target**, see [Deployment](#deployment)

## Coding Standards

Expand Down
69 changes: 21 additions & 48 deletions src/graphql/mutations/streak_mutations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use async_graphql::{Context, Error, Object, Result};
use async_graphql::{Context, Object, Result};
use sqlx::PgPool;

use crate::models::status_update_streak::{StatusUpdateStreak as Streak, StreakInput};
Expand All @@ -14,29 +14,15 @@ impl StreakMutations {
async fn increment_streak(&self, ctx: &Context<'_>, input: StreakInput) -> Result<Streak> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

// Ensure at least one identifier is provided
if input.member_id.is_none() && input.discord_id.is_none() {
return Err(Error::new(
"Either `member_id` or `discord_id` must be provided.",
));
}

let mut sql = String::from("UPDATE Streaks SET current_streak = current_streak + 1, max_streak = GREATEST(max_streak, current_streak + 1) WHERE ");
if let Some(_) = input.member_id {
sql.push_str("member_id = $1");
} else if let Some(_) = input.discord_id {
sql.push_str("discord_id = $1");
}

sql.push_str(" RETURNING *");

let query = if let Some(member_id) = input.member_id {
sqlx::query_as::<_, Streak>(&sql).bind(member_id)
} else if let Some(discord_id) = input.discord_id {
sqlx::query_as::<_, Streak>(&sql).bind(discord_id)
} else {
return Err(Error::new("Invalid input."));
};
let query = sqlx::query_as::<_, Streak>(
"
INSERT INTO StatusUpdateStreak VALUES ($1, 1, 1)
ON CONFLICT (member_id) DO UPDATE SET
current_streak = StatusUpdateStreak.current_streak + 1,
max_streak = GREATEST(StatusUpdateStreak.max_streak, StatusUpdateStreak.current_streak + 1)
RETURNING *",
)
.bind(input.member_id);

let updated_streak = query.fetch_one(pool.as_ref()).await?;

Expand All @@ -46,30 +32,17 @@ impl StreakMutations {
async fn reset_streak(&self, ctx: &Context<'_>, input: StreakInput) -> Result<Streak> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

// Ensure at least one identifier is provided
if input.member_id.is_none() && input.discord_id.is_none() {
return Err(Error::new(
"Either `member_id` or `discord_id` must be provided.",
));
}

let mut sql = String::from("UPDATE Streaks SET current_streak = 0 WHERE ");

if let Some(_) = input.member_id {
sql.push_str("member_id = $1");
} else if let Some(_) = input.discord_id {
sql.push_str("discord_id = $1");
}

sql.push_str(" RETURNING *");

let query = if let Some(member_id) = input.member_id {
sqlx::query_as::<_, Streak>(&sql).bind(member_id)
} else if let Some(discord_id) = input.discord_id {
sqlx::query_as::<_, Streak>(&sql).bind(discord_id)
} else {
return Err(Error::new("Invalid input."));
};
let query = sqlx::query_as::<_, Streak>(
"
INSERT INTO StatusUpdateStreak VALUES ($1, 0, 0)
ON CONFLICT (member_id) DO UPDATE
SET current_streak = CASE
WHEN StatusUpdateStreak.current_streak > 0 THEN 0
ELSE StatusUpdateStreak.current_streak - 1
END
RETURNING *",
)
.bind(input.member_id);

let updated_streak = query.fetch_one(pool.as_ref()).await?;
Ok(updated_streak)
Expand Down
3 changes: 1 addition & 2 deletions src/models/status_update_streak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ pub struct StatusUpdateStreakInfo {
/// This struct is used to deserialize the input recieved for mutations on StatusUpdateStreak.
#[derive(InputObject)]
pub struct StreakInput {
pub member_id: Option<i32>,
pub discord_id: Option<String>,
pub member_id: i32,
}