use actix_web::{delete, get, post, put}; use actix_web::{web, HttpResponse}; use chrono::{DateTime, Utc}; use log::error; use serde::{Deserialize, Serialize}; use crate::Data; use crate::error::ResponseError; 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")] async fn list_indexes(data: web::Data) -> Result { match data.list_indexes() { Ok(indexes) => { let json = serde_json::to_string(&indexes).unwrap(); Ok(HttpResponse::Ok().body(&json)) } Err(e) => { error!("error listing indexes: {}", e); unimplemented!() } } } #[get("/indexes/{index_uid}", wrap = "Authentication::Private")] async fn get_index( _data: web::Data, _path: web::Path, ) -> Result { todo!() } #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] struct IndexCreateRequest { name: Option, uid: Option, primary_key: Option, } #[post("/indexes", wrap = "Authentication::Private")] async fn create_index( _data: web::Data, _body: web::Json, ) -> Result { todo!() } #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase", deny_unknown_fields)] struct UpdateIndexRequest { name: Option, primary_key: Option, } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] struct UpdateIndexResponse { name: String, uid: String, created_at: DateTime, updated_at: DateTime, primary_key: Option, } #[put("/indexes/{index_uid}", wrap = "Authentication::Private")] async fn update_index( _data: web::Data, _path: web::Path, _body: web::Json, ) -> Result { todo!() } #[delete("/indexes/{index_uid}", wrap = "Authentication::Private")] async fn delete_index( _data: web::Data, _path: web::Path, ) -> Result { todo!() } #[derive(Deserialize)] struct UpdateParam { index_uid: String, update_id: u64, } #[get( "/indexes/{index_uid}/updates/{update_id}", wrap = "Authentication::Private" )] async fn get_update_status( data: web::Data, path: web::Path, ) -> Result { let result = data.get_update_status(&path.index_uid, path.update_id); match result { Ok(Some(meta)) => { let json = serde_json::to_string(&meta).unwrap(); Ok(HttpResponse::Ok().body(json)) } Ok(None) => { todo!() } Err(e) => { error!("{}", e); todo!() } } } #[get("/indexes/{index_uid}/updates", wrap = "Authentication::Private")] async fn get_all_updates_status( data: web::Data, path: web::Path, ) -> Result { let result = data.get_updates_status(&path.index_uid); match result { Ok(metas) => { let json = serde_json::to_string(&metas).unwrap(); Ok(HttpResponse::Ok().body(json)) } Err(e) => { error!("{}", e); todo!() } } }