2020-12-12 13:32:06 +01:00
|
|
|
use actix_web::{web, HttpResponse};
|
2021-05-10 20:24:14 +02:00
|
|
|
use chrono::{DateTime, Utc};
|
2021-06-23 12:18:34 +02:00
|
|
|
use log::debug;
|
2021-05-31 16:03:39 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-12-12 13:32:06 +01:00
|
|
|
|
2021-06-15 16:02:49 +02:00
|
|
|
use super::{IndexParam, UpdateStatusResponse};
|
2020-12-22 14:02:41 +01:00
|
|
|
use crate::error::ResponseError;
|
2021-06-24 15:33:21 +02:00
|
|
|
use crate::extractors::authentication::{policies::*, GuardedData};
|
2021-03-15 18:11:10 +01:00
|
|
|
use crate::Data;
|
2020-12-12 13:32:06 +01:00
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
2021-06-24 15:33:21 +02:00
|
|
|
cfg.service(
|
|
|
|
web::resource("indexes")
|
|
|
|
.route(web::get().to(list_indexes))
|
|
|
|
.route(web::post().to(create_index)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/indexes/{index_uid}")
|
|
|
|
.route(web::get().to(get_index))
|
|
|
|
.route(web::put().to(update_index))
|
|
|
|
.route(web::delete().to(delete_index)),
|
|
|
|
)
|
2021-06-24 19:02:28 +02:00
|
|
|
.service(
|
2021-06-29 15:25:18 +02:00
|
|
|
web::resource("/indexes/{index_uid}/updates").route(web::get().to(get_all_updates_status)),
|
2021-06-24 15:33:21 +02:00
|
|
|
)
|
2021-06-24 19:02:28 +02:00
|
|
|
.service(
|
|
|
|
web::resource("/indexes/{index_uid}/updates/{update_id}")
|
2021-06-29 15:25:18 +02:00
|
|
|
.route(web::get().to(get_update_status)),
|
2021-06-24 15:33:21 +02:00
|
|
|
);
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct IndexCreateRequest {
|
2021-02-18 17:48:37 +01:00
|
|
|
uid: String,
|
2020-12-12 13:32:06 +01:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct UpdateIndexRequest {
|
2021-03-15 14:43:47 +01:00
|
|
|
uid: Option<String>,
|
2020-12-12 13:32:06 +01:00
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:43:49 +02:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UpdateIndexResponse {
|
|
|
|
name: String,
|
|
|
|
uid: String,
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
updated_at: DateTime<Utc>,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:33:21 +02:00
|
|
|
async fn list_indexes(data: GuardedData<Private, Data>) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let indexes = data.list_indexes().await?;
|
|
|
|
debug!("returns: {:?}", indexes);
|
|
|
|
Ok(HttpResponse::Ok().json(indexes))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_index(
|
|
|
|
data: GuardedData<Private, Data>,
|
|
|
|
body: web::Json<IndexCreateRequest>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let body = body.into_inner();
|
|
|
|
let meta = data.create_index(body.uid, body.primary_key).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(meta))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_index(
|
|
|
|
data: GuardedData<Private, Data>,
|
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
|
|
|
let meta = data.index(path.index_uid.clone()).await?;
|
|
|
|
debug!("returns: {:?}", meta);
|
|
|
|
Ok(HttpResponse::Ok().json(meta))
|
|
|
|
}
|
|
|
|
|
2020-12-12 13:32:06 +01:00
|
|
|
async fn update_index(
|
2021-06-24 15:33:21 +02:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-02-09 11:26:52 +01:00
|
|
|
path: web::Path<IndexParam>,
|
|
|
|
body: web::Json<UpdateIndexRequest>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("called with params: {:?}", body);
|
2021-03-15 16:52:05 +01:00
|
|
|
let body = body.into_inner();
|
2021-06-15 16:02:49 +02:00
|
|
|
let meta = data
|
2021-03-15 18:11:10 +01:00
|
|
|
.update_index(path.into_inner().index_uid, body.primary_key, body.uid)
|
2021-06-15 16:02:49 +02:00
|
|
|
.await?;
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("returns: {:?}", meta);
|
2021-06-15 16:02:49 +02:00
|
|
|
Ok(HttpResponse::Ok().json(meta))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_index(
|
2021-06-24 15:33:21 +02:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-02-15 10:53:21 +01:00
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-15 16:02:49 +02:00
|
|
|
data.delete_index(path.index_uid.clone()).await?;
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct UpdateParam {
|
2021-01-28 17:20:51 +01:00
|
|
|
index_uid: String,
|
|
|
|
update_id: u64,
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_update_status(
|
2021-06-24 15:33:21 +02:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-01-28 17:20:51 +01:00
|
|
|
path: web::Path<UpdateParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-03-15 16:52:05 +01:00
|
|
|
let params = path.into_inner();
|
2021-06-15 16:02:49 +02:00
|
|
|
let meta = data
|
2021-03-15 18:11:10 +01:00
|
|
|
.get_update_status(params.index_uid, params.update_id)
|
2021-06-15 16:02:49 +02:00
|
|
|
.await?;
|
|
|
|
let meta = UpdateStatusResponse::from(meta);
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("returns: {:?}", meta);
|
2021-06-15 16:02:49 +02:00
|
|
|
Ok(HttpResponse::Ok().json(meta))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_all_updates_status(
|
2021-06-24 15:33:21 +02:00
|
|
|
data: GuardedData<Private, Data>,
|
2021-01-28 18:32:24 +01:00
|
|
|
path: web::Path<IndexParam>,
|
2020-12-12 13:32:06 +01:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2021-06-15 16:02:49 +02:00
|
|
|
let metas = data.get_updates_status(path.into_inner().index_uid).await?;
|
|
|
|
let metas = metas
|
|
|
|
.into_iter()
|
|
|
|
.map(UpdateStatusResponse::from)
|
|
|
|
.collect::<Vec<_>>();
|
2021-04-29 19:31:58 +02:00
|
|
|
|
2021-06-23 12:18:34 +02:00
|
|
|
debug!("returns: {:?}", metas);
|
2021-06-15 16:02:49 +02:00
|
|
|
Ok(HttpResponse::Ok().json(metas))
|
2020-12-12 13:32:06 +01:00
|
|
|
}
|