forked from University/epr24pr5-ojanssen2
Fix on Part of Deletion Restriction
This commit is contained in:
parent
4dde8d2209
commit
037173106a
4 changed files with 39 additions and 33 deletions
|
@ -5,18 +5,7 @@
|
|||
using err::Error;
|
||||
using util::Manager;
|
||||
namespace models {
|
||||
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();
|
||||
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(const int& user_id, const int& task_id) : user_id(user_id), task_id(task_id) {}
|
||||
Assignment::Assignment(): user_id(0), task_id(0) {}
|
||||
|
||||
ostream& Assignment::write(ostream& stream) const
|
||||
|
@ -33,12 +22,12 @@ namespace models {
|
|||
return this->user_id;
|
||||
}
|
||||
|
||||
ostream& operator<<(ostream& os, const Assignment& t) {
|
||||
os << t.get_user_id() << " " << t.get_task_id() << "\n";
|
||||
ostream& operator<<(ostream& os, const Assignment& assignment) {
|
||||
os << assignment.get_user_id() << " " << assignment.get_task_id() << "\n";
|
||||
return os;
|
||||
}
|
||||
|
||||
istream& operator>>(istream& is, Assignment& t) {
|
||||
istream& operator>>(istream& is, Assignment& assignment) {
|
||||
int user_id;
|
||||
is >> user_id;
|
||||
if (!is)
|
||||
|
@ -47,7 +36,7 @@ namespace models {
|
|||
is >> task_id;
|
||||
if (!is)
|
||||
throw Error(602, "Datei hat ein unbekanntes Format.");
|
||||
t = {user_id, task_id};
|
||||
assignment= {user_id, task_id};
|
||||
return is;
|
||||
}
|
||||
} // models
|
||||
|
|
|
@ -23,6 +23,7 @@ namespace commands
|
|||
if (data_type == "user")
|
||||
mgr->del_user(id);
|
||||
else if (data_type == "task")
|
||||
// Validate if we can Delete This Task.
|
||||
mgr->del_task(id);
|
||||
else
|
||||
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
|
||||
|
|
37
Manager.cpp
37
Manager.cpp
|
@ -161,7 +161,7 @@ namespace util
|
|||
return this->assignments;
|
||||
}
|
||||
|
||||
User* Manager::get_user(const int& id)
|
||||
User* Manager::get_user(const int id)
|
||||
{
|
||||
if (this->users.count(id) == 0)
|
||||
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)
|
||||
return nullptr;
|
||||
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;
|
||||
if (this->get_user(id) == nullptr)
|
||||
if (this->get_user(user_id) == nullptr)
|
||||
throw Error(401, "Eine solche BenutzerIn existiert nicht.");
|
||||
for (Assignment* as : this->assignments)
|
||||
if (as->get_user_id() == id)
|
||||
if (as->get_user_id() == user_id)
|
||||
user_assignments.push_back(as);
|
||||
return user_assignments;
|
||||
}
|
||||
|
||||
bool Manager::assignment_exists(const int& user_id, const int& task_id)
|
||||
Vector<Assignment*> Manager::get_assignments_for_task(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)
|
||||
if (as->get_user_id() == user_id && as->get_task_id() == task_id)
|
||||
return true;
|
||||
|
@ -221,19 +231,24 @@ namespace util
|
|||
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)
|
||||
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);
|
||||
}
|
||||
|
||||
void Manager::del_task(const int& id)
|
||||
void Manager::del_task(const int id)
|
||||
{
|
||||
if (this->tasks.count(id) == 0)
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
13
Manager.h
13
Manager.h
|
@ -43,17 +43,18 @@ namespace util
|
|||
vector<Task*> get_tasks() const;
|
||||
vector<Assignment*> get_assignments();
|
||||
|
||||
User* get_user(const int& id);
|
||||
Task* get_task(const int& id);
|
||||
vector<Assignment*> get_assignments_for_user(const int& id);
|
||||
bool assignment_exists(const int& user_id, const int& task_id);
|
||||
User* get_user(int id);
|
||||
Task* get_task(int id);
|
||||
vector<Assignment *> get_assignments_for_user(int user_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_task(Task* task);
|
||||
void add_assignment(Assignment* as);
|
||||
|
||||
void del_user(const int& id);
|
||||
void del_task(const int& id);
|
||||
void del_user(int id);
|
||||
void del_task(int id);
|
||||
void del_assignment(Assignment& as);
|
||||
};
|
||||
} // util
|
||||
|
|
Loading…
Add table
Reference in a new issue