Found Needle in Haystack. Fixed? Singelton

This commit is contained in:
jbrass 2025-02-09 23:22:43 +01:00
parent 44495dd11e
commit eab4c106e0
13 changed files with 50 additions and 54 deletions

View file

@ -14,15 +14,15 @@ namespace commands
void ActiveCommand::run(stringstream& args) void ActiveCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
int user_id; int user_id;
args >> user_id; args >> user_id;
if (!user_id) if (!user_id)
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
const User* u = mgr.get_user(user_id); const User* u = mgr->get_user(user_id);
if (!u) if (!u)
throw Error(401, "Eine solche BenutzerIn existiert nicht."); throw Error(401, "Eine solche BenutzerIn existiert nicht.");
for (const Assignment* a : mgr.get_assignments_for_user(user_id)) for (const Assignment* a : mgr->get_assignments_for_user(user_id))
cout << a->get_task_id() << "\n"; cout << a->get_task_id() << "\n";
} }
} // commands } // commands

View file

@ -17,21 +17,21 @@ namespace commands
void AddCommand::run(stringstream& args) void AddCommand::run(stringstream& args)
{ {
const string data_type = Util::read_string(args); const string data_type = Util::read_string(args);
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
if (data_type == "user") if (data_type == "user")
{ {
AddUser u; AddUser u;
args >> u; args >> u;
mgr.add_user(new User(u)); mgr->add_user(new User(u));
} }
else if (data_type == "task") else if (data_type == "task")
{ {
AddTask t; AddTask t;
args >> t; args >> t;
mgr.add_task(new Task(t)); mgr->add_task(new Task(t));
} }
else else
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden."); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden.");
mgr.save(); mgr->save();
} }
} // commands } // commands

View file

@ -14,21 +14,21 @@ namespace commands
void AssignCommand::run(stringstream& args) void AssignCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
int user_id, task_id; int user_id, task_id;
args >> user_id >> task_id; args >> user_id >> task_id;
if (!args) if (!args)
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
const User* u = mgr.get_user(user_id); const User* u = mgr->get_user(user_id);
if (!u) if (!u)
throw Error(401, "Eine solche BenutzerIn existiert nicht."); throw Error(401, "Eine solche BenutzerIn existiert nicht.");
const Task* t = mgr.get_task(task_id); const Task* t = mgr->get_task(task_id);
if (!t) if (!t)
throw Error(402, "Eine solche Aufgabe existiert nicht."); throw Error(402, "Eine solche Aufgabe existiert nicht.");
if (mgr.assignment_exists(user_id, task_id)) if (mgr->assignment_exists(user_id, task_id))
throw Error(302, "Eine solche Zuordnung existiert bereits."); throw Error(302, "Eine solche Zuordnung existiert bereits.");
Assignment* a = new Assignment(user_id, task_id); Assignment* a = new Assignment(user_id, task_id);
mgr.add_assignment(a); mgr->add_assignment(a);
mgr.save(); mgr->save();
} }
} // commands } // commands

View file

@ -7,7 +7,14 @@ using util::Manager;
namespace models { namespace models {
Assignment::Assignment(const int& user_id, const int& task_id) : user_id(user_id), task_id(task_id) { Assignment::Assignment(const int& user_id, const int& task_id) : user_id(user_id), task_id(task_id) {
//validate user exist //validate user exist
//Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
if (mgr->get_user(user_id) == nullptr) {
throw Error(401, "Eine solche BenutzerIn existiert nicht.");
}
if (mgr->get_task(task_id) == nullptr) {
throw Error(402, "Eine solche Aufgabe existiert nicht.");
}
} }
Assignment::Assignment(): user_id(0), task_id(0) {} Assignment::Assignment(): user_id(0), task_id(0) {}

View file

@ -14,18 +14,18 @@ namespace commands
void DelCommand::run(stringstream& args) void DelCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
const string data_type = Util::read_string(args); const string data_type = Util::read_string(args);
int id; int id;
args >> id; args >> id;
if (!args) if (!args)
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
if (data_type == "user") if (data_type == "user")
mgr.del_user(id); mgr->del_user(id);
else if (data_type == "task") else if (data_type == "task")
mgr.del_task(id); mgr->del_task(id);
else else
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
mgr.save(); mgr->save();
} }
} // commands } // commands

View file

@ -17,16 +17,16 @@ namespace commands
void ListCommand::run(stringstream& args) void ListCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
const string data_type = Util::read_string(args); const string data_type = Util::read_string(args);
if (data_type == "users") if (data_type == "users")
for (const User* u : mgr.get_users()) for (const User* u : mgr->get_users())
cout << *u; cout << *u;
else if (data_type == "tasks") else if (data_type == "tasks")
for (const Task* t : mgr.get_tasks()) for (const Task* t : mgr->get_tasks())
cout << *t; cout << *t;
else if (data_type == "assignments") else if (data_type == "assignments")
for (const Assignment* a : mgr.get_assignments()) for (const Assignment* a : mgr->get_assignments())
cout << *a; cout << *a;
else else
throw Error(101, "Befehl ist unbekannt."); throw Error(101, "Befehl ist unbekannt.");

View file

@ -103,7 +103,7 @@ namespace util
void Manager::update_task_id_index() void Manager::update_task_id_index()
{ {
int i = 0; int i = 0;
for (const auto& task_pair : this->users) for (const auto& task_pair : this->tasks)
if (task_pair.second && task_pair.second->get_id() >= i) if (task_pair.second && task_pair.second->get_id() >= i)
i = task_pair.second->get_id() + 1; i = task_pair.second->get_id() + 1;
this->task_id_index = i; this->task_id_index = i;
@ -129,13 +129,13 @@ namespace util
return value; return value;
} }
Manager* Manager::INSTANCE; Manager* Manager::INSTANCE = nullptr;
Manager& Manager::get_instance() Manager* Manager::get_instance()
{ {
if (!INSTANCE) if (INSTANCE == nullptr)
INSTANCE = new Manager(); INSTANCE = new Manager();
return *INSTANCE; return INSTANCE;
} }
vector<User*> Manager::get_users() const vector<User*> Manager::get_users() const

View file

@ -30,7 +30,9 @@ namespace util
void update_task_id_index(); void update_task_id_index();
public: public:
static Manager& get_instance(); Manager(const Manager&) = delete;
Manager& operator=(const Manager&) = delete;
static Manager* get_instance();
void save(); void save();
void update_indexes(); void update_indexes();

View file

@ -18,15 +18,15 @@ namespace commands
void ShowCommand::run(stringstream& args) void ShowCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
const string data_type = Util::read_string(args); const string data_type = Util::read_string(args);
if (data_type == "user") if (data_type == "user")
for (User* u : mgr.get_users()) { for (User* u : mgr->get_users()) {
AddUser au = {*u}; AddUser au = {*u};
cout << au; cout << au;
} }
else if (data_type == "task") else if (data_type == "task")
for (Task* t : mgr.get_tasks()) { for (Task* t : mgr->get_tasks()) {
AddTask at = {*t}; AddTask at = {*t};
cout << at; cout << at;
} }

View file

@ -20,8 +20,8 @@ namespace models
Task::Task(const AddTask& t): id(0), name(t.get_name()), description(t.get_description()), children(t.get_children()) Task::Task(const AddTask& t): id(0), name(t.get_name()), description(t.get_description()), children(t.get_children())
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
this->id = mgr.get_task_id(); this->id = mgr->get_task_id();
} }

View file

@ -13,21 +13,21 @@ namespace commands
void UnassignCommand::run(stringstream& args) void UnassignCommand::run(stringstream& args)
{ {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
int user_id, task_id; int user_id, task_id;
args >> user_id >> task_id; args >> user_id >> task_id;
if (!args) if (!args)
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
const User* u = mgr.get_user(user_id); const User* u = mgr->get_user(user_id);
if (!u) if (!u)
throw Error(401, "Eine solche BenutzerIn existiert nicht."); throw Error(401, "Eine solche BenutzerIn existiert nicht.");
const Task* t = mgr.get_task(task_id); const Task* t = mgr->get_task(task_id);
if (!t) if (!t)
throw Error(401, "Eine solche Zuordnung existiert nicht."); throw Error(401, "Eine solche Zuordnung existiert nicht.");
if (!mgr.assignment_exists(user_id, task_id)) if (!mgr->assignment_exists(user_id, task_id))
throw Error(302, "Eine solche Zuordnung existiert bereits."); throw Error(302, "Eine solche Zuordnung existiert bereits.");
Assignment as = {user_id, task_id}; Assignment as = {user_id, task_id};
mgr.del_assignment(as); mgr->del_assignment(as);
mgr.save(); mgr->save();
} }
} // commands } // commands

View file

@ -62,8 +62,8 @@ namespace models {
istream& operator>>(istream& is, AddUser& u) { istream& operator>>(istream& is, AddUser& u) {
Manager& mgr = Manager::get_instance(); Manager* mgr = Manager::get_instance();
int id = mgr.get_user_id(); int id = mgr->get_user_id();
string name, surname; string name, surname;
is >> name >> surname; is >> name >> surname;

13
tasks
View file

@ -1,13 +0,0 @@
[tasks]
0 %Eine Aufgabe% %Mit einer längeren Beschreibung%
1 %Noch eine Aufgabe% %Kurze Beschreibung% 0
[users]
0 Fleissiger Mitarbeiter
1 NochEin Mitarbeiter
[assignments]
0 0
0 1
1 1