➕ Updated target for debugger
✨ Tests now use the new flags required for /channels/create ❗ Doubled size for channel descriptions
This commit is contained in:
@@ -10,6 +10,8 @@ use crate::{sql_err, no_conn, Response};
|
||||
pub const VOICE_CHANNEL: Integer = 1;
|
||||
pub const TEXT_CHANNEL: Integer = 2;
|
||||
|
||||
pub const MAX_NAME_LEN: usize = 256;
|
||||
pub const MAX_DESCRIPTION_LEN: usize = 2048;
|
||||
|
||||
impl crate::Channel {
|
||||
pub async fn filter(p: &Pool, kind: Integer) -> Result<Response<Self>, SqlError> {
|
||||
@@ -40,45 +42,39 @@ impl crate::Channel {
|
||||
|
||||
}
|
||||
|
||||
pub async fn new(p: &Pool, name: &str, desc: &str, kind: Integer) -> Response<Self> {
|
||||
pub async fn add(p: &Pool, name: &str, description: &str, kind: Integer)
|
||||
-> Result<Response<Self>, SqlError> {
|
||||
//! @returns on success -> Response::Row<Channel>
|
||||
//! @returns on partial success -> Response::Empty
|
||||
//! @returns on failure -> Response::Other
|
||||
//! @returns on user failure -> Response::RestrictedInput(msg)
|
||||
if let Ok(conn) = p.get_conn().await {
|
||||
let q = "INSERT INTO channels (name, description, kind) VALUES (:n, :d, :k)";
|
||||
let insert_result = conn.drop_exec(q, params!{
|
||||
"n" => name,
|
||||
"d" => desc,
|
||||
"k" => kind
|
||||
}).await;
|
||||
if let Ok(conn) = insert_result {
|
||||
// This is only kosher because names are enforced as unique by sql
|
||||
let q = "SELECT id FROM channels WHERE name = :name";
|
||||
let fetch_result : Result<(Conn, Option<UBigInt>), SqlError> =
|
||||
conn.first_exec(q, params!{"name" => name}).await;
|
||||
|
||||
return match fetch_result {
|
||||
Ok((_, id_opt)) => {
|
||||
if let Some(id) = id_opt {
|
||||
Response::Row(Self {
|
||||
id,
|
||||
name: name.into(),
|
||||
description: Some(desc.into()),
|
||||
kind
|
||||
})
|
||||
}
|
||||
else { Response::Empty }
|
||||
},
|
||||
Err(_) => Response::Empty
|
||||
};
|
||||
}
|
||||
else {
|
||||
return Response::RestrictedInput(
|
||||
"Could not add channel, make sure the name is unique and not longer than 256 bytes".into());
|
||||
}
|
||||
// bounds are literally [1, 2]
|
||||
if kind == TEXT_CHANNEL || kind == VOICE_CHANNEL {
|
||||
let conn = p.get_conn().await?;
|
||||
let q = "INSERT INTO channels (name, description, kind) VALUES (:n, :d, :k)";
|
||||
let conn = conn.drop_exec(q, params!{
|
||||
"n" => name,
|
||||
"d" => description,
|
||||
"k" => kind
|
||||
}).await?;
|
||||
|
||||
let q = "SELECT id FROM channels WHERE name = :name";
|
||||
let (_, id): (_, Option<UBigInt>) =
|
||||
conn.first_exec(q, params!{"name" => name}).await?;
|
||||
|
||||
// probably safe but honestly could fail if the net goes down right here
|
||||
let id = id.unwrap();
|
||||
Ok(Response::Row(Self {
|
||||
id,
|
||||
name: name.to_string(),
|
||||
description: Some(description.to_string()),
|
||||
kind
|
||||
}))
|
||||
}
|
||||
else {
|
||||
return Ok(Response::RestrictedInput(String::from("Invalid channel type")));
|
||||
}
|
||||
return Response::Other(no_conn!("db::channels::add"))
|
||||
}
|
||||
|
||||
pub async fn delete(p: &Pool, id: UBigInt) -> Response<Self> {
|
||||
|
||||
Reference in New Issue
Block a user