实现TodoItem

794365
2021/11/25 18:37:36

本章代码在06/待办事项分支。

模型定义

/// 待办事项模型
#[derive(PostgresMapper, Serialize)]
#[pg_mapper(table = "todo_item")]
pub struct TodoItem {
    pub id: i32,
    pub title: String,
    pub checked: bool,
    pub list_id: i32,
}

/// 待办事项新ID模型
#[derive(PostgresMapper, Serialize)]
#[pg_mapper(table = "todo_item")]
pub struct TodoItemID {
    pub id: i32,
}

表单定义

接着,我们在src/form.rs定义相关表单:

#[derive(Deserialize)]
pub struct CreateTodoItem {
    pub title: String,
    pub list_id: i32,
}

数据库操作

然后,我们实现其数据库操作。创建src/db/todo_item.rs文件,并输入以下代码:(别忘了在src/db/mod.rs中声明todo_item模块)

从代码可以看出,都是对父模块相关方法的调用,这里不再赘述。

handler 的定义

同样的,我们创建src/handler/todo_item.rs,并输入以下内容:

pub async fn create(
    Extension(state): Extension<AppState>,
    Json(payload): Json<form::CreateTodoItem>,
) -> HandlerResult<TodoItemID> {
    let handler_name = "todo_item_create";
    let client = get_client(&state, handler_name).await?;
    let result = todo_item::create(&client, payload)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    Ok(Json(Response::ok(result)))
}

pub async fn all(
    Extension(state): Extension<AppState>,
    Path(list_id): Path<i32>,
) -> HandlerResult<Vec<TodoItem>> {
    let handler_name = "todo_item_all";
    let client = get_client(&state, handler_name).await?;
    let result = todo_item::all(&client, list_id)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    Ok(Json(Response::ok(result)))
}

pub async fn find(
    Extension(state): Extension<AppState>,
    Path((list_id, item_id)): Path<(i32, i32)>,
) -> HandlerResult<TodoItem> {
    let handler_name = "todo_item_find";
    let client = get_client(&state, handler_name).await?;
    let result = todo_item::find(&client, list_id, item_id)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    Ok(Json(Response::ok(result)))
}

pub async fn check(
    Extension(state): Extension<AppState>,
    Path((list_id, item_id)): Path<(i32, i32)>,
) -> HandlerResult<bool> {
    let handler_name = "todo_item_check";
    let client = get_client(&state, handler_name).await?;
    let result = todo_item::check(&client, list_id, item_id)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    Ok(Json(Response::ok(result)))
}
pub async fn delete(
    Extension(state): Extension<AppState>,
    Path((list_id, item_id)): Path<(i32, i32)>,
) -> HandlerResult<bool> {
    let handler_name = "todo_item_delete";
    let client = get_client(&state, handler_name).await?;
    let result = todo_item::delete(&client, list_id, item_id)
        .await
        .map_err(log_error(handler_name.to_string()))?;
    Ok(Json(Response::ok(result)))
}

也没什么新知识,都是简单的函数调用。

至此,TodoItem 已经开发完成,也意味着整个 Todo 服务开发完成。