域名 AXUM.RS 将于 2025 年 10 月到期。我们无意再对其进行续费,如果你有意接续这个域名,请与我们取得联系。
  • AXUM.RS 现仅需人民币 3000 元(大写:叁仟元整。接受适度议价
  • 按照行业规则,AXUM.RS 到期后,大概率会进入长时间的赎回期,该期间内,如果你想拥有该域名,将要付出高额的费用
  • 我们已启用 AXUM.EU.ORG 域名,并将持续运营
  • 仅接受微信或支付宝交易
如果你对 AXUM.RS 有兴趣,请和我们进行联系:

实现TodoItem

经过一番重构,目前我们的 Todo 服务已经基本完善了,现在只差最后一个部分:TodoItem。本章我们就来实现它。

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

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

模型定义

表单定义

接着,我们在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模块)

pub async fn all(client: &Client, list_id: i32) -> Result<Vec<TodoItem>> {
    let result: Vec<TodoItem> = super::query(
        client,
        "SELECT id,title,checked,list_id FROM todo_item WHERE list_id=$1 ORDER BY id ASC",
        &[&list_id],
    )
    .await?;
    Ok(result)
}

pub async fn find(client: &Client, list_id: i32, item_id: i32) -> Result<TodoItem> {
    let result: TodoItem = super::query_one(
        client,
        "SELECT id,title,checked,list_id FROM todo_item WHERE id=$1 AND list_id=$2",
        &[&item_id, &list_id],
    )
    .await?;
    Ok(result)
}

pub async fn check(client: &Client, list_id: i32, item_id: i32) -> Result<bool> {
    let result = super::execute(
        client,
        "UPDATE todo_item SET checked=true WHERE id=$1 AND list_id=$2 AND checked=false",
        &[&item_id, &list_id],
    )
    .await?;
    Ok(result > 0)
}

pub async fn delete(client: &Client, list_id: i32, item_id: i32) -> Result<bool> {
    let result = super::execute(
        client,
        "DELETE FROM todo_item WHERE id=$1 AND list_id=$2",
        &[&item_id, &list_id],
    )
    .await?;
    Ok(result > 0)
}

pub async fn create(client: &Client, frm: form::CreateTodoItem) -> Result<TodoItemID> {
    let result = query_one(
        client,
        "INSERT INTO todo_item (title, checked, list_id) VALUES ($1,$2,$3) RETURNING id",
        &[&frm.title, &false, &frm.list_id],
    )
    .await?;
    Ok(result)
}

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

handler 的定义

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

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

要查看完整内容,请先登录