surrealpatch/src/web/key.rs

176 lines
5.2 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::web::head;
use serde::Deserialize;
2021-03-29 15:43:37 +00:00
use std::collections::HashMap;
use std::str;
2020-06-29 15:36:01 +00:00
use warp::path;
use warp::Filter;
#[derive(Default, Deserialize, Debug, Clone)]
pub struct Query {
pub limit: Option<String>,
pub start: Option<String>,
}
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let base = warp::path("key");
// Set opts method
let opts = base.and(warp::options()).map(warp::reply);
// ------------------------------
// Routes for a table
// ------------------------------
// Set base path for all
let base = path!("key" / String).and(warp::path::end());
// Set select method
let select = base.and(warp::get()).and(warp::query()).and_then(select_all);
// Set create method
let create = base
.and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024 * 1)) // 1MiB
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(create_all);
// Set delete method
let delete = base.and(warp::delete()).and_then(delete_all);
// Specify route
let all = select.or(create).or(delete);
// ------------------------------
// Routes for a thing
// ------------------------------
// Set base path for one
let base = path!("key" / String / String).and(warp::path::end());
// Set select method
let select = base.and(warp::get()).and_then(select_one);
// Set create method
let create = base
.and(warp::post())
.and(warp::body::content_length_limit(1024 * 1024 * 1)) // 1MiB
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(create_one);
// Set update method
let update = base
.and(warp::put())
.and(warp::body::content_length_limit(1024 * 1024 * 1)) // 1MiB
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(update_one);
// Set modify method
let modify = base
.and(warp::patch())
.and(warp::body::content_length_limit(1024 * 1024 * 1)) // 1MiB
2020-06-29 15:36:01 +00:00
.and(warp::body::bytes())
.and_then(modify_one);
// Set delete method
let delete = base.and(warp::delete()).and_then(delete_one);
// Specify route
let one = select.or(create).or(update).or(modify).or(delete);
// ------------------------------
// All routes
// ------------------------------
// Specify route
opts.or(all).or(one).with(head::cors())
}
// ------------------------------
// Routes for a table
// ------------------------------
async fn select_all(table: String, query: Query) -> Result<impl warp::Reply, warp::Rejection> {
let sql = format!(
2021-03-29 15:43:37 +00:00
"SELECT * FROM type::table($table) LIMIT {l} START {s}",
2020-06-29 15:36:01 +00:00
l = query.limit.unwrap_or(String::from("100")),
s = query.start.unwrap_or(String::from("0")),
);
2021-03-29 15:43:37 +00:00
let mut var = HashMap::new();
var.insert("table", table);
let res = crate::dbs::execute(sql.as_str(), Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn create_all(
table: String,
body: bytes::Bytes,
) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "CREATE type::table($table) CONTENT $data";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("data", str::from_utf8(&body).unwrap().to_owned());
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn delete_all(table: String) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "DELETE type::table($table)";
let mut var = HashMap::new();
var.insert("table", table);
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
// ------------------------------
// Routes for a thing
// ------------------------------
async fn select_one(table: String, id: String) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "SELECT * FROM type::thing($table, $id)";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("id", id);
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn create_one(
table: String,
id: String,
body: bytes::Bytes,
) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "CREATE type::thing($table, $id) CONTENT $data";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("id", id);
var.insert("data", str::from_utf8(&body).unwrap().to_owned());
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn update_one(
table: String,
id: String,
body: bytes::Bytes,
) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "UPDATE type::thing($table, $id) CONTENT $data";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("id", id);
var.insert("data", str::from_utf8(&body).unwrap().to_owned());
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn modify_one(
table: String,
id: String,
body: bytes::Bytes,
) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "UPDATE type::thing($table, $id) MERGE $data";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("id", id);
var.insert("data", str::from_utf8(&body).unwrap().to_owned());
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}
async fn delete_one(table: String, id: String) -> Result<impl warp::Reply, warp::Rejection> {
2021-03-29 15:43:37 +00:00
let sql = "DELETE type::thing($table, $id)";
let mut var = HashMap::new();
var.insert("table", table);
var.insert("id", id);
let res = crate::dbs::execute(sql, Some(var)).unwrap();
Ok(warp::reply::json(&res))
2020-06-29 15:36:01 +00:00
}