Skip to content
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

OmniError, getting users/roles by id/handle #41

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
40 changes: 31 additions & 9 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ url = "2.5.2"
sqlx = { version = "0.8", features = ["runtime-tokio", "postgres", "uuid"] }
dotenvy = "0.15.7"
uuid = { version = "1.11.0", features = ["serde", "v7"] }
thiserror = "2.0.9"
serde_json = "1.0.134"
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use tokio::net::TcpListener;
use tower_http::cors::{Any, CorsLayer};
use tracing::error;
mod database;
mod omni_error;
mod routes;
mod setup;
mod users;
Expand Down
9 changes: 9 additions & 0 deletions src/omni_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[derive(thiserror::Error, Debug)]
pub enum OmniError {
#[error("sqlx::Error: {0}")]
SqlxError(#[from] sqlx::Error),
#[error("users::photourl::PhotoUrlError: {0}")]
PhotoUrlError(#[from] crate::users::photourl::PhotoUrlError),
#[error("serde_json::Error: {0}")]
SerdeJsonError(#[from] serde_json::Error),
}
89 changes: 86 additions & 3 deletions src/users/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::omni_error::OmniError;
use permissions::Permission;
use photourl::PhotoUrl;
use roles::Role;
use serde_json::Error as JsonError;
use sqlx::{Pool, Postgres};
use uuid::Uuid;

pub mod permissions;
Expand All @@ -10,7 +13,7 @@ pub mod roles;
pub struct User {
pub id: Uuid,
pub handle: String,
pub profilepicture: Option<PhotoUrl>,
pub profile_picture: Option<PhotoUrl>,
}

pub struct TournamentUser {
Expand All @@ -26,9 +29,70 @@ impl User {
User {
id: Uuid::max(),
handle: String::from("admin"),
profilepicture: None,
profile_picture: None,
}
}
// ---------- DATABASE ----------
pub async fn get_by_id(id: Uuid, pool: &Pool<Postgres>) -> Result<User, OmniError> {
let user =
sqlx::query!("SELECT handle, pictureLink FROM users WHERE id = $1", id)
.fetch_one(pool)
.await?;

Ok(User {
id,
handle: user.handle,
profile_picture: match user.picturelink {
Some(url) => Some(PhotoUrl::new(&url)?),
None => None,
},
})
}
pub async fn get_by_handle(
handle: &str,
pool: &Pool<Postgres>,
) -> Result<User, OmniError> {
let user = sqlx::query!(
"SELECT id, pictureLink FROM users WHERE handle = $1",
handle
)
.fetch_one(pool)
.await?;

Ok(User {
id: user.id,
handle: handle.to_string(),
profile_picture: match user.picturelink {
Some(url) => Some(PhotoUrl::new(&url)?),
None => None,
},
})
}
// ---------- DATABASE HELPERS ----------
async fn get_roles(
&self,
tournament: Uuid,
pool: &Pool<Postgres>,
) -> Result<Vec<Role>, OmniError> {
let roles = sqlx::query!(
"SELECT roles FROM roles WHERE userId = $1 AND tournamentId = $2",
self.id,
tournament
)
.fetch_one(pool)
.await?
.roles;

let vec = match roles {
Some(vec) => vec
.iter()
.map(|role| serde_json::from_str(role.as_str()))
.collect::<Result<Vec<Role>, JsonError>>()?,
None => vec![],
};

Ok(vec)
}
}

impl TournamentUser {
Expand All @@ -37,6 +101,25 @@ impl TournamentUser {
.iter()
.any(|role| role.get_role_permissions().contains(&permission))
}
// ---------- DATABASE ----------
pub async fn get_by_id(
user: Uuid,
tournament: Uuid,
pool: &Pool<Postgres>,
) -> Result<TournamentUser, OmniError> {
let user = User::get_by_id(user, pool).await?;
let roles = user.get_roles(tournament, pool).await?;
Ok(TournamentUser { user, roles })
}
pub async fn get_by_handle(
handle: &str,
tournament: Uuid,
pool: &Pool<Postgres>,
) -> Result<TournamentUser, OmniError> {
let user = User::get_by_handle(handle, pool).await?;
let roles = user.get_roles(tournament, pool).await?;
Ok(TournamentUser { user, roles })
}
}

#[test]
Expand All @@ -45,7 +128,7 @@ fn construct_tournament_user() {
user: User {
id: Uuid::now_v7(),
handle: String::from("some_org"),
profilepicture: Some(
profile_picture: Some(
PhotoUrl::new("https://i.imgur.com/hbrb2U0.png").unwrap(),
),
},
Expand Down
3 changes: 2 additions & 1 deletion src/users/roles.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use serde::Deserialize;
use strum::VariantArray;

use super::permissions::Permission;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Deserialize)]
pub enum Role {
Admin,
Organizer,
Expand Down
Loading