Skip to content

Commit

Permalink
E02-C03 - RPC ParamsList & quick_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Nov 19, 2023
1 parent ada6387 commit 9995c67
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
49 changes: 34 additions & 15 deletions examples/quick_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,33 @@ async fn main() -> Result<()> {
);
req_login.await?.print().await?;

let req_create_task = hc.do_post(
"/api/rpc",
json!({
"id": 1,
"method": "create_task",
"params": {
"data": {
"title": "task AAA"
// -- Create Tasks
let mut task_ids: Vec<i64> = Vec::new();
for i in 0..=4 {
let req_create_task = hc.do_post(
"/api/rpc",
json!({
"id": 1,
"method": "create_task",
"params": {
"data": {
"title": format!("task AAA {i}")
}
}
}
}),
);
req_create_task.await?.print().await?;
}),
);
let result = req_create_task.await?;
task_ids.push(result.json_value::<i64>("/result/id")?);
}

// -- Update first Task
let req_update_task = hc.do_post(
"/api/rpc",
json!({
"id": 1,
"method": "update_task",
"params": {
"id": 1000, // Hardcode the task id.
"id": task_ids[0],
"data": {
"title": "task BB"
}
Expand All @@ -47,23 +53,36 @@ async fn main() -> Result<()> {
);
req_update_task.await?.print().await?;

// -- Delete second Task
let req_delete_task = hc.do_post(
"/api/rpc",
json!({
"id": 1,
"method": "delete_task",
"params": {
"id": 1001 // Harcode the task id
"id": task_ids[1] // Second task
}
}),
);
req_delete_task.await?.print().await?;

// -- List Tasks with filters
let req_list_tasks = hc.do_post(
"/api/rpc",
json!({
"id": 1,
"method": "list_tasks"
"method": "list_tasks",
"params": {
"filters": [{
"title": {"$endsWith": "BB"},
"done": false,
},{
"id": {"$in": [task_ids[2], task_ids[3]]}
}],
"list_options": {
"order_bys": "!id"
}
}
}),
);
req_list_tasks.await?.print().await?;
Expand Down
2 changes: 1 addition & 1 deletion src/web/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn _rpc_handler(
let result_json: Value = match rpc_method.as_str() {
// -- Task RPC methods.
"create_task" => exec_rpc_fn!(create_task, ctx, mm, rpc_params),
"list_tasks" => exec_rpc_fn!(list_tasks, ctx, mm),
"list_tasks" => exec_rpc_fn!(list_tasks, ctx, mm, rpc_params),
"update_task" => exec_rpc_fn!(update_task, ctx, mm, rpc_params),
"delete_task" => exec_rpc_fn!(delete_task, ctx, mm, rpc_params),

Expand Down
14 changes: 14 additions & 0 deletions src/web/rpc/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
//! each rpc handler function to receive the exact desired type.
//!

use modql::filter::ListOptions;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_with::{serde_as, OneOrMany};

#[derive(Deserialize)]
pub struct ParamsForCreate<D> {
Expand All @@ -22,3 +25,14 @@ pub struct ParamsForUpdate<D> {
pub struct ParamsIded {
pub id: i64,
}

#[serde_as]
#[derive(Deserialize)]
pub struct ParamsList<F>
where
F: DeserializeOwned,
{
#[serde_as(deserialize_as = "Option<OneOrMany<_>>")]
pub filters: Option<Vec<F>>,
pub list_options: Option<ListOptions>,
}
12 changes: 9 additions & 3 deletions src/web/rpc/task_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ctx::Ctx;
use crate::model::task::{Task, TaskBmc, TaskForCreate, TaskForUpdate};
use crate::model::task::{Task, TaskBmc, TaskFilter, TaskForCreate, TaskForUpdate};
use crate::model::ModelManager;
use crate::web::rpc::params::ParamsList;
use crate::web::rpc::{ParamsForCreate, ParamsForUpdate, ParamsIded};
use crate::web::Result;

Expand All @@ -17,8 +18,13 @@ pub async fn create_task(
Ok(task)
}

pub async fn list_tasks(ctx: Ctx, mm: ModelManager) -> Result<Vec<Task>> {
let tasks = TaskBmc::list(&ctx, &mm, None, None).await?;
pub async fn list_tasks(
ctx: Ctx,
mm: ModelManager,
params: ParamsList<TaskFilter>,
) -> Result<Vec<Task>> {
let tasks =
TaskBmc::list(&ctx, &mm, params.filters, params.list_options).await?;

Ok(tasks)
}
Expand Down

0 comments on commit 9995c67

Please sign in to comment.