feat: add fetch_one() function to Channel struct

This commit is contained in:
Radical 2025-05-07 23:46:40 +02:00
parent 71f0cc14be
commit 7ee500bf10

View file

@ -9,14 +9,14 @@ use log::error;
mod uuid; mod uuid;
#[derive(Serialize, FromRow)] #[derive(Serialize, Clone, FromRow)]
struct ChannelPermission { struct ChannelPermission {
role_uuid: String, role_uuid: String,
permissions: i32 permissions: i32
} }
#[derive(Serialize)] #[derive(Serialize, Clone)]
struct Channel { pub struct Channel {
uuid: String, uuid: String,
name: String, name: String,
description: Option<String>, description: Option<String>,
@ -64,6 +64,37 @@ impl Channel {
Ok(channels?) Ok(channels?)
} }
pub async fn fetch_one(pool: &Pool<Postgres>, guild_uuid: Uuid, channel_uuid: Uuid) -> Result<Self, HttpResponse> {
let row = sqlx::query_as(&format!("SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}' AND uuid = '{}'", guild_uuid, channel_uuid))
.fetch_one(pool)
.await;
if let Err(error) = row {
error!("{}", error);
return Err(HttpResponse::InternalServerError().finish())
}
let (uuid, name, description): (String, String, Option<String>) = row.unwrap();
let row = sqlx::query_as(&format!("SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}' AND uuid = '{}'", guild_uuid, channel_uuid))
.fetch_all(pool)
.await;
if let Err(error) = row {
error!("{}", error);
return Err(HttpResponse::InternalServerError().finish())
}
Ok(Self {
uuid,
name,
description,
permissions: row.unwrap(),
})
}
} }
#[get("{uuid}/channels")] #[get("{uuid}/channels")]