Add: universal task display
This commit is contained in:
76
Modules/Server/src/file_status.cpp
Normal file
76
Modules/Server/src/file_status.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "file_status.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <soci/boost-optional.h>
|
||||
|
||||
#include "tables.hpp"
|
||||
|
||||
std::optional<TaskInfo> TaskInfo::getId(soci::session& sql, std::string_view hash)
|
||||
{
|
||||
auto hash_str = std::string { hash };
|
||||
|
||||
soci::rowset<soci::row> row_data = (sql.prepare << R"(--
|
||||
SELECT Tasks.hash,
|
||||
Tasks.status,
|
||||
Tasks.timestamp AS TaskTimestamp,
|
||||
Tasks.url,
|
||||
Files.timestamp AS FileTimestamp,
|
||||
Files.filename,
|
||||
Files.format
|
||||
FROM Tasks LEFT OUTER JOIN Files ON Files.id = Tasks.id
|
||||
WHERE Tasks.hash = :file_hash ;)",
|
||||
soci::use(hash_str, "file_hash"));
|
||||
|
||||
for (auto const& el : row_data)
|
||||
{
|
||||
return fromRow(el);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<TaskInfo> TaskInfo::getAll(soci::session& sql)
|
||||
{
|
||||
soci::rowset<soci::row> row_data(sql.prepare << R"(--
|
||||
SELECT Tasks.hash,
|
||||
Tasks.status,
|
||||
Tasks.timestamp AS TaskTimestamp,
|
||||
Tasks.url,
|
||||
Files.timestamp AS FileTimestamp,
|
||||
Files.filename,
|
||||
Files.format
|
||||
FROM Tasks LEFT JOIN Files ON Files.id = Tasks.id
|
||||
ORDER BY TaskTimestamp DESC;")");
|
||||
|
||||
std::vector<TaskInfo> data;
|
||||
|
||||
for (auto const& el : row_data)
|
||||
{
|
||||
data.push_back(fromRow(el));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
TaskInfo TaskInfo::fromRow(soci::row const& row)
|
||||
{
|
||||
TaskInfo status;
|
||||
|
||||
|
||||
|
||||
auto file_status = row.get<FileStatus>("status");
|
||||
|
||||
status.hash = row.get<std::string>("hash");
|
||||
status.name = fmt::format("Task {}", status.hash);
|
||||
status.status = magic_enum::enum_name(file_status);
|
||||
status.timestamp = row.get<int>("TaskTimestamp");
|
||||
status.url = row.get<std::string>("url");
|
||||
|
||||
if (file_status == FileStatus::COMPLETE)
|
||||
{
|
||||
status.file = { .timestamp = row.get<int>("FileTimestamp"),
|
||||
.filename = row.get<std::string>("filename"),
|
||||
.format = row.get<std::string>("format") };
|
||||
}
|
||||
return status;
|
||||
}
|
||||
Reference in New Issue
Block a user