From 5a2a87fff729b1eaf083789e5bfcb3c57112c9d2 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 27 May 2020 21:29:57 -0700 Subject: [PATCH] /channels/list/voice + text now implemted and ready for testing --- server/src/channels.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/server/src/channels.rs b/server/src/channels.rs index 5076e67..35c6fba 100644 --- a/server/src/channels.rs +++ b/server/src/channels.rs @@ -5,11 +5,13 @@ use diesel::{self, prelude::*}; use rocket_contrib::json::Json; use std::vec::Vec; -use crate::models::{InsertableChannel, Channel, VOICE_CHANNEL, TEXT_CHANNEL}; -use crate::auth::{AuthResult, AuthErr, AuthKey}; +use crate::models::{InsertableChannel, Channel}; +use crate::auth::AuthResult; use crate::{DBConn, schema, payload}; use crate::err::{self, DbError}; +const VOICE_CHANNEL: i32 = 1; +const TEXT_CHANNEL: i32 = 2; #[post("/create////")] pub fn insert_new_channel(qname: String, perms: i32, lim: i32, ctype: i32, conn: DBConn) -> @@ -32,12 +34,23 @@ pub fn insert_new_channel(qname: String, perms: i32, lim: i32, ctype: i32, conn: } } +pub fn get_channel_by_type(conn: &MysqlConnection, ctype: i32/*short for channel type*/) -> Result, err::DbResponse> { + use schema::channels::dsl::*; + if let Ok(result) = channels.filter(type_.eq(ctype)).load(conn) { + Ok(result) + } + else { + Err(err::DbResponse{err_msg: "/channeels/list/ Could not query database"}) + } + +} + #[get("/list/voice")] pub fn get_voice_channels(conn: DBConn) -> AuthResult>, err::DbResponse> { - use schema::channels::dsl::*; - - let result = channels.filter(type_.eq(VOICE_CHANNEL)).load(&conn.0); - if let Ok(data) = result { + let db_result = get_channel_by_type(&conn.0, VOICE_CHANNEL); + // We have to rewrap the data given to us which is annoying but hey + // at least its safe.jpg + if let Ok(data) = db_result { Ok(Json(data)) } else { @@ -48,10 +61,8 @@ pub fn get_voice_channels(conn: DBConn) -> AuthResult>, err::D #[get("/list/text")] pub fn get_text_chanels(conn: DBConn) -> DbError>, err::DbResponse> { - use schema::channels::dsl::*; - - let result = channels.filter(type_.eq(TEXT_CHANNEL)).load(&conn.0); - if let Ok(data) = result { + let db_result = get_channel_by_type(&conn.0, TEXT_CHANNEL); + if let Ok(data) = db_result { Ok(Json(data)) } else {