plan_execute/include/app.hpp

104 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef TODO_MANAGER_APP_HPP
#define TODO_MANAGER_APP_HPP
#include <string>
#include <vector>
#include <optional>
/**
* @brief 待办任务数据实体。
*
* 对应 Python 版 TodoItem(BaseModel),使用 C++ 结构体存储。
*/
struct TodoItem {
int id = 0; ///< 任务唯一标识
std::string title; ///< 任务标题
std::string description; ///< 任务描述(可选)
bool completed = false; ///< 完成状态,默认未完成
};
/**
* @brief 待办任务服务类,提供内存 CRUD 操作和 JSON 文件持久化。
*
* 对应 Python 版 TodoService内部使用 std::vector 存储,
* 数据以 JSON 格式写入本地文件,每次修改后自动持久化。
*/
class TodoService {
public:
/**
* @brief 构造服务实例,自动从指定文件加载数据。
* @param filepath JSON 数据文件路径,默认为 "todos.json"
*/
explicit TodoService(const std::string& filepath = "todos.json");
/// @brief 析构函数(当前无需特殊清理)。
~TodoService() = default;
// 禁止拷贝和赋值,避免文件状态不一致
TodoService(const TodoService&) = delete;
TodoService& operator=(const TodoService&) = delete;
/// @brief 允许移动构造/赋值
TodoService(TodoService&&) = default;
TodoService& operator=(TodoService&&) = default;
/**
* @brief 获取所有任务的常引用。
* @return 任务列表的 const 引用
*/
const std::vector<TodoItem>& get_all() const;
/**
* @brief 根据 ID 查找单个任务。
* @param id 任务 ID
* @return 若存在则返回 TodoItem否则返回 std::nullopt
*/
std::optional<TodoItem> get_by_id(int id) const;
/**
* @brief 创建新任务,自动分配 ID 并持久化。
* @param title 任务标题
* @param description 任务描述(可选,默认为空)
* @return 创建后的 TodoItem包含自动分配的 ID
*/
TodoItem create(const std::string& title, const std::string& description = "");
/**
* @brief 更新指定任务的部分或全部字段。
* @param id 要更新的任务 ID
* @param title 新标题std::nullopt 表示不更新)
* @param description 新描述std::nullopt 表示不更新)
* @param completed 新完成状态std::nullopt 表示不更新)
* @return 更新后的 TodoItemID 不存在则返回 std::nullopt
*/
std::optional<TodoItem> update(int id,
const std::optional<std::string>& title = std::nullopt,
const std::optional<std::string>& description = std::nullopt,
const std::optional<bool>& completed = std::nullopt);
/**
* @brief 删除指定 ID 的任务。
* @param id 要删除的任务 ID
* @return 删除成功返回 trueID 不存在返回 false
*/
bool delete_item(int id);
private:
/**
* @brief 从 JSON 文件加载数据到内存。
* 若文件不存在或格式错误,则初始化为空列表。
*/
void load();
/**
* @brief 将当前内存数据写入 JSON 文件。
*/
void save();
std::string filepath_; ///< JSON 持久化文件路径
std::vector<TodoItem> items_; ///< 内存中的任务列表
int next_id_ = 1; ///< 下一个可用 ID
};
#endif // TODO_MANAGER_APP_HPP