#include "machine_repository.h" #include "sqlite_modern_cpp.h" MachineRepository::MachineRepository(sqlite::database& db) : _db(db) {} long long MachineRepository::create(std::optional owner_id, const std::string& hostname, const std::string& ip_address) { if(owner_id.has_value()) { _db << "INSERT INTO machines (owner_id, hostname, ip_address) VALUES(?, ?, ?);" << owner_id.value() << hostname << ip_address; } else { _db << "INSERT INTO machines (owner_id, hostname, ip_address) VALUES (NULL, ?, ?);" << hostname << ip_address; } return _db.last_insert_rowid(); } int MachineRepository::get_npc_count(void) { int count = 0; _db << "SELECT count(*) FROM machines WHERE owner_id IS NULL;" >> count; return count; } std::vector MachineRepository::get_all_npcs(void) { std::vector machines; _db << "SELECT id, ip_address FROM machines WHERE owner_id IS NULL;" >> [&](long long id, std::string ip_address) { machines.push_back({id, ip_address}); }; return machines; }