Skip to content

Commit

Permalink
feat: add support for pagination in /internal/oca-bundles and /intern…
Browse files Browse the repository at this point in the history
…al/capture-bases endpoints
  • Loading branch information
olichwiruk committed Sep 12, 2023
1 parent 2b70723 commit 8b736e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dyn-clonable = "0.9.0"
indexmap = { version = "1.9.3", features = ["serde"] }
isolang = "2.3.0"
oca-parser-xls = { version = "2.0.0-rc.1", optional = true }
oca-rs = "0.3.0-rc.14"
oca-rs = "0.3.0-rc.15"
rusqlite = "0.29.0"
serde = { version = "1.0", features = ["derive"] }
serde-value = "0.7.0"
Expand Down
30 changes: 23 additions & 7 deletions src/routes/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ use oca_rs::{
data_storage::DataStorage, repositories::SQLiteConfig, EncodeBundle,
};

#[derive(serde::Deserialize)]
pub struct FetchAllOCABundleParams {
page: Option<usize>,
}

pub async fn fetch_all_oca_bundle(
db: web::Data<Box<dyn DataStorage>>,
cache_storage: web::Data<SQLiteConfig>,
query_params: web::Query<FetchAllOCABundleParams>,
) -> HttpResponse {
let oca_facade = oca_rs::Facade::new(
db.get_ref().clone(),
cache_storage.get_ref().clone(),
);
let result = match oca_facade.fetch_all_oca_bundle(500) {
Ok(oca_bundles) => {
let page = query_params.page.unwrap_or(1);
let result = match oca_facade.fetch_all_oca_bundle(100, page) {
Ok(all_oca_bundles) => {
serde_json::json!({
"success": true,
"results":
oca_bundles.iter().map(|oca_bundle| {
"r":
all_oca_bundles.records.iter().map(|oca_bundle| {
serde_json::from_str(
&String::from_utf8(
oca_bundle.encode().unwrap()
).unwrap()
).unwrap()
}).collect::<Vec<serde_json::Value>>(),
"m": all_oca_bundles.metadata,
})
}
Err(errors) => {
Expand All @@ -38,19 +46,27 @@ pub async fn fetch_all_oca_bundle(
.body(serde_json::to_string(&result).unwrap())
}

#[derive(serde::Deserialize)]
pub struct FetchAllCaptureBaseParams {
page: Option<usize>,
}

pub async fn fetch_all_capture_base(
db: web::Data<Box<dyn DataStorage>>,
cache_storage: web::Data<SQLiteConfig>,
query_params: web::Query<FetchAllCaptureBaseParams>,
) -> HttpResponse {
let oca_facade = oca_rs::Facade::new(
db.get_ref().clone(),
cache_storage.get_ref().clone(),
);
let result = match oca_facade.fetch_all_capture_base(500) {
Ok(capture_bases) => {
let page = query_params.page.unwrap_or(1);
let result = match oca_facade.fetch_all_capture_base(100, page) {
Ok(all_capture_bases) => {
serde_json::json!({
"success": true,
"results": capture_bases,
"r": all_capture_bases.records,
"m": all_capture_bases.metadata,
})
}
Err(errors) => {
Expand Down

0 comments on commit 8b736e9

Please sign in to comment.