diff --git a/json-api/db/src/channels.rs b/json-api/db/src/channels.rs index 1081210..961eaf4 100644 --- a/json-api/db/src/channels.rs +++ b/json-api/db/src/channels.rs @@ -1,9 +1,9 @@ -use mysql_async::{params, Pool, Conn}; +use mysql_async::{params, Pool}; 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::{sql_err, no_conn, Response}; +use crate::{Channel, UBigInt, Integer}; +use crate::{sql_err, Response}; use rand::RngCore; @@ -13,36 +13,23 @@ pub const TEXT_CHANNEL: Integer = 2; pub const MAX_NAME_LEN: usize = 256; pub const MAX_DESCRIPTION_LEN: usize = 2048; -impl crate::Channel { +impl Channel { pub async fn filter(p: &Pool, kind: Integer) -> Result, SqlError> { //! @returns -> on success : Ok(Response::Set(Vec)) //! @throw -> on sql fail : Err(SqlError) - if kind != VOICE_CHANNEL || kind !=TEXT_CHANNEL { - let 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? - } - }; + if kind == VOICE_CHANNEL || kind ==TEXT_CHANNEL { + let mut conn = p.get_conn().await?; - let (_conn, channels) = result.map_and_drop(|row| { - let (id, name, description, kind): (UBigInt, VarChar, Option, Integer) = - mysql_async::from_row(row); - Self { - id, - name, - description, - kind + let q = "SELECT id, name, description, kind FROM channels WHERE kind = :kind"; + let params = params!{"kind" => kind}; + let channels = conn.exec_map(q, params, |(id, name, description, kind)| { + Channel { + id, name, description, kind } }).await?; - Ok(Response::Set(channels)) - } - else { + } else { return Ok(Response::RestrictedInput("Channel kind is invalid".into())); } @@ -57,10 +44,10 @@ impl crate::Channel { // bounds are literally [1, 2] 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 id = rand::rngs::OsRng.next_u64(); // generate id's randomly for channels - conn.drop_exec(q, params!{ + conn.exec_drop(q, params!{ "i" => id, "n" => name, "d" => description, @@ -79,23 +66,19 @@ impl crate::Channel { } } - pub async fn delete(p: &Pool, id: UBigInt) -> Response { + pub async fn delete(p: &Pool, id: UBigInt) -> Result, SqlError> { //! Deletes channel given UBigInt as the row key //! @param p -> SqlPool //! @param id -> UBigInt //! @return on success -> Response::Success //! @return on server failure -> Response::Other - if let Ok(conn) = p.get_conn().await { - let q = "DELETE FROM channels WHERE id = :id"; - let result: Result = - conn.drop_exec(q, params!{"id" => id}).await; - return match result { - Ok(_) => Response::Success, - Err(sql) => Response::Other(sql_err!(sql)) - } - } - else { - return Response::Other(no_conn!("Member::FromDB::delete")) + let mut conn = p.get_conn().await?; + + let q = "DELETE FROM channels WHERE id = :id"; + let result = conn.exec_drop(q, params!{"id" => id}).await; + return match result { + Ok(_) => Ok(Response::Success), + Err(sql) => Ok(Response::Other(sql_err!(sql))) } }