104 lines
2.1 KiB
C++
104 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <clickhouse/client.h>
|
|
#include "clickhouse/base/uuid.h"
|
|
#include "skwyward-api-utils.hpp"
|
|
|
|
namespace database_utils {
|
|
|
|
clickhouse::UUID parse_uuid(const std::string& str);
|
|
std::string uuid_to_string(const clickhouse::UUID& u);
|
|
|
|
// ---------- DB Handle ----------
|
|
|
|
using CHClient = std::shared_ptr<clickhouse::Client>;
|
|
|
|
// ---------- User ----------
|
|
|
|
struct UserRecord {
|
|
std::string user_id; // UUID
|
|
api_utils::Login login;
|
|
};
|
|
|
|
// ---------- Snapshot ----------
|
|
|
|
struct GradeSnapshot {
|
|
std::string response_id;
|
|
api_utils::GradesResponse response;
|
|
};
|
|
|
|
// ---------- Assignment Diff ----------
|
|
|
|
struct AssignmentDiff {
|
|
std::string class_grade_id;
|
|
std::string assignment_id;
|
|
|
|
std::optional<api_utils::AssignmentGrade> old_grade;
|
|
api_utils::AssignmentGrade new_grade;
|
|
};
|
|
|
|
// ---------- User ops ----------
|
|
|
|
std::vector<UserRecord>
|
|
get_all_users(const CHClient& client);
|
|
|
|
bool
|
|
register_user(
|
|
const CHClient& client,
|
|
const std::string& username,
|
|
const std::string& password
|
|
);
|
|
|
|
bool
|
|
authenticate_user(
|
|
const CHClient& client,
|
|
const std::string& username,
|
|
const std::string& password
|
|
);
|
|
|
|
// ---------- Grades ----------
|
|
|
|
std::optional<GradeSnapshot>
|
|
load_latest_grades(
|
|
const CHClient& client,
|
|
const std::string& user_id
|
|
);
|
|
|
|
// ---------- Diff ----------
|
|
|
|
std::vector<AssignmentDiff>
|
|
diff_grade_responses(
|
|
const api_utils::GradesResponse& old_resp,
|
|
const api_utils::GradesResponse& new_resp
|
|
);
|
|
|
|
// ---------- Conditional insert ----------
|
|
|
|
bool
|
|
conditionally_insert_grades(
|
|
const CHClient& client,
|
|
const std::string& user_id,
|
|
const api_utils::GradesResponse& new_resp
|
|
);
|
|
|
|
// ---------- Persist diffs ----------
|
|
|
|
void
|
|
insert_grade_updates(
|
|
const CHClient& client,
|
|
const std::string& user_id,
|
|
const std::string& old_response_id,
|
|
const std::string& new_response_id,
|
|
const std::vector<AssignmentDiff>& diffs
|
|
);
|
|
|
|
std::optional<clickhouse::UUID> get_user_uuid(const CHClient& client, const std::string& username);
|
|
|
|
} // namespace database_utils
|
|
|