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 25, 2024
1 parent e571480 commit 6594ee1
Show file tree
Hide file tree
Showing 6 changed files with 63 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;
47 changes: 47 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,47 @@
use crate::{
db::{models::ConfigVersion, schema::config_versions},
types::QueryFilters,
};

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

use superposition_types::result as superposition;

use actix_web::{get, web::Query, HttpResponse, Scope};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use serde_json::json;

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

#[get("")]
async fn get(
db_conn: DbConnection,
filters: Query<QueryFilters>,
) -> superposition::Result<HttpResponse> {
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 u64;
Ok(HttpResponse::Ok().json(json!({
"total_pages": 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 @@ -2,4 +2,5 @@ pub mod api;
pub mod db;
pub mod helpers;
pub mod middlewares;
pub mod types;
pub mod validation_functions;
7 changes: 7 additions & 0 deletions crates/context_aware_config/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct QueryFilters {
pub count: Option<i64>,
pub page: Option<i64>,
}
5 changes: 5 additions & 0 deletions crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,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 6594ee1

Please sign in to comment.