Skip to content

Commit

Permalink
feat: get config-versions api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit Mahato authored and Ankit Mahato committed Sep 30, 2024
1 parent e77ae0b commit 863f411
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/context_aware_config/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod audit_log;
pub mod config;
pub mod config_versions;
pub mod context;
pub mod default_config;
pub mod dimension;
Expand Down
2 changes: 2 additions & 0 deletions crates/context_aware_config/src/api/config_versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod handlers;
pub use handlers::endpoints;
50 changes: 50 additions & 0 deletions crates/context_aware_config/src/api/config_versions/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{
db::{models::ConfigVersion, schema::config_versions},
types::{PaginatedResponse, QueryFilters},
};

extern crate base64;
use service_utils::service::types::DbConnection;

use superposition_types::result as superposition;

use actix_web::{
get,
web::{Json, Query},
Scope,
};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};

pub fn endpoints() -> Scope {
Scope::new("").service(get)
}

#[get("")]
async fn get(
db_conn: DbConnection,
filters: Query<QueryFilters>,
) -> superposition::Result<Json<PaginatedResponse<ConfigVersion>>> {
let DbConnection(mut conn) = db_conn;

let n_version: i64 = config_versions::dsl::config_versions
.count()
.get_result(&mut conn)?;
let mut builder = config_versions::dsl::config_versions
.into_boxed()
.order(config_versions::dsl::created_at.desc());
if let Some(limit) = filters.count {
builder = builder.limit(limit);
}
if let Some(page) = filters.page {
let offset = (page - 1) * filters.count.unwrap_or(10);
builder = builder.offset(offset);
}
let limit = filters.count.unwrap_or(10);
let config_versions: Vec<ConfigVersion> = builder.load(&mut conn)?;
let total_pages = (n_version as f64 / limit as f64).ceil() as i64;
Ok(Json(PaginatedResponse {
total_pages,
total_items: n_version,
data: config_versions,
}))
}
1 change: 1 addition & 0 deletions crates/context_aware_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod api;
pub mod db;
pub mod helpers;
pub mod middlewares;
pub mod types;
pub mod validation_functions;
14 changes: 14 additions & 0 deletions crates/context_aware_config/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize)]
pub struct QueryFilters {
pub count: Option<i64>,
pub page: Option<i64>,
}

#[derive(Serialize, Debug, Clone, Deserialize)]
pub struct PaginatedResponse<T> {
pub total_pages: i64,
pub total_items: i64,
pub data: Vec<T>,
}
5 changes: 5 additions & 0 deletions crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ async fn main() -> Result<()> {
.wrap(AppExecutionScopeMiddlewareFactory::new(AppScope::CAC))
.service(type_templates::endpoints()),
)
.service(
scope("/config-versions")
.wrap(AppExecutionScopeMiddlewareFactory::new(AppScope::CAC))
.service(config_versions::endpoints()),
)
.service(
experiments::endpoints(scope("/experiments")).wrap(
AppExecutionScopeMiddlewareFactory::new(AppScope::EXPERIMENTATION),
Expand Down

0 comments on commit 863f411

Please sign in to comment.