Added documentation to current code
Explaining why things are being done
This commit is contained in:
parent
4f71e566c3
commit
8d149014e1
@ -11,6 +11,7 @@ enum ChannelType {
|
||||
}
|
||||
|
||||
impl ChannelType {
|
||||
// These funcs are mainly here to help translation from mysql
|
||||
fn from_i32(x: i32) -> ChannelType {
|
||||
match x {
|
||||
1 => ChannelType::Voice,
|
||||
@ -28,6 +29,7 @@ impl ChannelType {
|
||||
}
|
||||
}
|
||||
|
||||
// Primary way of interpretting sql data on our channels table
|
||||
struct Channel {
|
||||
id: i32,
|
||||
name: String,
|
||||
@ -36,6 +38,10 @@ struct Channel {
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
/*
|
||||
* When our sql library queries things we generally get back tuples rather reasily
|
||||
* we can use this method to get something that makes more sense
|
||||
*/
|
||||
fn from_tup(tup: (i32, String, String, i32)) -> Channel {
|
||||
Channel {
|
||||
id: tup.0,
|
||||
@ -44,6 +50,16 @@ impl Channel {
|
||||
kind: ChannelType::from_i32(tup.3)
|
||||
}
|
||||
}
|
||||
/*
|
||||
* When responding with some channel data to the client we use json
|
||||
* this itemizes a single struct as the following(without the pretty output)
|
||||
* {
|
||||
* "id": id<i32>,
|
||||
* "name": "<some name here>",
|
||||
* "description": "<description here>",
|
||||
* "kind": kind<i32>
|
||||
* }
|
||||
*/
|
||||
fn as_json_str(&self) -> String {
|
||||
let mut base = String::from("{");
|
||||
base.push_str(&format!("\"id\":{},", self.id));
|
||||
@ -55,6 +71,10 @@ impl Channel {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Forwarding SQL errors as we can handle those error in caller site for this leaf function
|
||||
* On success we back a Vec<Channel> which we can JSON'ify later and use as a response
|
||||
*/
|
||||
async fn get_channels_vec(conn: Conn) -> Result<Vec<Channel>, Error> {
|
||||
let rows_db = conn.prep_exec(r"SELECT * FROM channels", ()).await?;
|
||||
let (_, rows) = rows_db.map_and_drop(|row| {
|
||||
@ -66,6 +86,11 @@ async fn get_channels_vec(conn: Conn) -> Result<Vec<Channel>, Error> {
|
||||
}
|
||||
|
||||
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||
/*
|
||||
* Primary dispatcher for dealing with the CHANNELS_LIST route
|
||||
* For the most part this function will have a lot more error handling as it
|
||||
* should know what kind of issues its child functions will have
|
||||
*/
|
||||
if let Ok(conn) = pool.get_conn().await {
|
||||
match get_channels_vec(conn).await {
|
||||
Ok(chans) => {
|
||||
@ -73,6 +98,9 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||
response.headers_mut().insert("Content-Type",
|
||||
HeaderValue::from_static("application/json"));
|
||||
|
||||
// At this point we build the content of our response body
|
||||
// which is a json payload hence why there is weird string manipulation
|
||||
// because we're trying to avoid dependancy issues and serializing things ourselves
|
||||
let mut new_body = String::from("{\"channels\":[");
|
||||
for chan in chans.iter() {
|
||||
let s = format!("{},", chan.as_json_str());
|
||||
@ -88,4 +116,7 @@ pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
@ -24,7 +24,6 @@ use auth::AuthReason;
|
||||
|
||||
mod routes;
|
||||
mod invites;
|
||||
mod sql_traits;
|
||||
mod channels;
|
||||
|
||||
fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> {
|
||||
|
Loading…
Reference in New Issue
Block a user