MeiliSearch/meilisearch-http/src/routes/indexes/updates.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

2021-07-05 14:29:20 +02:00
use actix_web::{web, HttpResponse};
use chrono::{DateTime, Utc};
use log::debug;
use meilisearch_lib::MeiliSearch;
2021-07-05 14:29:20 +02:00
use serde::{Deserialize, Serialize};
use crate::error::ResponseError;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::routes::{IndexParam, UpdateStatusResponse};
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("").route(web::get().to(get_all_updates_status)))
.service(web::resource("{update_id}").route(web::get().to(get_update_status)));
}
#[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>,
}
#[derive(Deserialize)]
2021-07-07 16:20:22 +02:00
pub struct UpdateParam {
2021-07-05 14:29:20 +02:00
index_uid: String,
update_id: u64,
}
2021-07-07 16:20:22 +02:00
pub async fn get_update_status(
2021-09-24 12:03:16 +02:00
meilisearch: GuardedData<Private, MeiliSearch>,
2021-07-05 14:29:20 +02:00
path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> {
let params = path.into_inner();
2021-09-24 12:03:16 +02:00
let meta = meilisearch
.update_status(params.index_uid, params.update_id)
2021-07-05 14:29:20 +02:00
.await?;
let meta = UpdateStatusResponse::from(meta);
debug!("returns: {:?}", meta);
Ok(HttpResponse::Ok().json(meta))
}
2021-07-07 16:20:22 +02:00
pub async fn get_all_updates_status(
2021-09-24 12:03:16 +02:00
meilisearch: GuardedData<Private, MeiliSearch>,
2021-07-05 14:29:20 +02:00
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
2021-09-28 22:22:59 +02:00
let metas = meilisearch
.all_update_status(path.into_inner().index_uid)
.await?;
2021-07-05 14:29:20 +02:00
let metas = metas
.into_iter()
.map(UpdateStatusResponse::from)
.collect::<Vec<_>>();
debug!("returns: {:?}", metas);
Ok(HttpResponse::Ok().json(metas))
}