➖ Removing more unused trait methods in db-lib's channel module
➕ /channels/list now takes a "type" parameter which defaults to TEXT_CHANNEL(1) ✨ Refactoring db::Channel::filter to use a more latency friendly approach ✨ db::Channel::Filter now returns Result<Response<Self>, SqlError>
This commit is contained in:
@@ -6,28 +6,33 @@ use serde_json::json;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use db::{
|
||||
self,
|
||||
common::FromDB,
|
||||
channels::Channel
|
||||
};
|
||||
use db::{ self, common::FromDB, Channel };
|
||||
|
||||
use crate::http::set_json_body;
|
||||
use crate::qs_param;
|
||||
|
||||
|
||||
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>) {
|
||||
pub async fn list_channels(pool: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
||||
/*
|
||||
* @user-params -> for now none as i don't feel like dealing with it
|
||||
* @TODO: add in a let var that actually
|
||||
*/
|
||||
return match db::channels::Channel::filter(pool, 0).await {
|
||||
db::Response::Set(channels) => {
|
||||
set_json_body(response, json!({"channels": channels}));
|
||||
},
|
||||
db::Response::Other(_msg) => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
_ => *response.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
|
||||
// Default to filtering for text channels only
|
||||
let chan_type = match qs_param!(params, "type", i32) {
|
||||
Some(ctype) => ctype,
|
||||
None => db::channels::TEXT_CHANNEL
|
||||
};
|
||||
|
||||
match db::Channel::filter(pool, chan_type).await {
|
||||
Ok(resp) => match resp {
|
||||
db::Response::Set(channels) => set_json_body(response, json!(channels)),
|
||||
_ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR
|
||||
},
|
||||
Err(e) => {
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
||||
|
||||
@@ -61,7 +61,7 @@ async fn route_dispatcher(
|
||||
(POST, routes::INVITE_CREATE) => invites::create(pool, resp, params).await,
|
||||
(GET, routes::INVITE_JOIN) => invites::join(pool, resp, params).await,
|
||||
/* CHANNELS */
|
||||
(GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp).await,
|
||||
(GET, routes::CHANNELS_LIST) => channels::list_channels(pool, resp, params).await,
|
||||
(POST, routes::CHANNELS_CREATE) => channels::create_channel(pool, resp, params).await,
|
||||
(DELETE, routes::CHANNELS_DELETE) => channels::delete_channel(pool, resp, params).await,
|
||||
/* MESSAGING */
|
||||
|
||||
Reference in New Issue
Block a user