-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ankit Mahato
authored and
Ankit Mahato
committed
Sep 25, 2024
1 parent
e571480
commit 6594ee1
Showing
6 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
crates/context_aware_config/src/api/config_versions/handlers.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod api; | |
pub mod db; | ||
pub mod helpers; | ||
pub mod middlewares; | ||
pub mod types; | ||
pub mod validation_functions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters