2022-07-27 13:05:28 +00:00
|
|
|
use crate::cli::CF;
|
2022-07-04 00:01:24 +00:00
|
|
|
use crate::dbs::DB;
|
2022-01-19 11:35:35 +00:00
|
|
|
use crate::err::Error;
|
2022-02-09 15:55:04 +00:00
|
|
|
use crate::net::output;
|
2022-05-10 07:29:25 +00:00
|
|
|
use crate::net::session;
|
2022-01-13 17:36:41 +00:00
|
|
|
use bytes::Bytes;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::Deserialize;
|
2021-03-29 15:43:37 +00:00
|
|
|
use std::str;
|
2022-02-22 14:16:50 +00:00
|
|
|
use surrealdb::sql::Value;
|
|
|
|
use surrealdb::Session;
|
2020-06-29 15:36:01 +00:00
|
|
|
use warp::path;
|
|
|
|
use warp::Filter;
|
|
|
|
|
2022-05-10 00:07:03 +00:00
|
|
|
const MAX: u64 = 1024 * 16; // 16 KiB
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[derive(Default, Deserialize, Debug, Clone)]
|
2022-05-10 00:07:03 +00:00
|
|
|
struct Query {
|
2020-06-29 15:36:01 +00:00
|
|
|
pub limit: Option<String>,
|
|
|
|
pub start: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
2022-01-13 17:36:41 +00:00
|
|
|
// ------------------------------
|
|
|
|
// Routes for OPTIONS
|
|
|
|
// ------------------------------
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
let base = warp::path("key");
|
|
|
|
// Set opts method
|
|
|
|
let opts = base.and(warp::options()).map(warp::reply);
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Routes for a table
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
// Set select method
|
2022-05-10 00:07:03 +00:00
|
|
|
let select = warp::any()
|
|
|
|
.and(warp::get())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String).and(warp::path::end()))
|
|
|
|
.and(warp::query())
|
|
|
|
.and_then(select_all);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Set create method
|
2022-05-10 00:07:03 +00:00
|
|
|
let create = warp::any()
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::post())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String).and(warp::path::end()))
|
|
|
|
.and(warp::body::content_length_limit(MAX))
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(create_all);
|
|
|
|
// Set delete method
|
2022-05-10 00:07:03 +00:00
|
|
|
let delete = warp::any()
|
|
|
|
.and(warp::delete())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String).and(warp::path::end()))
|
|
|
|
.and_then(delete_all);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Specify route
|
|
|
|
let all = select.or(create).or(delete);
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Routes for a thing
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
// Set select method
|
2022-05-10 00:07:03 +00:00
|
|
|
let select = warp::any()
|
|
|
|
.and(warp::get())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String / String).and(warp::path::end()))
|
|
|
|
.and_then(select_one);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Set create method
|
2022-05-10 00:07:03 +00:00
|
|
|
let create = warp::any()
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::post())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String / String).and(warp::path::end()))
|
|
|
|
.and(warp::body::content_length_limit(MAX))
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(create_one);
|
|
|
|
// Set update method
|
2022-05-10 00:07:03 +00:00
|
|
|
let update = warp::any()
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::put())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String / String).and(warp::path::end()))
|
|
|
|
.and(warp::body::content_length_limit(MAX))
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(update_one);
|
|
|
|
// Set modify method
|
2022-05-10 00:07:03 +00:00
|
|
|
let modify = warp::any()
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::patch())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String / String).and(warp::path::end()))
|
|
|
|
.and(warp::body::content_length_limit(MAX))
|
2020-06-29 15:36:01 +00:00
|
|
|
.and(warp::body::bytes())
|
|
|
|
.and_then(modify_one);
|
|
|
|
// Set delete method
|
2022-05-10 00:07:03 +00:00
|
|
|
let delete = warp::any()
|
|
|
|
.and(warp::delete())
|
2022-05-10 07:29:25 +00:00
|
|
|
.and(session::build())
|
2022-09-16 01:18:28 +00:00
|
|
|
.and(warp::header::<String>(http::header::ACCEPT.as_str()))
|
2022-05-10 00:07:03 +00:00
|
|
|
.and(path!("key" / String / String).and(warp::path::end()))
|
|
|
|
.and_then(delete_one);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Specify route
|
|
|
|
let one = select.or(create).or(update).or(modify).or(delete);
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// All routes
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
// Specify route
|
2022-07-23 12:49:26 +00:00
|
|
|
opts.or(all).or(one)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Routes for a table
|
|
|
|
// ------------------------------
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
async fn select_all(
|
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
table: String,
|
|
|
|
query: Query,
|
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Specify the request statement
|
2020-06-29 15:36:01 +00:00
|
|
|
let sql = format!(
|
2021-03-29 15:43:37 +00:00
|
|
|
"SELECT * FROM type::table($table) LIMIT {l} START {s}",
|
2022-03-04 16:01:32 +00:00
|
|
|
l = query.limit.unwrap_or_else(|| String::from("100")),
|
|
|
|
s = query.start.unwrap_or_else(|| String::from("0")),
|
2020-06-29 15:36:01 +00:00
|
|
|
);
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-09 15:55:04 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql.as_str(), &session, Some(vars), opt.strict).await {
|
2022-02-05 23:06:16 +00:00
|
|
|
Ok(ref res) => match output.as_ref() {
|
|
|
|
"application/json" => Ok(output::json(res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_all(
|
2022-01-13 17:36:41 +00:00
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2020-06-29 15:36:01 +00:00
|
|
|
table: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
body: Bytes,
|
2020-06-29 15:36:01 +00:00
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Convert the HTTP request body
|
2022-01-13 17:36:41 +00:00
|
|
|
let data = str::from_utf8(&body).unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Parse the request body as JSON
|
2022-02-22 14:16:50 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
Ok(data) => {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request statement
|
2022-01-13 17:36:41 +00:00
|
|
|
let sql = "CREATE type::table($table) CONTENT $data";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
2022-03-04 16:01:32 +00:00
|
|
|
String::from("data") => data,
|
2022-02-13 23:37:30 +00:00
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2022-03-25 20:31:45 +00:00
|
|
|
Err(_) => Err(warp::reject::custom(Error::Request)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 17:13:44 +00:00
|
|
|
async fn delete_all(
|
|
|
|
session: Session,
|
|
|
|
output: String,
|
|
|
|
table: String,
|
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Specify the request statement
|
2021-03-29 15:43:37 +00:00
|
|
|
let sql = "DELETE type::table($table)";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
// Routes for a thing
|
|
|
|
// ------------------------------
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
async fn select_one(
|
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
table: String,
|
|
|
|
id: String,
|
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Specify the request statement
|
2021-03-29 15:43:37 +00:00
|
|
|
let sql = "SELECT * FROM type::thing($table, $id)";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
String::from("id") => Value::from(id),
|
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_one(
|
2022-01-13 17:36:41 +00:00
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2020-06-29 15:36:01 +00:00
|
|
|
table: String,
|
|
|
|
id: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
body: Bytes,
|
2020-06-29 15:36:01 +00:00
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Convert the HTTP request body
|
2022-01-13 17:36:41 +00:00
|
|
|
let data = str::from_utf8(&body).unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Parse the request body as JSON
|
2022-02-22 14:16:50 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
Ok(data) => {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request statement
|
2022-01-13 17:36:41 +00:00
|
|
|
let sql = "CREATE type::thing($table, $id) CONTENT $data";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
String::from("id") => Value::from(id),
|
2022-03-04 16:01:32 +00:00
|
|
|
String::from("data") => data,
|
2022-02-13 23:37:30 +00:00
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2022-03-25 20:31:45 +00:00
|
|
|
Err(_) => Err(warp::reject::custom(Error::Request)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_one(
|
2022-01-13 17:36:41 +00:00
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2020-06-29 15:36:01 +00:00
|
|
|
table: String,
|
|
|
|
id: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
body: Bytes,
|
2020-06-29 15:36:01 +00:00
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Convert the HTTP request body
|
2022-01-13 17:36:41 +00:00
|
|
|
let data = str::from_utf8(&body).unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Parse the request body as JSON
|
2022-02-22 14:16:50 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
Ok(data) => {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request statement
|
2022-01-13 17:36:41 +00:00
|
|
|
let sql = "UPDATE type::thing($table, $id) CONTENT $data";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
String::from("id") => Value::from(id),
|
2022-03-04 16:01:32 +00:00
|
|
|
String::from("data") => data,
|
2022-02-13 23:37:30 +00:00
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2022-03-25 20:31:45 +00:00
|
|
|
Err(_) => Err(warp::reject::custom(Error::Request)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn modify_one(
|
2022-01-13 17:36:41 +00:00
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2020-06-29 15:36:01 +00:00
|
|
|
table: String,
|
|
|
|
id: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
body: Bytes,
|
2020-06-29 15:36:01 +00:00
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Convert the HTTP request body
|
2022-01-13 17:36:41 +00:00
|
|
|
let data = str::from_utf8(&body).unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Parse the request body as JSON
|
2022-02-22 14:16:50 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
Ok(data) => {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request statement
|
2022-01-13 17:36:41 +00:00
|
|
|
let sql = "UPDATE type::thing($table, $id) MERGE $data";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
String::from("id") => Value::from(id),
|
2022-03-04 16:01:32 +00:00
|
|
|
String::from("data") => data,
|
2022-02-13 23:37:30 +00:00
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2022-03-25 20:31:45 +00:00
|
|
|
Err(_) => Err(warp::reject::custom(Error::Request)),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
async fn delete_one(
|
|
|
|
session: Session,
|
2022-01-14 17:13:44 +00:00
|
|
|
output: String,
|
2022-01-13 17:36:41 +00:00
|
|
|
table: String,
|
|
|
|
id: String,
|
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get the datastore reference
|
2022-05-03 20:20:36 +00:00
|
|
|
let db = DB.get().unwrap();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Get local copy of options
|
|
|
|
let opt = CF.get().unwrap();
|
|
|
|
// Specify the request statement
|
2021-03-29 15:43:37 +00:00
|
|
|
let sql = "DELETE type::thing($table, $id)";
|
2022-07-27 13:05:28 +00:00
|
|
|
// Specify the request variables
|
2022-03-07 18:14:37 +00:00
|
|
|
let vars = map! {
|
2022-02-13 23:37:30 +00:00
|
|
|
String::from("table") => Value::from(table),
|
|
|
|
String::from("id") => Value::from(id),
|
|
|
|
};
|
2022-07-27 13:05:28 +00:00
|
|
|
// Execute the query and return the result
|
|
|
|
match db.execute(sql, &session, Some(vars), opt.strict).await {
|
2022-01-14 17:13:44 +00:00
|
|
|
Ok(res) => match output.as_ref() {
|
2022-02-05 23:06:16 +00:00
|
|
|
"application/json" => Ok(output::json(&res)),
|
|
|
|
"application/cbor" => Ok(output::cbor(&res)),
|
|
|
|
"application/msgpack" => Ok(output::pack(&res)),
|
2022-09-24 23:18:03 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
2022-01-14 17:13:44 +00:00
|
|
|
},
|
2022-09-24 23:18:03 +00:00
|
|
|
// There was an error when executing the query
|
2022-02-22 14:16:50 +00:00
|
|
|
Err(err) => Err(warp::reject::custom(Error::from(err))),
|
2022-01-14 17:13:44 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|