Skip to content

Commit

Permalink
perf: Improve error handling for count method
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Jul 15, 2022
1 parent ee88e4b commit 73b37e0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn handle<B: std::marker::Send>(
let Path(ID { server_id, .. }) = req.extract::<Path<ID>>().await.unwrap();
let user = req.extensions().get::<User>().unwrap();

let exists = Member::count(&format!("id = {} AND server_id = {}", user.id, server_id)).await;
let exists = Member::count(&format!("id = {} AND server_id = {}", user.id, server_id)).await?;

if exists == 0 {
return Err(Error::UnknownServer);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/invites/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub async fn join(Extension(user): Extension<User>, Path(code): Path<String>) ->
return Err(Error::MissingAccess);
}

let count = Member::count(&format!("server_id = {}", server_id)).await;
let count = Member::count(&format!("server_id = {}", server_id)).await?;

if count > *MAX_SERVER_MEMBERS {
if count >= *MAX_SERVER_MEMBERS {
return Err(Error::MaximumChannels);
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/servers/channels/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub async fn create(
.await?
.has(Permissions::MANAGE_CHANNELS)?;

let count = Channel::count(&format!("server_id = {}", server_id)).await;
let count = Channel::count(&format!("server_id = {}", server_id)).await?;

if count > *MAX_SERVER_CHANNELS {
if count >= *MAX_SERVER_CHANNELS {
return Err(Error::MaximumChannels);
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/servers/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub async fn create(
Extension(user): Extension<User>,
ValidatedJson(data): ValidatedJson<CreateServerOptions>,
) -> Result<Json<Server>> {
let count = Member::count(&format!("id = {}", user.id)).await;
let count = Member::count(&format!("id = {}", user.id)).await?;

if count > *MAX_SERVERS {
if count >= *MAX_SERVERS {
return Err(Error::MaximumServers);
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/servers/roles/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub async fn create(
.await?
.has(Permissions::MANAGE_ROLES)?;

let count = Role::count(&format!("server_id = {}", server_id)).await;
let count = Role::count(&format!("server_id = {}", server_id)).await?;

if count > *MAX_SERVER_ROLES {
if count >= *MAX_SERVER_ROLES {
return Err(Error::MaximumRoles);
}

Expand Down
9 changes: 4 additions & 5 deletions src/structures/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ use sqlx::{Encode, Postgres, Type};

#[async_trait]
pub trait Base: Model<Postgres> {
async fn count(filter: &str) -> u64 {
sqlx::query(&format!(
async fn count(filter: &str) -> Result<u64, sqlx::Error> {
Ok(sqlx::query(&format!(
"SELECT COUNT(*) FROM {} WHERE {}",
Self::table_name(),
filter
))
.execute(pool())
.await
.unwrap()
.rows_affected()
.await?
.rows_affected())
}

async fn save(self) -> Result<Self, ormlite::Error> {
Expand Down

0 comments on commit 73b37e0

Please sign in to comment.