Add task.
This commit is contained in:
parent
b8f7369d31
commit
fd432b29f6
1
.gitignore
vendored
1
.gitignore
vendored
@ -41,5 +41,6 @@ target_wrapper.*
|
||||
# QtCreator CMake
|
||||
CMakeLists.txt.user*
|
||||
build
|
||||
logs
|
||||
Server/logs
|
||||
Server/conf/cert
|
@ -8,4 +8,6 @@ FetchContent_Declare(Kylin
|
||||
)
|
||||
FetchContent_MakeAvailable(Kylin)
|
||||
|
||||
add_subdirectory(Server)
|
||||
add_subdirectory(Server)
|
||||
add_subdirectory(ThirdParty)
|
||||
add_subdirectory(UnitTest)
|
@ -1,5 +1,18 @@
|
||||
find_package(Boost COMPONENTS program_options json REQUIRED)
|
||||
|
||||
add_library(Database
|
||||
Database.h Database.cpp
|
||||
)
|
||||
|
||||
target_include_directories(Database
|
||||
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(Database
|
||||
PUBLIC sqlite3
|
||||
PUBLIC Universal
|
||||
)
|
||||
|
||||
add_executable(Server main.cpp
|
||||
HttpSession.h HttpSession.cpp
|
||||
Listener.h Listener.cpp
|
||||
@ -15,8 +28,8 @@ add_executable(Server main.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(Server
|
||||
PRIVATE Universal
|
||||
PRIVATE HttpProxy
|
||||
PRIVATE Database
|
||||
PRIVATE ${Boost_LIBRARIES}
|
||||
)
|
||||
|
||||
|
@ -1,7 +1,56 @@
|
||||
#include "Database.h"
|
||||
#include "BoostLog.h"
|
||||
#include <sqlite3.h>
|
||||
#include <sstream>
|
||||
|
||||
bool Database::open(const std::string &path) {
|
||||
bool ret = true;
|
||||
int result = sqlite3_open(path.c_str(), &m_sqlite3);
|
||||
if (result != SQLITE_OK) {
|
||||
ret = false;
|
||||
LOG(error) << "open database failed.";
|
||||
}
|
||||
initialize();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Database::addTask(uint64_t createTime, const std::string &content) {
|
||||
void Database::addTask(uint64_t createTime, const std::string &content, bool finished) {
|
||||
std::ostringstream oss;
|
||||
oss << "INSERT INTO tasks (create_time,content,finished) VALUES (" << createTime << ",\"" << content << "\","
|
||||
<< finished << ");";
|
||||
auto sql = oss.str();
|
||||
int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, NULL);
|
||||
if (result != SQLITE_OK) {
|
||||
LOG(error) << "add task failed: " << sqlite3_errmsg(m_sqlite3) << ", sql: " << sql;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Database::setTaskFinished(int id, bool finished, uint64_t finishedTime) {
|
||||
std::ostringstream oss;
|
||||
oss << "UPDATE tasks SET finished = " << finished << ", finished_time = " << finishedTime << " WHERE id = " << id;
|
||||
auto sql = oss.str();
|
||||
int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, NULL);
|
||||
if (result != SQLITE_OK) {
|
||||
LOG(error) << "add task failed: " << sqlite3_errmsg(m_sqlite3) << ", sql: " << sql;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Database::initialize() {
|
||||
const char *sql =
|
||||
"CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, create_time INTEGER NOT NULL, "
|
||||
"parent_id INTEGER, content VARCHAR(512) NOT NULL, finished BOLL, finished_time INTEGER);";
|
||||
int result = sqlite3_exec(m_sqlite3, sql, NULL, NULL, NULL);
|
||||
if (result != SQLITE_OK) {
|
||||
LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Database::~Database() {
|
||||
if (m_sqlite3 != nullptr) {
|
||||
sqlite3_close(m_sqlite3);
|
||||
m_sqlite3 = nullptr;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,25 @@
|
||||
#ifndef __DATABASE_H__
|
||||
#define __DATABASE_H__
|
||||
|
||||
#include "Singleton.h"
|
||||
#include <string>
|
||||
|
||||
struct sqlite3;
|
||||
|
||||
class Database {
|
||||
friend class Amass::Singleton<Database>;
|
||||
|
||||
public:
|
||||
~Database();
|
||||
bool open(const std::string &path);
|
||||
void addTask(uint64_t createTime, const std::string &content);
|
||||
void addTask(uint64_t createTime, const std::string &content, bool finished = false);
|
||||
void setTaskFinished(int id, bool finished, uint64_t finishedTime);
|
||||
|
||||
protected:
|
||||
void initialize();
|
||||
|
||||
private:
|
||||
sqlite3 *m_sqlite3 = nullptr;
|
||||
};
|
||||
|
||||
#endif // __DATABASE_H__
|
5
Server/ThirdParty/CMakeLists.txt
vendored
5
Server/ThirdParty/CMakeLists.txt
vendored
@ -1,5 +0,0 @@
|
||||
|
||||
|
||||
add_library(sqlite
|
||||
sqlite/sqlite3.c
|
||||
)
|
@ -11,6 +11,7 @@
|
||||
#include <boost/property_tree/ini_parser.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <filesystem>
|
||||
#include "Database.h"
|
||||
|
||||
void initSettings();
|
||||
|
||||
@ -56,6 +57,9 @@ int main(int argc, char const *argv[]) {
|
||||
}
|
||||
initSettings();
|
||||
|
||||
auto database = Amass::Singleton<Database>::instance<Amass::Construct>();
|
||||
database->open("database.sqlite");
|
||||
|
||||
std::string server = "0.0.0.0";
|
||||
|
||||
boost::property_tree::ptree ptree;
|
||||
|
7
ThirdParty/CMakeLists.txt
vendored
Normal file
7
ThirdParty/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
add_library(sqlite3
|
||||
sqlite/sqlite3.c
|
||||
)
|
||||
|
||||
target_include_directories(sqlite3
|
||||
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/sqlite
|
||||
)
|
18
UnitTest/CMakeLists.txt
Normal file
18
UnitTest/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
|
||||
|
||||
# --detect_memory_leak=0 --run_test=MarkdownParserTest,ProcessUtilityTest
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_executable(UnitTest main.cpp
|
||||
DatabaseTest.cpp
|
||||
)
|
||||
|
||||
target_compile_definitions(UnitTest
|
||||
PUBLIC LOG_FILTER_LEVEL=1
|
||||
)
|
||||
|
||||
target_link_libraries(UnitTest
|
||||
PRIVATE Boost::unit_test_framework
|
||||
PRIVATE Database
|
||||
)
|
18
UnitTest/DatabaseTest.cpp
Normal file
18
UnitTest/DatabaseTest.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "Database.h"
|
||||
#include "BoostLog.h"
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
static constexpr auto path = "build/database.sqlite";
|
||||
using namespace std::chrono;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DatabaseTest) {
|
||||
Database database;
|
||||
BOOST_TEST(database.open(path));
|
||||
|
||||
database.addTask(1234, "Hello");
|
||||
|
||||
database.addTask(1234, "这是一个测试", true);
|
||||
|
||||
auto now = duration_cast<seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
database.setTaskFinished(1, true, now);
|
||||
}
|
2
UnitTest/main.cpp
Normal file
2
UnitTest/main.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#define BOOST_TEST_MODULE OlderTest
|
||||
#include "boost/test/included/unit_test.hpp"
|
@ -27,6 +27,10 @@ function build() {
|
||||
/opt/Qt/Tools/CMake/bin/cmake \
|
||||
--build ${build_path} \
|
||||
--target all
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
build/UnitTest/UnitTest
|
||||
}
|
||||
|
||||
function deploy_backend() {
|
||||
@ -66,4 +70,4 @@ main $@
|
||||
|
||||
# curl -k --insecure https://127.0.0.1/lua
|
||||
# openresty -p Server
|
||||
# sudo openresty -p Server -s reload
|
||||
# sudo openresty -p Server -s reload
|
||||
|
Loading…
Reference in New Issue
Block a user