From 1df434efbafb6251f633af60e965253c75772597 Mon Sep 17 00:00:00 2001 From: amass Date: Wed, 27 Nov 2024 00:08:24 +0800 Subject: [PATCH] add chrono format of dbo. --- Database/Session.cpp | 16 +++++++++++++++- Database/Session.h | 1 + Database/Task.h | 4 ++-- Database/VisitorRecord.h | 22 ++++++++++++++++++++++ Server/Application.cpp | 3 ++- 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 Database/VisitorRecord.h diff --git a/Database/Session.cpp b/Database/Session.cpp index c4dee9a..29bd175 100644 --- a/Database/Session.cpp +++ b/Database/Session.cpp @@ -1,11 +1,13 @@ #include "Session.h" #include +#include Session::Session(Wt::Dbo::SqlConnectionPool &connectionPool) { setConnectionPool(connectionPool); mapClass("task"); mapClass("homebox_item"); + mapClass("visitor_record"); mapClass("user"); mapClass("auth_info"); @@ -34,6 +36,18 @@ Wt::Auth::Login &Session::login() { return m_login; } +namespace Wt { +namespace Dbo { +template <> +void JsonSerializer::act(FieldRef field) { + using namespace std::chrono; + writeFieldName(field.name()); + fastJsStringLiteral(std::to_string(duration_cast(field.value().time_since_epoch()).count())); +} +} // namespace Dbo +} // namespace Wt + DBO_INSTANTIATE_TEMPLATES(User) DBO_INSTANTIATE_TEMPLATES(Task) -DBO_INSTANTIATE_TEMPLATES(HomeBox::Item) \ No newline at end of file +DBO_INSTANTIATE_TEMPLATES(HomeBox::Item) +DBO_INSTANTIATE_TEMPLATES(VisitorRecord) diff --git a/Database/Session.h b/Database/Session.h index d76204b..343c534 100644 --- a/Database/Session.h +++ b/Database/Session.h @@ -6,6 +6,7 @@ #include "User.h" #include #include +#include "VisitorRecord.h" using UserDatabase = Wt::Auth::Dbo::UserDatabase; diff --git a/Database/Task.h b/Database/Task.h index b744376..d6dafaa 100644 --- a/Database/Task.h +++ b/Database/Task.h @@ -2,6 +2,7 @@ #define __TASK_H__ #include +#include #include class Task; @@ -10,7 +11,7 @@ using Tasks = Wt::Dbo::collection>; class Task { public: bool finished = false; - int32_t createTime = 0; + std::chrono::system_clock::time_point createTime; std::string content; std::string comment; @@ -29,5 +30,4 @@ public: } }; - #endif // __TASK_H__ \ No newline at end of file diff --git a/Database/VisitorRecord.h b/Database/VisitorRecord.h new file mode 100644 index 0000000..9ae31eb --- /dev/null +++ b/Database/VisitorRecord.h @@ -0,0 +1,22 @@ +#ifndef __VISITORRECORD_H__ +#define __VISITORRECORD_H__ + +#include + +class VisitorRecord { +public: + std::string url; + std::string userAgent; + std::string visitorUuid; + std::chrono::system_clock::time_point time; + + template + void persist(Action &a) { + Wt::Dbo::field(a, url, "url"); + Wt::Dbo::field(a, userAgent, "user_agent"); + Wt::Dbo::field(a, visitorUuid, "visitor_uuid"); + Wt::Dbo::field(a, time, "time"); + } +}; + +#endif // __VISITORRECORD_H__ \ No newline at end of file diff --git a/Server/Application.cpp b/Server/Application.cpp index c5a0cbc..51e839e 100644 --- a/Server/Application.cpp +++ b/Server/Application.cpp @@ -78,6 +78,7 @@ Application::Application(const std::string &path) m_router->insert("/api/v1/task/add", [this](HttpSession &session, const Request &request, const boost::urls::matches &matches) mutable { using namespace boost::beast; + using namespace std::chrono; LOG(info) << "add task: " << request.body(); auto rootJson = boost::json::parse(request.body()); auto &root = rootJson.as_object(); @@ -88,7 +89,7 @@ Application::Application(const std::string &path) } auto database = Amass::Singleton::instance()->session(); auto task = std::make_unique(); - task->createTime = root.at("createTime").as_int64(); + task->createTime = system_clock::time_point(seconds(root.at("createTime").as_int64())); task->content = content; task->comment = std::string(root.at("comment").as_string()); auto t = database->add(std::move(task));