MeiliSearch/meilisearch-http/src/routes/index.rs

141 lines
4.2 KiB
Rust
Raw Normal View History

2020-12-12 13:32:06 +01:00
use actix_web::{delete, get, post, put};
use actix_web::{web, HttpResponse};
2021-04-19 19:03:53 +02:00
use serde::Deserialize;
2020-12-12 13:32:06 +01:00
2020-12-22 14:02:41 +01:00
use crate::error::ResponseError;
2020-12-12 13:32:06 +01:00
use crate::helpers::Authentication;
use crate::routes::IndexParam;
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) {
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-03 17:44:20 +01:00
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
2021-03-06 20:12:20 +01:00
match data.list_indexes().await {
2021-03-16 16:09:14 +01:00
Ok(indexes) => Ok(HttpResponse::Ok().json(indexes)),
2021-02-03 17:44:20 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-02-03 17:44:20 +01:00
}
}
2020-12-12 13:32:06 +01:00
}
#[get("/indexes/{index_uid}", wrap = "Authentication::Private")]
async fn get_index(
2021-02-04 12:34:12 +01:00
data: web::Data<Data>,
path: web::Path<IndexParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-03-15 16:52:05 +01:00
match data.index(path.index_uid.clone()).await {
2021-03-16 16:09:14 +01:00
Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
2021-03-15 16:52:05 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-02-04 12:34:12 +01: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>,
}
#[post("/indexes", wrap = "Authentication::Private")]
async fn create_index(
2021-02-08 10:47:34 +01:00
data: web::Data<Data>,
body: web::Json<IndexCreateRequest>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-03-15 16:52:05 +01:00
let body = body.into_inner();
match data.create_index(body.uid, body.primary_key).await {
2021-03-16 16:09:14 +01:00
Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
2021-02-08 10:47:34 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-02-08 10:47:34 +01:00
}
}
2020-12-12 13:32:06 +01:00
}
#[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>,
}
#[put("/indexes/{index_uid}", wrap = "Authentication::Private")]
async fn update_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<UpdateIndexRequest>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-03-15 16:52:05 +01:00
let body = body.into_inner();
2021-03-15 18:11:10 +01:00
match data
.update_index(path.into_inner().index_uid, body.primary_key, body.uid)
.await
{
2021-03-16 16:09:14 +01:00
Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
2021-02-16 15:26:13 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-02-16 15:26:13 +01:00
}
}
2020-12-12 13:32:06 +01:00
}
#[delete("/indexes/{index_uid}", wrap = "Authentication::Private")]
async fn delete_index(
2021-02-15 10:53:21 +01:00
data: web::Data<Data>,
path: web::Path<IndexParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-02-15 10:53:21 +01:00
match data.delete_index(path.index_uid.clone()).await {
Ok(_) => Ok(HttpResponse::NoContent().finish()),
2021-02-15 10:53:21 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-02-15 10:53:21 +01:00
}
}
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
}
#[get(
"/indexes/{index_uid}/updates/{update_id}",
wrap = "Authentication::Private"
)]
async fn get_update_status(
2021-01-28 17:20:51 +01:00
data: web::Data<Data>,
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-03-15 18:11:10 +01:00
let result = data
.get_update_status(params.index_uid, params.update_id)
.await;
2021-01-28 17:20:51 +01:00
match result {
2021-03-16 16:09:14 +01:00
Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
2021-01-28 17:20:51 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-01-28 17:20:51 +01:00
}
}
2020-12-12 13:32:06 +01:00
}
#[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")]
async fn get_all_updates_status(
2021-01-28 18:32:24 +01:00
data: web::Data<Data>,
path: web::Path<IndexParam>,
2020-12-12 13:32:06 +01:00
) -> Result<HttpResponse, ResponseError> {
2021-03-15 16:52:05 +01:00
let result = data.get_updates_status(path.into_inner().index_uid).await;
2021-01-28 18:32:24 +01:00
match result {
2021-03-16 16:09:14 +01:00
Ok(metas) => Ok(HttpResponse::Ok().json(metas)),
2021-01-28 18:32:24 +01:00
Err(e) => {
2021-03-16 16:09:14 +01:00
Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
2021-01-28 18:32:24 +01:00
}
}
2020-12-12 13:32:06 +01:00
}