plan_execute/include/app.hpp

104 lines
3.4 KiB
C++
Raw Normal View History

2026-05-06 05:54:41 +00:00
#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