2020-12-12 20:32:06 +08:00
|
|
|
use actix_web::{delete, get, post, put};
|
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::Data;
|
2020-12-22 21:02:41 +08:00
|
|
|
use crate::error::ResponseError;
|
2020-12-12 20:32:06 +08:00
|
|
|
use crate::helpers::Authentication;
|
|
|
|
use crate::routes::IndexParam;
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(list_indexes)
|
|
|
|
.service(get_index)
|
|
|
|
.service(create_index)
|
|
|
|
.service(update_index)
|
|
|
|
.service(delete_index)
|
|
|
|
.service(get_update_status)
|
|
|
|
.service(get_all_updates_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/indexes", wrap = "Authentication::Private")]
|
2021-02-04 00:44:20 +08:00
|
|
|
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2021-03-07 03:12:20 +08:00
|
|
|
match data.list_indexes().await {
|
2021-02-04 00:44:20 +08:00
|
|
|
Ok(indexes) => {
|
|
|
|
let json = serde_json::to_string(&indexes).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(&json))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-04 00:44:20 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}", wrap = "Authentication::Private")]
|
|
|
|
async fn get_index(
|
2021-02-04 19:34:12 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-07 03:17:58 +08:00
|
|
|
match data.index(&path.index_uid).await? {
|
2021-02-04 19:34:12 +08:00
|
|
|
Some(meta) => {
|
|
|
|
let json = serde_json::to_string(&meta).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
None => {
|
2021-02-16 22:26:13 +08:00
|
|
|
let e = format!("Index {:?} doesn't exist.", path.index_uid);
|
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-04 19:34:12 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct IndexCreateRequest {
|
2021-02-19 00:48:37 +08:00
|
|
|
uid: String,
|
2020-12-12 20:32:06 +08:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/indexes", wrap = "Authentication::Private")]
|
|
|
|
async fn create_index(
|
2021-02-08 17:47:34 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
body: web::Json<IndexCreateRequest>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-26 16:10:04 +08:00
|
|
|
match data.create_index(&body.uid, body.primary_key.clone()).await {
|
2021-02-08 17:47:34 +08:00
|
|
|
Ok(meta) => {
|
|
|
|
let json = serde_json::to_string(&meta).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-08 17:47:34 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct UpdateIndexRequest {
|
|
|
|
name: Option<String>,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct UpdateIndexResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
updated_at: DateTime<Utc>,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/indexes/{index_uid}", wrap = "Authentication::Private")]
|
|
|
|
async fn update_index(
|
2021-02-09 18:26:52 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<UpdateIndexRequest>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-12 21:48:43 +08:00
|
|
|
match data.update_index(&path.index_uid, body.primary_key.as_ref(), body.name.as_ref()).await {
|
2021-02-09 18:26:52 +08:00
|
|
|
Ok(meta) => {
|
|
|
|
let json = serde_json::to_string(&meta).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
2021-02-16 22:26:13 +08:00
|
|
|
Err(e) => {
|
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
|
|
|
}
|
2021-02-09 18:26:52 +08:00
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/indexes/{index_uid}", wrap = "Authentication::Private")]
|
|
|
|
async fn delete_index(
|
2021-02-15 17:53:21 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-02-15 17:53:21 +08:00
|
|
|
match data.delete_index(path.index_uid.clone()).await {
|
|
|
|
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-02-15 17:53:21 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct UpdateParam {
|
2021-01-29 00:20:51 +08:00
|
|
|
index_uid: String,
|
|
|
|
update_id: u64,
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get(
|
|
|
|
"/indexes/{index_uid}/updates/{update_id}",
|
|
|
|
wrap = "Authentication::Private"
|
|
|
|
)]
|
|
|
|
async fn get_update_status(
|
2021-01-29 00:20:51 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<UpdateParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-06 17:51:52 +08:00
|
|
|
let result = data.get_update_status(&path.index_uid, path.update_id).await;
|
2021-01-29 00:20:51 +08:00
|
|
|
match result {
|
|
|
|
Ok(Some(meta)) => {
|
|
|
|
let json = serde_json::to_string(&meta).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
2021-03-11 01:04:20 +08:00
|
|
|
let e = format!("update {} for index {:?} doesn't exists.", path.update_id, path.index_uid);
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-01-29 00:20:51 +08:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-01-29 00:20:51 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")]
|
|
|
|
async fn get_all_updates_status(
|
2021-01-29 01:32:24 +08:00
|
|
|
data: web::Data<Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 20:32:06 +08:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-06 01:34:04 +08:00
|
|
|
let result = data.get_updates_status(&path.index_uid).await;
|
2021-01-29 01:32:24 +08:00
|
|
|
match result {
|
|
|
|
Ok(metas) => {
|
|
|
|
let json = serde_json::to_string(&metas).unwrap();
|
|
|
|
Ok(HttpResponse::Ok().body(json))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-02-16 22:26:13 +08:00
|
|
|
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })))
|
2021-01-29 01:32:24 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 20:32:06 +08:00
|
|
|
}
|