add chrono format of dbo.
Some checks failed
Deploy / Build (push) Failing after 3m26s

This commit is contained in:
amass 2024-11-27 00:08:24 +08:00
parent 059d6d3879
commit 1df434efba
5 changed files with 42 additions and 4 deletions

View File

@ -1,11 +1,13 @@
#include "Session.h"
#include <Wt/Auth/Dbo/UserDatabase.h>
#include <Wt/Dbo/WtJsonSqlTraits.h>
Session::Session(Wt::Dbo::SqlConnectionPool &connectionPool) {
setConnectionPool(connectionPool);
mapClass<Task>("task");
mapClass<HomeBox::Item>("homebox_item");
mapClass<VisitorRecord>("visitor_record");
mapClass<User>("user");
mapClass<AuthInfo>("auth_info");
@ -34,6 +36,18 @@ Wt::Auth::Login &Session::login() {
return m_login;
}
namespace Wt {
namespace Dbo {
template <>
void JsonSerializer::act(FieldRef<std::chrono::system_clock::time_point> field) {
using namespace std::chrono;
writeFieldName(field.name());
fastJsStringLiteral(std::to_string(duration_cast<seconds>(field.value().time_since_epoch()).count()));
}
} // namespace Dbo
} // namespace Wt
DBO_INSTANTIATE_TEMPLATES(User)
DBO_INSTANTIATE_TEMPLATES(Task)
DBO_INSTANTIATE_TEMPLATES(HomeBox::Item)
DBO_INSTANTIATE_TEMPLATES(VisitorRecord)

View File

@ -6,6 +6,7 @@
#include "User.h"
#include <Wt/Auth/Login.h>
#include <Wt/Dbo/Session.h>
#include "VisitorRecord.h"
using UserDatabase = Wt::Auth::Dbo::UserDatabase<AuthInfo>;

View File

@ -2,6 +2,7 @@
#define __TASK_H__
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/StdSqlTraits.h>
#include <string>
class Task;
@ -10,7 +11,7 @@ using Tasks = Wt::Dbo::collection<Wt::Dbo::ptr<Task>>;
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__

22
Database/VisitorRecord.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __VISITORRECORD_H__
#define __VISITORRECORD_H__
#include <Wt/Dbo/Dbo.h>
class VisitorRecord {
public:
std::string url;
std::string userAgent;
std::string visitorUuid;
std::chrono::system_clock::time_point time;
template <class Action>
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__

View File

@ -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<Database>::instance()->session();
auto task = std::make_unique<Task>();
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));