Refactored stuff into new files, added a test modal for adding critters
This commit is contained in:
109
src/database.hpp
109
src/database.hpp
@@ -1,104 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "critter.hpp"
|
||||
#include <SQLiteCpp/Database.h>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
inline static bool db_get_all_critters(std::map<int, Critter> &critters) {
|
||||
try {
|
||||
SQLite::Database db("./assets/local.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
|
||||
SQLite::Statement q(db, "SELECT * FROM critters");
|
||||
|
||||
critters.clear();
|
||||
|
||||
while (q.executeStep()) {
|
||||
int id = q.getColumn(0);
|
||||
|
||||
critters.try_emplace(
|
||||
id,
|
||||
id,
|
||||
q.getColumn(1).getText(),
|
||||
q.getColumn(2).getText(),
|
||||
q.getColumn(3).getInt(),
|
||||
q.getColumn(4).getInt(),
|
||||
q.getColumn(5).getText(),
|
||||
q.getColumn(6).getText()
|
||||
);
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
std::cout << "[ERROR] DB exception: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline static void db_print_all_critters(std::map<int, Critter> &critters) {
|
||||
std::cout << "Current loaded critters:";
|
||||
for (auto c : critters) {
|
||||
std::cout << "\nCritter " << c.first << "\n";
|
||||
std::cout << "ID: " << c.second.id << std::endl;
|
||||
std::cout << "Name:" << c.second.name << std::endl;
|
||||
std::cout << "Gender: " << c.second.gender << std::endl;
|
||||
std::cout << "Mother ID: " << c.second.mother_id << std::endl;
|
||||
std::cout << "Father ID: " << c.second.father_id << std::endl;
|
||||
std::cout << "Notes: " << c.second.notes << std::endl;
|
||||
std::cout << "Species: " << c.second.species << std::endl;
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
inline static bool validate_critter(const Critter &c, std::map<int, Critter> &all_critters, std::string &out_error) {
|
||||
if (c.mother_id != 0 && c.father_id != 0 && c.mother_id == c.father_id) {
|
||||
out_error = "Mother ID and Father ID cannot be the same.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mother Validation
|
||||
if (c.mother_id != 0) {
|
||||
auto it = all_critters.find(c.mother_id);
|
||||
if (it == all_critters.end()) {
|
||||
out_error = "Mother ID " + std::to_string(c.mother_id) + " does not exist.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const Critter& mother = it->second;
|
||||
|
||||
if (mother.gender != "F" && mother.gender != "Female") {
|
||||
out_error = "Mother must have female gender ('F' or 'Female').";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mother.species != c.species) {
|
||||
out_error = "Mother species ('" + mother.species + "') must match critter species ('" + c.species + "').";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Father Validation
|
||||
if (c.father_id != 0) {
|
||||
auto it = all_critters.find(c.father_id);
|
||||
if (it == all_critters.end()) {
|
||||
out_error = "Father ID " + std::to_string(c.father_id) + " does not exist.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const Critter& father = it->second;
|
||||
|
||||
if (father.gender != "M" && father.gender != "Male") {
|
||||
out_error = "Father must have male gender ('M' or 'Male').";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (father.species != c.species) {
|
||||
out_error = "Father species ('" + father.species + "') must match critter species ('" + c.species + "').";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline static bool db_try_add_critter() {
|
||||
|
||||
}
|
||||
bool db_get_all_critters(std::map<int, Critter> &critters);
|
||||
bool db_add_critter(const Critter &c, int &out_id);
|
||||
bool db_delete_critter(int id);
|
||||
void db_print_all_critters(const std::map<int, Critter> &critters);
|
||||
bool validate_critter(const Critter &c, const std::map<int, Critter> &all_critters, std::string &out_error);
|
||||
Reference in New Issue
Block a user