- Removing non-result returns
+ Updating further for newest version of mysql_async
This commit is contained in:
parent
fea94bffa0
commit
54f4dd53be
@ -1,9 +1,9 @@
|
|||||||
use mysql_async::{params, Pool, Conn};
|
use mysql_async::{params, Pool};
|
||||||
use mysql_async::prelude::Queryable;
|
use mysql_async::prelude::Queryable;
|
||||||
use mysql_async::error::Error as SqlError;
|
use mysql_async::Error as SqlError;
|
||||||
|
|
||||||
use crate::{VarChar, UBigInt, Integer};
|
use crate::{Channel, UBigInt, Integer};
|
||||||
use crate::{sql_err, no_conn, Response};
|
use crate::{sql_err, Response};
|
||||||
|
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
@ -13,36 +13,23 @@ pub const TEXT_CHANNEL: Integer = 2;
|
|||||||
pub const MAX_NAME_LEN: usize = 256;
|
pub const MAX_NAME_LEN: usize = 256;
|
||||||
pub const MAX_DESCRIPTION_LEN: usize = 2048;
|
pub const MAX_DESCRIPTION_LEN: usize = 2048;
|
||||||
|
|
||||||
impl crate::Channel {
|
impl Channel {
|
||||||
pub async fn filter(p: &Pool, kind: Integer) -> Result<Response<Self>, SqlError> {
|
pub async fn filter(p: &Pool, kind: Integer) -> Result<Response<Self>, SqlError> {
|
||||||
//! @returns -> on success : Ok(Response::Set(Vec<Channel>))
|
//! @returns -> on success : Ok(Response::Set(Vec<Channel>))
|
||||||
//! @throw -> on sql fail : Err(SqlError)
|
//! @throw -> on sql fail : Err(SqlError)
|
||||||
|
|
||||||
if kind != VOICE_CHANNEL || kind !=TEXT_CHANNEL {
|
if kind == VOICE_CHANNEL || kind ==TEXT_CHANNEL {
|
||||||
let conn = p.get_conn().await?;
|
let mut conn = p.get_conn().await?;
|
||||||
let result = match kind {
|
|
||||||
0 => conn.prep_exec("SELECT id, name, description, kind FROM channels", ()).await?,
|
|
||||||
_ => {
|
|
||||||
conn.prep_exec(
|
|
||||||
"SELECT id, name, description, kind FROM channels WHERE kind = :kind",
|
|
||||||
params!{"kind" => kind}).await?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (_conn, channels) = result.map_and_drop(|row| {
|
let q = "SELECT id, name, description, kind FROM channels WHERE kind = :kind";
|
||||||
let (id, name, description, kind): (UBigInt, VarChar, Option<VarChar>, Integer) =
|
let params = params!{"kind" => kind};
|
||||||
mysql_async::from_row(row);
|
let channels = conn.exec_map(q, params, |(id, name, description, kind)| {
|
||||||
Self {
|
Channel {
|
||||||
id,
|
id, name, description, kind
|
||||||
name,
|
|
||||||
description,
|
|
||||||
kind
|
|
||||||
}
|
}
|
||||||
}).await?;
|
}).await?;
|
||||||
|
|
||||||
Ok(Response::Set(channels))
|
Ok(Response::Set(channels))
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return Ok(Response::RestrictedInput("Channel kind is invalid".into()));
|
return Ok(Response::RestrictedInput("Channel kind is invalid".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,10 +44,10 @@ impl crate::Channel {
|
|||||||
|
|
||||||
// bounds are literally [1, 2]
|
// bounds are literally [1, 2]
|
||||||
if kind == TEXT_CHANNEL || kind == VOICE_CHANNEL {
|
if kind == TEXT_CHANNEL || kind == VOICE_CHANNEL {
|
||||||
let conn = p.get_conn().await?;
|
let mut conn = p.get_conn().await?;
|
||||||
let q = "INSERT INTO channels (id, name, description, kind) VALUES (:i, :n, :d, :k)";
|
let q = "INSERT INTO channels (id, name, description, kind) VALUES (:i, :n, :d, :k)";
|
||||||
let id = rand::rngs::OsRng.next_u64(); // generate id's randomly for channels
|
let id = rand::rngs::OsRng.next_u64(); // generate id's randomly for channels
|
||||||
conn.drop_exec(q, params!{
|
conn.exec_drop(q, params!{
|
||||||
"i" => id,
|
"i" => id,
|
||||||
"n" => name,
|
"n" => name,
|
||||||
"d" => description,
|
"d" => description,
|
||||||
@ -79,23 +66,19 @@ impl crate::Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(p: &Pool, id: UBigInt) -> Response<Self> {
|
pub async fn delete(p: &Pool, id: UBigInt) -> Result<Response<Self>, SqlError> {
|
||||||
//! Deletes channel given UBigInt as the row key
|
//! Deletes channel given UBigInt as the row key
|
||||||
//! @param p -> SqlPool
|
//! @param p -> SqlPool
|
||||||
//! @param id -> UBigInt
|
//! @param id -> UBigInt
|
||||||
//! @return on success -> Response::Success
|
//! @return on success -> Response::Success
|
||||||
//! @return on server failure -> Response::Other
|
//! @return on server failure -> Response::Other
|
||||||
if let Ok(conn) = p.get_conn().await {
|
let mut conn = p.get_conn().await?;
|
||||||
|
|
||||||
let q = "DELETE FROM channels WHERE id = :id";
|
let q = "DELETE FROM channels WHERE id = :id";
|
||||||
let result: Result<Conn, SqlError> =
|
let result = conn.exec_drop(q, params!{"id" => id}).await;
|
||||||
conn.drop_exec(q, params!{"id" => id}).await;
|
|
||||||
return match result {
|
return match result {
|
||||||
Ok(_) => Response::Success,
|
Ok(_) => Ok(Response::Success),
|
||||||
Err(sql) => Response::Other(sql_err!(sql))
|
Err(sql) => Ok(Response::Other(sql_err!(sql)))
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Response::Other(no_conn!("Member::FromDB::delete"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user