diff --git a/server/src/channels.rs b/server/src/channels.rs index f3e53b5..2b9b108 100644 --- a/server/src/channels.rs +++ b/server/src/channels.rs @@ -5,28 +5,168 @@ use diesel::{self, prelude::*}; use rocket_contrib::json::Json; use std::vec::Vec; -use crate::models::{Channel, VOICE_CHANNEL}; -use crate::DBConn; -use crate::schema; +use crate::models::{InsertableChannel, Channel, VOICE_CHANNEL, TEXT_CHANNEL}; +use crate::{DBConn, schema, payload}; +use crate::err::{self, DbError}; - -#[get("/voice")] -pub fn get_voice_channels(conn: DBConn) -> Json> { - use schema::channels::dsl::*; - - let result = channels.filter(type_.eq(VOICE_CHANNEL)).load(&conn.0); - if let Ok(data) = result { - Json(data) - } - else { - let bust = Channel { +macro_rules! busted_channels_response { + () => { + Json(vec![Channel { id: -1, name: "".to_string(), permissions: 0, limit: 0, type_: 0, // the other fields are bs this is the only one that really mattesr - }; - Json(vec![bust]) + }]) } -} \ No newline at end of file +} + + +// TODO: fix the return type of this to data thats verifiable +#[post("/create////")] +pub fn insert_new_channel(qname: String, perms: i32, lim: i32, ctype: i32, conn: DBConn) -> + DbError, err::DbResponse> { + use schema::channels::dsl::*; + + let chan = InsertableChannel { + name: qname, + permissions: perms, + limit: lim, + type_: ctype, + }; + let result = diesel::insert_into(channels) + .values(&chan) + .execute(&conn.0); + + match result { + Ok(_d) => Ok(Json(payload::ChannelIndexed{name:chan.name, ctype:chan.type_})), + Err(_e) => Err(err::DbResponse{err_msg:"Could not create channel"}) + } +} + +#[get("/voice")] +pub fn get_voice_channels(conn: DBConn) -> DbError>, err::DbResponse> { + use schema::channels::dsl::*; + + let result = channels.filter(type_.eq(VOICE_CHANNEL)).load(&conn.0); + if let Ok(data) = result { + Ok(Json(data)) + } + else { + Err(err::DbResponse{err_msg:"Could not query voice channels"}) + } +} + + +#[get("/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 { + Ok(Json(data)) + } + else { + Err(err::DbResponse{err_msg:"Could not query text channels"}) + } +} + +#[cfg(test)] +mod channel_tests { + use super::*; + use rocket; + use rocket::local::Client; + use rocket::http::Status; + use serde_json::from_str; + + macro_rules! rocket_inst { + ($func:expr) => { + rocket::ignite() + .mount("/channels", routes![$func]) + .attach(DBConn::fairing()) + } + } + + #[test] + fn generate_channels_test() { + use diesel::mysql::MysqlConnection; + use schema::channels::dsl::*; + let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat") + .unwrap(); + + let app = rocket_inst!(insert_new_channel); + let client = Client::new(app).expect("bad rocket instance"); + + let text_names = vec!["tone", "ttwo", "tthre"]; + for i in text_names.iter() { + let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, TEXT_CHANNEL); + let mut response = client.post(uri).dispatch(); + + assert_eq!(response.status(), Status::Ok); + + let body = response.body_string().unwrap(); + let data: crate::payload::ChannelIndexed = from_str(&body).unwrap(); + assert_eq!(data.name, i.to_string()); + } + + let voice_names = vec!["voiceone", "voicetwo", "voicethre"]; + for i in voice_names.iter() { + let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, VOICE_CHANNEL); + let mut response = client.post(uri).dispatch(); + + assert_eq!(response.status(), Status::Ok); + + let body = response.body_string().unwrap(); + let data: crate::payload::ChannelIndexed = from_str(&body).unwrap(); + assert_eq!(data.name, i.to_string()); + } + let _ = diesel::delete(channels).filter(id.is_not_null()) + .execute(&conn) + .expect("bro what the hell delete * from channels where id is not null;"); + } + + fn generate_dummy_channels() { + let app = rocket_inst!(insert_new_channel); + let client = Client::new(app).expect("bad rocket instance"); + + let text_names = vec!["tone", "ttwo", "tthre"]; + for i in text_names.iter() { + let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, TEXT_CHANNEL); + let mut response = client.post(uri).dispatch(); + + assert_eq!(response.status(), Status::Ok); + + let body = response.body_string().unwrap(); + let data: crate::payload::ChannelIndexed = from_str(&body).unwrap(); + assert_eq!(data.name, i.to_string()); + } + + let voice_names = vec!["voiceone", "voicetwo", "voicethre"]; + for i in voice_names.iter() { + let uri = format!("/channels/create/{}/{}/{}/{}", i, 0, 0, VOICE_CHANNEL); + let mut response = client.post(uri).dispatch(); + + assert_eq!(response.status(), Status::Ok); + + let body = response.body_string().unwrap(); + let data: crate::payload::ChannelIndexed = from_str(&body).unwrap(); + assert_eq!(data.name, i.to_string()); + } + } + + #[test] + fn test_text_channels_get() { + generate_dummy_channels(); + let app = rocket_inst!(get_voice_channels); + + let client = Client::new(app).expect("bad rocket instance"); + let mut response = client.get("/channels/text").dispatch(); + let _body: String = response.body_string().unwrap(); + + assert_eq!(response.status(), Status::Ok); + assert_ne!(response.status(), Status::NotFound); + + println!("succ me") + } +}