Add task child.

This commit is contained in:
amass 2024-01-04 23:32:07 +08:00
parent 61858c438d
commit 2aa3d667c6
5 changed files with 44 additions and 13 deletions

View File

@ -29,6 +29,8 @@ static int selectTaskCallback(void *data, int argc, char **argv, char **columnNa
task.comment = argv[i]; task.comment = argv[i];
} else if (strcmp(columnName[i], "finished") == 0) { } else if (strcmp(columnName[i], "finished") == 0) {
task.finished = std::atol(argv[i]); task.finished = std::atol(argv[i]);
} else if (strcmp(columnName[i], "parent_id") == 0) {
task.parentId = std::atol(argv[i]);
} }
} }
tasks->push_back(task); tasks->push_back(task);
@ -42,14 +44,27 @@ Tasks Database::tasks() {
LOG(error) << "sqlite3_exec() failed: " << error << std::endl; LOG(error) << "sqlite3_exec() failed: " << error << std::endl;
sqlite3_free(error); sqlite3_free(error);
} }
std::unordered_map<int, Task *> tasks;
for (auto iterator = ret.begin(); iterator != ret.end();) {
if (iterator->parentId >= 0) {
auto parentTask = tasks.at(iterator->parentId);
parentTask->children.push_back(*iterator);
tasks.insert({iterator->id, &parentTask->children.back()});
iterator = ret.erase(iterator);
} else {
tasks.insert({iterator->id, &(*iterator)});
++iterator;
}
}
return ret; return ret;
} }
bool Database::addTask(uint64_t createTime, const std::string &content, const std::string &comment, bool finished) { bool Database::addTask(uint64_t createTime, const std::string &content, const std::string &comment, int parentId,
bool finished) {
bool ret = true; bool ret = true;
std::ostringstream oss; std::ostringstream oss;
oss << "INSERT INTO tasks (create_time,content,comment,finished) VALUES (" << createTime << ",\"" << content oss << "INSERT INTO tasks (create_time,content,comment,parent_id,finished) VALUES (" << createTime << ",\""
<< "\",\"" << comment << "\"," << finished << ");"; << content << "\",\"" << comment << "\"," << parentId << "," << finished << ");";
auto sql = oss.str(); auto sql = oss.str();
char *error = nullptr; char *error = nullptr;
int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, &error); int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, &error);

View File

@ -14,7 +14,7 @@ public:
~Database(); ~Database();
bool open(const std::string &path); bool open(const std::string &path);
Tasks tasks(); Tasks tasks();
bool addTask(uint64_t createTime, const std::string &content, const std::string &comment = "", bool addTask(uint64_t createTime, const std::string &content, const std::string &comment = "", int parentId = -1,
bool finished = false); bool finished = false);
bool removeTask(int id); bool removeTask(int id);
void setTaskFinished(int id, bool finished, uint64_t finishedTime); void setTaskFinished(int id, bool finished, uint64_t finishedTime);

View File

@ -5,16 +5,28 @@
namespace boost { namespace boost {
namespace json { namespace json {
static boost::json::object serialize(const Task &task) {
boost::json::object ret;
ret["id"] = task.id;
ret["parentId"] = task.parentId;
ret["finished"] = task.finished;
ret["createTime"] = task.createTime;
ret["content"] = task.content;
ret["comment"] = task.comment;
boost::json::array children;
for (auto &child : task.children) {
children.push_back(serialize(child));
}
ret["children"] = std::move(children);
return ret;
}
std::string serialize(const Tasks &tasks) { std::string serialize(const Tasks &tasks) {
boost::json::array ret; boost::json::array ret;
for (auto &task : tasks) { for (auto &task : tasks) {
boost::json::object t; ret.push_back(serialize(task));
t["id"] = task.id;
t["finished"] = task.finished;
t["createTime"] = task.createTime;
t["content"] = task.content;
t["comment"] = task.comment;
ret.push_back(std::move(t));
} }
return boost::json::serialize(ret); return boost::json::serialize(ret);
} }

View File

@ -4,15 +4,19 @@
#include <list> #include <list>
#include <string> #include <string>
class Task;
using Tasks = std::list<Task>;
class Task { class Task {
public: public:
int id = -1; int id = -1;
int parentId = -1;
bool finished = false; bool finished = false;
int32_t createTime = 0; int32_t createTime = 0;
std::string content; std::string content;
std::string comment; std::string comment;
Tasks children;
}; };
using Tasks = std::list<Task>;
namespace boost { namespace boost {
namespace json { namespace json {

View File

@ -70,7 +70,7 @@ SharedState::SharedState(boost::asio::io_context &ioContext, std::string doc_roo
auto database = Amass::Singleton<Database>::instance(); auto database = Amass::Singleton<Database>::instance();
auto root = boost::json::parse(request.body()); auto root = boost::json::parse(request.body());
bool ret = database->addTask(root.at("createTime").as_int64(), std::string(root.at("content").as_string()), bool ret = database->addTask(root.at("createTime").as_int64(), std::string(root.at("content").as_string()),
std::string(root.at("comment").as_string())); std::string(root.at("comment").as_string()), root.at("parentId").as_int64());
boost::json::object reply; boost::json::object reply;
reply["status"] = ret ? 0 : -1; reply["status"] = ret ? 0 : -1;
http::response<boost::beast::http::string_body> s{boost::beast::http::status::ok, request.version()}; http::response<boost::beast::http::string_body> s{boost::beast::http::status::ok, request.version()};