Compare commits

..

3 commits

Author SHA1 Message Date
jbrass
037173106a Fix on Part of Deletion Restriction 2025-02-10 00:25:28 +01:00
jbrass
4dde8d2209 Fix für die ReadNumbers Util 2025-02-10 00:00:59 +01:00
jbrass
eab4c106e0 Found Needle in Haystack. Fixed? Singelton 2025-02-09 23:22:43 +01:00
14 changed files with 103 additions and 85 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

@ -5,11 +5,7 @@
using err::Error; using err::Error;
using util::Manager; 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
//Manager& mgr = Manager::get_instance();
}
Assignment::Assignment(): user_id(0), task_id(0) {} Assignment::Assignment(): user_id(0), task_id(0) {}
ostream& Assignment::write(ostream& stream) const ostream& Assignment::write(ostream& stream) const
@ -26,12 +22,12 @@ namespace models {
return this->user_id; return this->user_id;
} }
ostream& operator<<(ostream& os, const Assignment& t) { ostream& operator<<(ostream& os, const Assignment& assignment) {
os << t.get_user_id() << " " << t.get_task_id() << "\n"; os << assignment.get_user_id() << " " << assignment.get_task_id() << "\n";
return os; return os;
} }
istream& operator>>(istream& is, Assignment& t) { istream& operator>>(istream& is, Assignment& assignment) {
int user_id; int user_id;
is >> user_id; is >> user_id;
if (!is) if (!is)
@ -40,7 +36,7 @@ namespace models {
is >> task_id; is >> task_id;
if (!is) if (!is)
throw Error(602, "Datei hat ein unbekanntes Format."); throw Error(602, "Datei hat ein unbekanntes Format.");
t = {user_id, task_id}; assignment= {user_id, task_id};
return is; return is;
} }
} // models } // models

View file

@ -14,18 +14,19 @@ 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); // Validate if we can Delete This Task.
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
@ -161,7 +161,7 @@ namespace util
return this->assignments; return this->assignments;
} }
User* Manager::get_user(const int& id) User* Manager::get_user(const int id)
{ {
if (this->users.count(id) == 0) if (this->users.count(id) == 0)
return nullptr; return nullptr;
@ -169,26 +169,36 @@ namespace util
} }
Task* Manager::get_task(const int& id) Task* Manager::get_task(const int id)
{ {
if (this->tasks.count(id) == 0) if (this->tasks.count(id) == 0)
return nullptr; return nullptr;
return this->tasks[id]; return this->tasks[id];
} }
Vector<Assignment*> Manager::get_assignments_for_user(const int& id) Vector<Assignment*> Manager::get_assignments_for_user(const int user_id)
{ {
Vector<Assignment*> user_assignments; Vector<Assignment*> user_assignments;
if (this->get_user(id) == nullptr) if (this->get_user(user_id) == nullptr)
throw Error(401, "Eine solche BenutzerIn existiert nicht."); throw Error(401, "Eine solche BenutzerIn existiert nicht.");
for (Assignment* as : this->assignments) for (Assignment* as : this->assignments)
if (as->get_user_id() == id) if (as->get_user_id() == user_id)
user_assignments.push_back(as); user_assignments.push_back(as);
return user_assignments; return user_assignments;
} }
Vector<Assignment*> Manager::get_assignments_for_task(const int task_id)
bool Manager::assignment_exists(const int& user_id, const int& task_id)
{ {
Vector<Assignment*> task_assignments;
if (this->get_task(task_id) == nullptr)
throw Error(401, "Eine solche BenutzerIn existiert nicht.");
for (Assignment* as : this->assignments)
if (as->get_task_id() == task_id)
task_assignments.push_back(as);
return task_assignments;
}
bool Manager::assignment_exists(int user_id, int task_id) {
for ( Assignment* as : this->assignments) for ( Assignment* as : this->assignments)
if (as->get_user_id() == user_id && as->get_task_id() == task_id) if (as->get_user_id() == user_id && as->get_task_id() == task_id)
return true; return true;
@ -221,19 +231,24 @@ namespace util
this->assignments.push_back(as); this->assignments.push_back(as);
} }
void Manager::del_user(const int& id) void Manager::del_user(const int id)
{ {
if (this->users.count(id) == 0) if (this->users.count(id) == 0)
throw Error(401, "Eine solche BenutzerIn existiert nicht."); throw Error(401, "Eine solche BenutzerIn existiert nicht.");
//Assigment
if (!this->get_assignments_for_user(id).empty()) {
throw Error(201, "Benutzer kann nicht gelöscht werden.");
}
this->users.erase(id); this->users.erase(id);
} }
void Manager::del_task(const int& id) void Manager::del_task(const int id)
{ {
if (this->tasks.count(id) == 0) if (this->tasks.count(id) == 0)
throw Error(402, "Eine solche Aufgabe existiert nicht."); throw Error(402, "Eine solche Aufgabe existiert nicht.");
if (!this->get_assignments_for_task(id).empty()) {
throw Error(201, "Aufgabe kann nicht gelöscht werden.");
}
this->tasks.erase(id); this->tasks.erase(id);
} }

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();
@ -41,17 +43,18 @@ namespace util
vector<Task*> get_tasks() const; vector<Task*> get_tasks() const;
vector<Assignment*> get_assignments(); vector<Assignment*> get_assignments();
User* get_user(const int& id); User* get_user(int id);
Task* get_task(const int& id); Task* get_task(int id);
vector<Assignment*> get_assignments_for_user(const int& id); vector<Assignment *> get_assignments_for_user(int user_id);
bool assignment_exists(const int& user_id, const int& task_id); vector<Assignment *> get_assignments_for_task(int task_id);
bool assignment_exists(int user_id, int task_id);
void add_user(User* user); void add_user(User* user);
void add_task(Task* task); void add_task(Task* task);
void add_assignment(Assignment* as); void add_assignment(Assignment* as);
void del_user(const int& id); void del_user(int id);
void del_task(const int& id); void del_task(int id);
void del_assignment(Assignment& as); void del_assignment(Assignment& as);
}; };
} // util } // util

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();
} }
@ -46,10 +46,8 @@ namespace models
return this->description; return this->description;
} }
vector<int> Task::get_children() const vector<int> Task::get_children() const { return this->children; }
{
return this->children;
}
AddTask::AddTask(): Task() {} AddTask::AddTask(): Task() {}
@ -89,7 +87,7 @@ namespace models
if (name.empty()) if (name.empty())
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
string description = Util::read_string_between_percent(is); string description = Util::read_string_between_percent(is);
vector<int> children = Util::read_numbers(is); vector<int> children = Task::read_children(is);
t = {id, name, description, children}; t = {id, name, description, children};
return is; return is;
} }
@ -115,7 +113,7 @@ namespace models
if (name.empty()) if (name.empty())
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
const string description = Util::read_string_between_percent(is); const string description = Util::read_string_between_percent(is);
const vector<int> children = Util::read_numbers(is); const vector<int> children = Task::read_children(is);
Task at = {0, name, description, children}; Task at = {0, name, description, children};
t = at; t = at;
return is; return is;
@ -127,4 +125,21 @@ namespace models
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden"); throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
} }
} }
vector<int> Task::read_children(istream &is) {
vector<int> nums;
int num;
// Read integers directly from the stream until we hit an error (non-integer or EOF)
while (is >> num) {
nums.push_back(num);
}
// Stream wurde bad durch was anderes als EOF?
if (!is.eof()) {
is.clear();
throw Error(703, "Could not convert input to integer.");
}
return nums;
}
} // models } // models

1
Task.h
View file

@ -31,6 +31,7 @@ namespace models
string get_name() const; string get_name() const;
string get_description() const; string get_description() const;
vector<int> get_children() const; vector<int> get_children() const;
static Vector<int> read_children(istream &is);
friend ostream& operator<<(ostream& os, const Task& t); friend ostream& operator<<(ostream& os, const Task& t);

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