add homebox code.
All checks were successful
Deploy / PullDocker (push) Successful in 8s
Deploy / Build (push) Successful in 1m27s

This commit is contained in:
amass 2024-07-10 14:37:40 +00:00
parent 5602856c8f
commit ec6a5b8fca
7 changed files with 89 additions and 1 deletions

View File

@ -1,6 +1,7 @@
add_library(Database
Database.h Database.cpp
Task.h Task.cpp
HomeBox.h HomeBox.cpp
)
target_include_directories(Database

View File

@ -106,6 +106,51 @@ void Database::setTaskFinished(int id, bool finished, uint64_t finishedTime) {
}
}
bool Database::addHomeBoxItem(const std::string &name, const std::string &location, int cost) {
bool ret = true;
std::ostringstream oss;
oss << "INSERT INTO homebox (name,location,cost) VALUES (\"" << name << "\",\"" << location << "\"," << cost
<< ");";
auto sql = oss.str();
char *error = nullptr;
int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, &error);
if (result != SQLITE_OK) {
LOG(error) << "add task failed: " << error << ", sql: " << sql;
sqlite3_free(error);
ret = false;
}
return ret;
}
static int selectHomeBoxItemCallback(void *data, int argc, char **argv, char **columnName) {
auto items = reinterpret_cast<HomeBox::Items *>(data);
HomeBox::Item item;
for (int i = 0; i < argc; i++) {
if (argv[i] == nullptr) continue;
if (strcmp(columnName[i], "id") == 0) {
item.id = std::atol(argv[i]);
} else if (strcmp(columnName[i], "name") == 0) {
item.name = argv[i];
} else if (strcmp(columnName[i], "location") == 0) {
item.location = argv[i];
} else if (strcmp(columnName[i], "cost") == 0) {
item.cost = std::atol(argv[i]);
}
}
items->push_back(item);
return 0;
}
HomeBox::Items Database::homeBoxItems() {
HomeBox::Items ret;
char *error = nullptr;
if (sqlite3_exec(m_sqlite3, "select * from homebox", selectHomeBoxItemCallback, &ret, &error) != SQLITE_OK) {
LOG(error) << "sqlite3_exec() failed: " << error << std::endl;
sqlite3_free(error);
}
return ret;
}
void Database::initialize() {
const char *sql =
"CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, create_time INTEGER NOT NULL, "
@ -116,6 +161,14 @@ void Database::initialize() {
LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3);
return;
}
const char *homeBoxSql = "CREATE TABLE IF NOT EXISTS homebox (id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(512) NOT NULL, location VARCHAR(512) NOT NULL, cost INTEGER);";
result = sqlite3_exec(m_sqlite3, homeBoxSql, NULL, NULL, NULL);
if (result != SQLITE_OK) {
LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3);
return;
}
}
Database::~Database() {

View File

@ -1,6 +1,7 @@
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include "HomeBox.h"
#include "Singleton.h"
#include "Task.h"
#include <string>
@ -19,6 +20,9 @@ public:
bool removeTask(int id);
void setTaskFinished(int id, bool finished, uint64_t finishedTime);
HomeBox::Items homeBoxItems();
bool addHomeBoxItem(const std::string &name, const std::string &location, int cost);
protected:
void initialize();

View File

@ -0,0 +1 @@
#include "HomeBox.h"

20
Server/Database/HomeBox.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __HOMEBOX_H__
#define __HOMEBOX_H__
#include <list>
#include <string>
class HomeBox {
public:
class Item {
public:
int id = -1;
std::string name;
std::string location;
int cost;
};
using Items = std::list<Item>;
};
#endif // __HOMEBOX_H__

View File

@ -1,17 +1,25 @@
#include "Database.h"
#include "BoostLog.h"
#include <boost/test/unit_test.hpp>
#include <filesystem>
static constexpr auto path = "build/database.sqlite";
using namespace std::chrono;
BOOST_AUTO_TEST_CASE(DatabaseTest) {
if (std::filesystem::exists(path)) {
std::filesystem::remove(path);
}
Database database;
BOOST_TEST(database.open(path));
database.addTask(1234, "Hello");
database.addTask(1234, "这是一个测试","", true);
database.addTask(1234, "这是一个测试", "", true);
database.addHomeBoxItem("手机", "抽屉", 1499);
auto items = database.homeBoxItems();
BOOST_CHECK_EQUAL(items.size(), 1);
auto now = duration_cast<seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
database.setTaskFinished(1, true, now);

View File

@ -72,3 +72,4 @@ main $@
# curl -k --insecure https://127.0.0.1/lua
# openresty -p Server
# sudo openresty -p Server -s reload
# export LD_LIBRARY_PATH=/opt/Libraries/boost_1_85_0/lib:$LD_LIBRARY_PATH