feat: use diesel on Channel and ChannelPermission structs

This commit is contained in:
Radical 2025-05-21 20:49:13 +02:00
parent 746949f0e5
commit da804cd436

View file

@ -1,14 +1,15 @@
use std::str::FromStr; use std::str::FromStr;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use diesel::Selectable;
use log::error; use log::error;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres, prelude::FromRow};
use uuid::Uuid; use uuid::Uuid;
use crate::Data; use crate::{Conn, Data, tables::*};
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone, Selectable)]
#[diesel(table_name = channels)]
pub struct Channel { pub struct Channel {
pub uuid: Uuid, pub uuid: Uuid,
pub guild_uuid: Uuid, pub guild_uuid: Uuid,
@ -17,7 +18,7 @@ pub struct Channel {
pub permissions: Vec<ChannelPermission>, pub permissions: Vec<ChannelPermission>,
} }
#[derive(Serialize, Clone, FromRow)] #[derive(Serialize, Clone)]
struct ChannelPermissionBuilder { struct ChannelPermissionBuilder {
role_uuid: String, role_uuid: String,
permissions: i32, permissions: i32,
@ -32,7 +33,8 @@ impl ChannelPermissionBuilder {
} }
} }
#[derive(Serialize, Deserialize, Clone, FromRow)] #[derive(Serialize, Deserialize, Clone, Selectable)]
#[diesel(table_name = channel_permissions)]
pub struct ChannelPermission { pub struct ChannelPermission {
pub role_uuid: Uuid, pub role_uuid: Uuid,
pub permissions: i32, pub permissions: i32,
@ -40,15 +42,10 @@ pub struct ChannelPermission {
impl Channel { impl Channel {
pub async fn fetch_all( pub async fn fetch_all(
pool: &Pool<Postgres>, conn: &mut Conn,
guild_uuid: Uuid, guild_uuid: Uuid,
) -> Result<Vec<Self>, HttpResponse> { ) -> Result<Vec<Self>, HttpResponse> {
let row = sqlx::query_as(&format!(
"SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}'",
guild_uuid
))
.fetch_all(pool)
.await;
if let Err(error) = row { if let Err(error) = row {
error!("{}", error); error!("{}", error);