Skip to content
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
5 changes: 5 additions & 0 deletions migrations/20260310140619_create_member_roles.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Add member_roles table to support multiple roles per member
CREATE TABLE MemberRoles (
discord_id VARCHAR(255) PRIMARY KEY, -- Use the Discord ID as the key
roles TEXT[] NOT NULL -- Store roles as a PostgreSQL Array
);
26 changes: 26 additions & 0 deletions src/graphql/mutations/member_mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,30 @@ impl MemberMutations {

Ok(member)
}

/// Save the roles of a member in the database
#[graphql(name = "saveMemberRoles", guard = "AuthGuard")]
async fn save_member_roles(
&self,
ctx: &Context<'_>,
#[graphql(name = "discordId")] discord_id: String,
roles: Vec<String>,
) -> Result<bool> {
let pool = ctx.data::<Arc<PgPool>>()?;

sqlx::query(
r#"
INSERT INTO MemberRoles (discord_id, roles)
VALUES ($1, $2)
ON CONFLICT (discord_id)
DO UPDATE SET roles = EXCLUDED.roles
"#,
)
.bind(discord_id)
.bind(roles)
.execute(pool.as_ref())
.await?;

Ok(true)
}
}
18 changes: 18 additions & 0 deletions src/graphql/queries/member_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ impl MemberQueries {
// The AuthGuard ensures that the user is authenticated, so we can unwrap here.
Ok(auth.user.clone().unwrap())
}

/// Fetch the roles of a member from the database
#[graphql(guard = "AuthGuard")]
async fn member_roles(
&self,
ctx: &Context<'_>,
#[graphql(name = "discordId")] discord_id: String,
) -> Result<Option<Vec<String>>> {
let pool = ctx.data::<Arc<PgPool>>()?;

let roles: Option<Vec<String>> =
sqlx::query_scalar("SELECT roles FROM MemberRoles WHERE discord_id = $1")
.bind(discord_id)
.fetch_optional(pool.as_ref())
.await?;

Ok(roles)
}
}

#[Object]
Expand Down
Loading