33 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "machine_repository.h"
 | 
						|
#include "sqlite_modern_cpp.h"
 | 
						|
 | 
						|
MachineRepository::MachineRepository(sqlite::database& db) : _db(db) {}
 | 
						|
 | 
						|
long long MachineRepository::create(std::optional<long long> 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<MachineData> MachineRepository::get_all_npcs(void) {
 | 
						|
  std::vector<MachineData> 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;
 | 
						|
}
 |