first commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#include "logrepository.h"
|
||||
|
||||
#include "../db/databasemanager.h"
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QVariant>
|
||||
|
||||
namespace {
|
||||
|
||||
bool ensureOpen(QString *error)
|
||||
{
|
||||
return DatabaseManager::instance().open(error);
|
||||
}
|
||||
|
||||
void setError(const QSqlQuery &query, QString *error)
|
||||
{
|
||||
if (error) {
|
||||
*error = query.lastError().text();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QList<LogRecord> LogRepository::listLogs(QString *error, int limit)
|
||||
{
|
||||
QList<LogRecord> records;
|
||||
if (!ensureOpen(error)) {
|
||||
return records;
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("select * from operation_logs order by id desc limit :limit");
|
||||
query.bindValue(":limit", limit);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return records;
|
||||
}
|
||||
|
||||
while (query.next()) {
|
||||
LogRecord record;
|
||||
record.id = query.value("id").toInt();
|
||||
record.type = query.value("type").toString();
|
||||
record.module = query.value("module").toString();
|
||||
record.action = query.value("action").toString();
|
||||
record.content = query.value("content").toString();
|
||||
record.status = query.value("status").toString();
|
||||
record.createdAt = query.value("created_at").toDateTime();
|
||||
records.append(record);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
bool LogRepository::insertLog(const LogRecord &record, QString *error)
|
||||
{
|
||||
if (!ensureOpen(error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery query(DatabaseManager::instance().database());
|
||||
query.prepare("insert into operation_logs (type, module, action, content, status) "
|
||||
"values (:type, :module, :action, :content, :status)");
|
||||
query.bindValue(":type", record.type);
|
||||
query.bindValue(":module", record.module);
|
||||
query.bindValue(":action", record.action);
|
||||
query.bindValue(":content", record.content);
|
||||
query.bindValue(":status", record.status);
|
||||
if (!query.exec()) {
|
||||
setError(query, error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user