Update list_index route to return all index information, not only list of uid

This commit is contained in:
Quentin de Quelen 2019-11-20 09:57:27 +01:00
parent b95acbece0
commit 394976d330

View File

@ -8,7 +8,6 @@ use serde_json::json;
use tide::querystring::ContextExt as QSContextExt; use tide::querystring::ContextExt as QSContextExt;
use tide::response::IntoResponse; use tide::response::IntoResponse;
use tide::{Context, Response}; use tide::{Context, Response};
use chrono::{DateTime, Utc};
use crate::error::{ResponseError, SResult}; use crate::error::{ResponseError, SResult};
use crate::helpers::tide::ContextExt; use crate::helpers::tide::ContextExt;
@ -28,17 +27,49 @@ fn generate_uid() -> String {
pub async fn list_indexes(ctx: Context<Data>) -> SResult<Response> { pub async fn list_indexes(ctx: Context<Data>) -> SResult<Response> {
ctx.is_allowed(IndexesRead)?; ctx.is_allowed(IndexesRead)?;
let list = ctx
let indexes_uids = ctx
.state() .state()
.db .db
.indexes_uids() .indexes_uids()
.map_err(ResponseError::internal)?; .map_err(ResponseError::internal)?;
Ok(tide::response::json(list))
let env = &ctx.state().db.env;
let mut reader = env.read_txn().map_err(ResponseError::internal)?;
let mut response_body = Vec::new();
for index_uid in indexes_uids {
let index = ctx
.state()
.db
.open_index(&index_uid)
.ok_or(ResponseError::internal(&index_uid))?;
let name = index.main.name(&mut reader)
.map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Name not found"))?;
let created_at = index.main.created_at(&mut reader)
.map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Created date not found"))?;
let updated_at = index.main.updated_at(&mut reader)
.map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Updated date not found"))?;
let index_reponse = IndexResponse {
name,
uid: index_uid,
created_at,
updated_at,
};
response_body.push(index_reponse);
}
Ok(tide::response::json(response_body))
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
struct GetIndexResponse { struct IndexResponse {
name: String, name: String,
uid: String, uid: String,
created_at: DateTime<Utc>, created_at: DateTime<Utc>,
@ -64,7 +95,7 @@ pub async fn get_index(ctx: Context<Data>) -> SResult<Response> {
.map_err(ResponseError::internal)? .map_err(ResponseError::internal)?
.ok_or(ResponseError::internal("Updated date not found"))?; .ok_or(ResponseError::internal("Updated date not found"))?;
let response_body = GetIndexResponse { let response_body = IndexResponse {
name, name,
uid, uid,
created_at, created_at,