From 114fe25b08080b61524c12a81945eb2dfb303fc0 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Thu, 2 Mar 2023 23:02:58 +0800 Subject: [PATCH] chore: block --- apps/keck/src/server/api/blocks/block.rs | 121 +++++++++++++---------- 1 file changed, 68 insertions(+), 53 deletions(-) diff --git a/apps/keck/src/server/api/blocks/block.rs b/apps/keck/src/server/api/blocks/block.rs index 54718df28..b22526e4d 100644 --- a/apps/keck/src/server/api/blocks/block.rs +++ b/apps/keck/src/server/api/blocks/block.rs @@ -25,14 +25,17 @@ pub async fn get_block( Extension(context): Extension>, Path(params): Path<(String, String)>, ) -> Response { - let (ws_id, block) = params; - info!("get_block: {}, {}", ws_id, block); - if let Ok(workspace) = context.storage.get_workspace(ws_id).await { - if let Some(block) = workspace.with_trx(|t| workspace.get(&t.trx, block)) { - Json(block).into_response() - } else { - StatusCode::NOT_FOUND.into_response() - } + let (ws_id, block_id) = params; + info!("get_block: {}, {}", ws_id, block_id); + + if let Some(block) = context + .storage + .get_workspace(ws_id) + .await + .ok() + .and_then(|workspace| workspace.with_trx(|t| workspace.get(&t.trx, block_id))) + { + Json(block).into_response() } else { StatusCode::NOT_FOUND.into_response() } @@ -126,16 +129,23 @@ pub async fn get_block_history( Extension(context): Extension>, Path(params): Path<(String, String)>, ) -> Response { - let (ws_id, block) = params; - info!("get_block_history: {}, {}", ws_id, block); - if let Ok(workspace) = context.storage.get_workspace(&ws_id).await { - workspace.with_trx(|t| { - if let Some(block) = workspace.get(&t.trx, block) { - Json(&block.history(&t.trx)).into_response() - } else { - StatusCode::NOT_FOUND.into_response() - } + let (ws_id, block_id) = params; + info!("get_block_history: {}, {}", ws_id, block_id); + + if let Some(history) = context + .storage + .get_workspace(&ws_id) + .await + .ok() + .and_then(|workspace| { + workspace.with_trx(|t| { + workspace + .get(&t.trx, block_id) + .map(|block| block.history(&t.rx)) + }) }) + { + Json(history).into_response() } else { StatusCode::NOT_FOUND.into_response() } @@ -162,22 +172,24 @@ pub async fn delete_block( Extension(context): Extension>, Path(params): Path<(String, String)>, ) -> StatusCode { - let (ws_id, block) = params; - info!("delete_block: {}, {}", ws_id, block); - if let Ok(workspace) = context.storage.get_workspace(&ws_id).await { - if let Some(update) = workspace.with_trx(|mut t| { - if t.remove(&block) { - Some(t.trx.encode_update_v1()) - } else { - None - } - }) { - if let Err(e) = context.storage.docs().write_update(ws_id, &update).await { - error!("db write error: {}", e.to_string()); - } - return StatusCode::NO_CONTENT; + let (ws_id, block_id) = params; + info!("delete_block: {}, {}", ws_id, block_id); + + if let Some(update) = context + .storage + .get_workspace(&ws_id) + .await + .ok() + .and_then(|workspace| { + workspace.with_trx(|mut t| t.remove(&block_id).then_some(t.trx.encode_update_v1())) + }) + { + if let Err(e) = context.storage.docs().write_update(ws_id, &update).await { + error!("db write error: {}", e.to_string()); } + return StatusCode::NO_CONTENT; } + StatusCode::NOT_FOUND } @@ -204,34 +216,37 @@ pub async fn get_block_children( Path(params): Path<(String, String)>, Query(pagination): Query, ) -> Response { - let (ws_id, block) = params; + let (ws_id, block_id) = params; let Pagination { offset, limit } = pagination; - info!("get_block_children: {}, {}", ws_id, block); - if let Ok(workspace) = context.storage.get_workspace(ws_id).await { - if let Some(block) = workspace.with_trx(|t| workspace.get(&t.trx, &block)) { - let data: Vec = - block.children_iter(|children| children.skip(offset).take(limit).collect()); - - let status = if data.is_empty() { - StatusCode::NOT_FOUND - } else { - StatusCode::OK - }; + info!("get_block_children: {}, {}", ws_id, block_id); + if let Some((total, data)) = context + .storage + .get_workspace(ws_id) + .await + .ok() + .and_then(|workspace| workspace.with_trx(|t| workspace.get(&t.trx, &block_id))) + .map(|block| { ( - status, - Json(PageData { - total: block.children_len() as usize, - data, + block.children_len() as usize, + block.children_iter(|children| { + children.skip(offset).take(limit).collect::>() }), ) - .into_response() - } else { - StatusCode::NOT_FOUND.into_response() - } - } else { - StatusCode::NOT_FOUND.into_response() + }) + { + return ( + if data.is_empty() { + StatusCode::NOT_FOUND + } else { + StatusCode::OK + }, + Json(PageData { total, data }), + ) + .into_response(); } + + StatusCode::NOT_FOUND.into_response() } /// Insert a another `Block` into a `Block`'s children