+ First pass of db backend and API handlers for badges module
It should be noted that /badge/update needs some more planning so 1 more commit is justified for it as some consideration must be taken for how individual badge permissions are going to be handled
This commit is contained in:
73
json-api/src/badges.rs
Normal file
73
json-api/src/badges.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use crate::{db, qs_param, http::set_json_body};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use mysql_async::Pool;
|
||||
use hyper::{Response, StatusCode, Body};
|
||||
use serde_json::json;
|
||||
|
||||
pub async fn new(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
||||
// Only name is really required the other two can default to white/
|
||||
let name = qs_param!(params, "badge_name", String);
|
||||
let perms = match qs_param!(params, "badge_perms", u64) {
|
||||
Some(perms) => perms,
|
||||
None => 0
|
||||
};
|
||||
let color = match qs_param!(params, "badge_color", u32) {
|
||||
Some(color) => color,
|
||||
None => 0xffffffff,
|
||||
};
|
||||
if let Some(name) = name {
|
||||
match db::badges::add(p, &name, color, perms).await {
|
||||
Ok(badge) => {
|
||||
set_json_body(response, json!({"badge": json!(badge)}));
|
||||
// TODO: add some rtc notification here
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("[HTTP][ERROR] /badge/new {}", e);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update(_p: &Pool, _response: &mut Response<Body>, _params: HashMap<String, String>) {
|
||||
/* TODO:
|
||||
* This handler could actually benefit from being split into multiple handlers
|
||||
* Concern: Permissions handling with is route could get ugly when handling updates to the
|
||||
* permissions flag in each badge which is why we may want a
|
||||
* /badge/update/color
|
||||
* /badge/update/name
|
||||
* /badge/update/permissions
|
||||
*/
|
||||
}
|
||||
|
||||
pub async fn delete(p: &Pool, response: &mut Response<Body>, params: HashMap<String, String>) {
|
||||
if let Some(id) = qs_param!(params, "badge_id", u64) {
|
||||
match db::badges::delete(p, id).await {
|
||||
Ok(id) => {
|
||||
// TODO: add rtc notification here
|
||||
println!("TODO remove me at some point {}", id);
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("[HTTP][ERROR] /badge/delete {}", e);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list(p: &Pool, response: &mut Response<Body>) {
|
||||
match db::badges::list(p).await {
|
||||
Ok(badges) => {
|
||||
set_json_body(response, json!({"badges": json!(badges)}));
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("[HTTP][ERRO] /badge/list {}", e);
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user