forked from University/epr24pr5-ojanssen2
Compare commits
3 commits
ca572b6fb5
...
684a321040
Author | SHA1 | Date | |
---|---|---|---|
![]() |
684a321040 | ||
![]() |
30dfe40ef9 | ||
![]() |
9d2de95332 |
7 changed files with 155 additions and 123 deletions
|
@ -16,12 +16,12 @@ namespace models {
|
|||
int get_user_id() const;
|
||||
int get_task_id() const;
|
||||
|
||||
friend ostream& operator<<(ostream& os, const Assignment& t);
|
||||
friend istream& operator>>(istream& is, Assignment& t);
|
||||
friend ostream& operator<<(ostream& os, const Assignment& assignment);
|
||||
friend istream& operator>>(istream& is, Assignment& assignment);
|
||||
};
|
||||
|
||||
ostream& operator<<(ostream& os, const Assignment& t);
|
||||
istream& operator>>(istream& is, Assignment& t);
|
||||
ostream& operator<<(ostream& os, const Assignment& assignment);
|
||||
istream& operator>>(istream& is, Assignment& assignment);
|
||||
}
|
||||
|
||||
#endif // ASSIGNMENT_H
|
||||
|
|
132
Manager.cpp
132
Manager.cpp
|
@ -1,19 +1,16 @@
|
|||
#include "Manager.h"
|
||||
#include "Error.h"
|
||||
#include <fstream>
|
||||
#include "DataType.h"
|
||||
#include "Error.h"
|
||||
|
||||
using err::Error;
|
||||
using util::DataType;
|
||||
|
||||
namespace util
|
||||
{
|
||||
Manager::Manager(): users({}), tasks({}), assignments({}), user_id_index(0), task_id_index(0), filename("tasks")
|
||||
{
|
||||
namespace util {
|
||||
Manager::Manager() : users({}), tasks({}), assignments({}), user_id_index(0), task_id_index(0), filename("tasks") {
|
||||
// Open File
|
||||
ifstream in = ifstream(this->filename, ios_base::in);
|
||||
if (!in)
|
||||
{
|
||||
if (!in) {
|
||||
this->save();
|
||||
in.clear();
|
||||
in.open(this->filename, ios_base::in);
|
||||
|
@ -25,11 +22,9 @@ namespace util
|
|||
string d; // dump
|
||||
string line, section;
|
||||
// Parse all Lines until EOF
|
||||
while (getline(in, line))
|
||||
{
|
||||
while (getline(in, line)) {
|
||||
// Check if the line starts with '['
|
||||
if (!line.empty() && line[0] == '[')
|
||||
{
|
||||
if (!line.empty() && line[0] == '[') {
|
||||
if (line == "[tasks]")
|
||||
current_type = DataType::TASK;
|
||||
else if (line == "[users]")
|
||||
|
@ -50,39 +45,30 @@ namespace util
|
|||
|
||||
// Reihenfolge der Verabeitung
|
||||
vector<DataType> sections = {DataType::USER, DataType::TASK, DataType::ASSIGNMENT};
|
||||
for (DataType type : sections)
|
||||
{
|
||||
for (DataType type: sections) {
|
||||
// Keine Data -> Continue
|
||||
if (buffer.find(type) == buffer.end())
|
||||
continue;
|
||||
const vector<string> &lines = buffer[type];
|
||||
// Iteriere über die Gebufferten Strings.
|
||||
for (const string& buffered_line : lines)
|
||||
{
|
||||
for (const string &buffered_line: lines) {
|
||||
stringstream string_stream(buffered_line);
|
||||
switch (type)
|
||||
{
|
||||
case DataType::TASK:
|
||||
{
|
||||
switch (type) {
|
||||
case DataType::TASK: {
|
||||
Task task;
|
||||
string_stream >> task;
|
||||
this->tasks[task.get_id()] = new Task(task);
|
||||
}
|
||||
break;
|
||||
case DataType::USER:
|
||||
{
|
||||
} break;
|
||||
case DataType::USER: {
|
||||
User user;
|
||||
string_stream >> user;
|
||||
this->users[user.get_id()] = new User(user);
|
||||
}
|
||||
break;
|
||||
case DataType::ASSIGNMENT:
|
||||
{
|
||||
} break;
|
||||
case DataType::ASSIGNMENT: {
|
||||
Assignment assignment;
|
||||
string_stream >> assignment;
|
||||
this->assignments.push_back(new Assignment(assignment));
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -91,8 +77,7 @@ namespace util
|
|||
this->update_indexes();
|
||||
}
|
||||
|
||||
void Manager::update_user_id_index()
|
||||
{
|
||||
void Manager::update_user_id_index() {
|
||||
int i = 0;
|
||||
for (const auto &user_pair: this->users)
|
||||
if (user_pair.second && user_pair.second->get_id() >= i)
|
||||
|
@ -100,8 +85,7 @@ namespace util
|
|||
this->user_id_index = i;
|
||||
}
|
||||
|
||||
void Manager::update_task_id_index()
|
||||
{
|
||||
void Manager::update_task_id_index() {
|
||||
int i = 0;
|
||||
for (const auto &task_pair: this->tasks)
|
||||
if (task_pair.second && task_pair.second->get_id() >= i)
|
||||
|
@ -109,21 +93,18 @@ namespace util
|
|||
this->task_id_index = i;
|
||||
}
|
||||
|
||||
void Manager::update_indexes()
|
||||
{
|
||||
void Manager::update_indexes() {
|
||||
this->update_user_id_index();
|
||||
this->update_task_id_index();
|
||||
}
|
||||
|
||||
int Manager::get_user_id()
|
||||
{
|
||||
int Manager::get_user_id() {
|
||||
const int value = this->user_id_index;
|
||||
++this->user_id_index;
|
||||
return value;
|
||||
}
|
||||
|
||||
int Manager::get_task_id()
|
||||
{
|
||||
int Manager::get_task_id() {
|
||||
const int value = this->task_id_index;
|
||||
++this->task_id_index;
|
||||
return value;
|
||||
|
@ -131,15 +112,13 @@ namespace util
|
|||
|
||||
Manager *Manager::INSTANCE = nullptr;
|
||||
|
||||
Manager* Manager::get_instance()
|
||||
{
|
||||
Manager *Manager::get_instance() {
|
||||
if (INSTANCE == nullptr)
|
||||
INSTANCE = new Manager();
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
vector<User*> Manager::get_users() const
|
||||
{
|
||||
vector<User *> Manager::get_users() const {
|
||||
vector<User *> user_vector;
|
||||
for (const auto &pair: this->users)
|
||||
if (pair.second) // Ensure it's not a nullptr
|
||||
|
@ -147,8 +126,7 @@ namespace util
|
|||
return user_vector;
|
||||
}
|
||||
|
||||
vector<Task*> Manager::get_tasks() const
|
||||
{
|
||||
vector<Task *> Manager::get_tasks() const {
|
||||
vector<Task *> task_vector;
|
||||
for (const auto &pair: this->tasks)
|
||||
if (pair.second) // Ensure it's not a nullptr
|
||||
|
@ -156,28 +134,22 @@ namespace util
|
|||
return task_vector;
|
||||
}
|
||||
|
||||
vector<Assignment*> Manager::get_assignments()
|
||||
{
|
||||
return this->assignments;
|
||||
}
|
||||
vector<Assignment *> Manager::get_assignments() { 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;
|
||||
return this->users[id];
|
||||
}
|
||||
|
||||
|
||||
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 user_id)
|
||||
{
|
||||
Vector<Assignment *> Manager::get_assignments_for_user(const int user_id) {
|
||||
Vector<Assignment *> user_assignments;
|
||||
if (this->get_user(user_id) == nullptr)
|
||||
throw Error(401, "Eine solche BenutzerIn existiert nicht.");
|
||||
|
@ -186,8 +158,7 @@ namespace util
|
|||
user_assignments.push_back(as);
|
||||
return user_assignments;
|
||||
}
|
||||
Vector<Assignment*> Manager::get_assignments_for_task(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.");
|
||||
|
@ -197,31 +168,29 @@ namespace util
|
|||
return task_assignments;
|
||||
}
|
||||
|
||||
bool Manager::assignment_exists(int user_id, int task_id) {
|
||||
bool Manager::assignment_exists(const int user_id, const int task_id) const {
|
||||
|
||||
for ( Assignment* as : this->assignments)
|
||||
for (Assignment const *as: this->assignments) {
|
||||
if (as->get_user_id() == user_id && as->get_task_id() == task_id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Manager::add_user(User* user)
|
||||
{
|
||||
void Manager::add_user(User *user) {
|
||||
if (this->users.count(user->get_id()) == 1)
|
||||
return;
|
||||
this->users[user->get_id()] = user;
|
||||
}
|
||||
|
||||
|
||||
void Manager::add_task(Task* task)
|
||||
{
|
||||
void Manager::add_task(Task *task) {
|
||||
if (this->tasks.count(task->get_id()) == 1)
|
||||
return;
|
||||
this->tasks[task->get_id()] = task;
|
||||
}
|
||||
|
||||
void Manager::add_assignment(Assignment* as)
|
||||
{
|
||||
void Manager::add_assignment(Assignment *as) {
|
||||
if (this->get_user(as->get_user_id()) == nullptr)
|
||||
throw Error(401, "Eine solche BenutzerIn existiert nicht.");
|
||||
|
||||
|
@ -231,8 +200,7 @@ 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
|
||||
|
@ -242,14 +210,14 @@ namespace util
|
|||
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(202, "Aufgabe kann nicht gelöscht werden.");
|
||||
}
|
||||
//TODO: Zudem kann eine Aufgabe nur gelöscht werden, wenn Sie nicht Nachfolgerin einer anderen Aufgabe ist -> Ensure its no Child
|
||||
// TODO: Zudem kann eine Aufgabe nur gelöscht werden, wenn Sie nicht Nachfolgerin einer anderen Aufgabe ist ->
|
||||
// Ensure its no Child
|
||||
/*for (Task* task : this->get_tasks()) {
|
||||
for (int child_id : task->get_children()) {
|
||||
if (id == child_id) {
|
||||
|
@ -259,18 +227,17 @@ namespace util
|
|||
}*/
|
||||
if (!this->get_task(id)->is_active()) {
|
||||
throw Error(202, "Aufgabe kann nicht gelöscht werden.");
|
||||
|
||||
}
|
||||
this->tasks.erase(id);
|
||||
}
|
||||
|
||||
void Manager::del_assignment(Assignment& as)
|
||||
{
|
||||
void Manager::del_assignment(Assignment const &as) {
|
||||
if (!this->assignment_exists(as.get_user_id(), as.get_task_id()))
|
||||
throw Error(301, "Eine solche Zuordnung existiert nicht.");
|
||||
int assignment_nr = 0;
|
||||
for (int i = 0; i < this->assignments.size(); ++i) {
|
||||
if (this->assignments[i]->get_user_id() == as.get_user_id() && this->assignments[i]->get_task_id() == as.get_task_id()) {
|
||||
if (this->assignments[i]->get_user_id() == as.get_user_id() &&
|
||||
this->assignments[i]->get_task_id() == as.get_task_id()) {
|
||||
assignment_nr = i;
|
||||
break;
|
||||
}
|
||||
|
@ -278,27 +245,26 @@ namespace util
|
|||
this->assignments.erase(this->assignments.begin() + assignment_nr);
|
||||
};
|
||||
|
||||
void Manager::save()
|
||||
{
|
||||
void Manager::save() {
|
||||
ofstream out = ofstream(this->filename, ios_base::trunc);
|
||||
if (!out)
|
||||
throw Error(603, "Datei kann nicht geschrieben werden.");
|
||||
|
||||
out << "[tasks]\n";
|
||||
for (const auto& task_pair : this->tasks)
|
||||
for (const auto &task_pair: this->tasks) {
|
||||
if (task_pair.second)
|
||||
task_pair.second->write(out);
|
||||
|
||||
}
|
||||
out << "\n[users]\n";
|
||||
for (const auto& user_pair : this->users)
|
||||
for (const auto &user_pair: this->users) {
|
||||
if (user_pair.second)
|
||||
user_pair.second->write(out);
|
||||
|
||||
}
|
||||
out << "\n[assignments]\n";
|
||||
for (Assignment* assignment : this->assignments)
|
||||
for (Assignment *assignment: this->assignments) {
|
||||
if (assignment)
|
||||
assignment->write(out);
|
||||
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
} // util
|
||||
} // namespace util
|
||||
|
|
80
Manager.h
80
Manager.h
|
@ -12,50 +12,118 @@ using models::Assignment;
|
|||
|
||||
namespace util
|
||||
{
|
||||
/// Verwalter von Benutzern, Aufgaben und deren Zuordnungen.
|
||||
class Manager
|
||||
{
|
||||
private:
|
||||
//Singelton Instanz des Managers
|
||||
static Manager* INSTANCE;
|
||||
|
||||
//Container für Zeiger auf alle User anhand der Id
|
||||
map<int, User*> users;
|
||||
//Container für Zeiger auf alle Task anhand der Id
|
||||
map<int, Task*> tasks;
|
||||
/// Speichert Zuordnungen von Benutzern zu Aufgaben.
|
||||
vector<Assignment*> assignments;
|
||||
//Nächste freie User Id
|
||||
int user_id_index;
|
||||
//Nächste frei Task Id
|
||||
int task_id_index;
|
||||
|
||||
//Dateiname für den persistenten Speicher
|
||||
string filename;
|
||||
|
||||
/// Erstellt eine Instanz des Managers und lädt vorhandene Daten.
|
||||
Manager();
|
||||
|
||||
/// Aktualisiert die Benutzer-ID.
|
||||
void update_user_id_index();
|
||||
|
||||
/// Aktualisiert die Aufgaben-ID.
|
||||
void update_task_id_index();
|
||||
|
||||
public:
|
||||
Manager(const Manager&) = delete;
|
||||
Manager& operator=(const Manager&) = delete;
|
||||
|
||||
/// Holt die Singleton-Instanz des Managers.
|
||||
/// @return Ein Zeiger auf die Instanz.
|
||||
static Manager* get_instance();
|
||||
|
||||
/// Speichert die aktuellen Daten in einer Datei.
|
||||
void save();
|
||||
|
||||
/// Aktualisiert die Benutzer- und Aufgaben-IDs.
|
||||
void update_indexes();
|
||||
|
||||
/// Holt eine eindeutige Benutzer-ID.
|
||||
/// @return Eine neue Benutzer-ID.
|
||||
int get_user_id();
|
||||
|
||||
/// Holt eine eindeutige Aufgaben-ID.
|
||||
/// @return Eine neue Aufgaben-ID.
|
||||
int get_task_id();
|
||||
|
||||
/// Holt eine Liste aller Benutzer.
|
||||
/// @return Ein Vektor von Benutzerzeigern.
|
||||
vector<User*> get_users() const;
|
||||
|
||||
/// Holt eine Liste aller Aufgaben.
|
||||
/// @return Ein Vektor von Aufgabenzeigern.
|
||||
vector<Task*> get_tasks() const;
|
||||
|
||||
/// Holt eine Liste aller Zuordnungen.
|
||||
/// @return Ein Vektor von Zuordnungen.
|
||||
vector<Assignment*> get_assignments();
|
||||
|
||||
/// Holt einen Benutzer anhand der ID.
|
||||
/// @param id Die Benutzer-ID.
|
||||
/// @return Ein Zeiger auf den Benutzer oder nullptr.
|
||||
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);
|
||||
|
||||
/// Holt eine Aufgabe anhand der ID.
|
||||
/// @param id Die Aufgaben-ID.
|
||||
/// @return Ein Zeiger auf die Aufgabe oder nullptr.
|
||||
Task* get_task(int id);
|
||||
|
||||
/// Holt alle Zuordnungen für einen Benutzer.
|
||||
/// @param user_id Die Benutzer-ID.
|
||||
/// @return Ein Vektor von Zuordnungen.
|
||||
vector<Assignment *> get_assignments_for_user(int user_id);
|
||||
|
||||
/// Holt alle Zuordnungen für eine Aufgabe.
|
||||
/// @param task_id Die Aufgaben-ID.
|
||||
/// @return Ein Vektor von Zuordnungen.
|
||||
vector<Assignment *> get_assignments_for_task(int task_id);
|
||||
|
||||
/// Prüft, ob eine Zuordnung existiert.
|
||||
/// @param user_id Die Benutzer-ID.
|
||||
/// @param task_id Die Aufgaben-ID.
|
||||
/// @return Wahr, falls die Zuordnung existiert.
|
||||
bool assignment_exists(int user_id, int task_id) const;
|
||||
|
||||
/// Fügt einen neuen Benutzer hinzu.
|
||||
/// @param user Ein Zeiger auf den Benutzer.
|
||||
void add_user(User* user);
|
||||
|
||||
/// Fügt eine neue Aufgabe hinzu.
|
||||
/// @param task Ein Zeiger auf die Aufgabe.
|
||||
void add_task(Task* task);
|
||||
|
||||
/// Fügt eine neue Zuordnung hinzu.
|
||||
/// @param as Ein Zeiger auf die Zuordnung.
|
||||
void add_assignment(Assignment* as);
|
||||
|
||||
/// Löscht einen Benutzer anhand der ID.
|
||||
/// @param id Die Benutzer-ID.
|
||||
void del_user(int id);
|
||||
|
||||
/// Löscht eine Aufgabe anhand der ID.
|
||||
/// @param id Die Aufgaben-ID.
|
||||
void del_task(int id);
|
||||
void del_assignment(Assignment& as);
|
||||
|
||||
/// Löscht eine Zuordnung.
|
||||
/// @param as Die Zuordnung.
|
||||
void del_assignment(Assignment const &as);
|
||||
};
|
||||
} // util
|
||||
#endif // ENVIRONMENT_H
|
||||
|
|
9
Task.cpp
9
Task.cpp
|
@ -39,12 +39,11 @@ namespace models
|
|||
*/
|
||||
bool Task::is_active() const {
|
||||
|
||||
Manager* mgr = Manager::get_instance();
|
||||
for (Task* task : mgr->get_tasks()) {
|
||||
for (int child_id : task->get_children()) {
|
||||
const Manager * mgr = Manager::get_instance();
|
||||
for (const Task * task : mgr->get_tasks()) {
|
||||
for (const int child_id : task->get_children()) {
|
||||
// Aktiver Task ist Child von Einem andern Task -> Nicht aktiv
|
||||
if (this->id == child_id) {
|
||||
// cout << this->id << " = " << child_id << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +74,7 @@ namespace models
|
|||
ostream& operator<<(ostream& os, const Task& t)
|
||||
{
|
||||
os << t.get_id() << " %" << t.get_name() << "% %" << t.get_description() << "%";
|
||||
// Fügt Childen Task mit passenden Lehrzeichen hinzu
|
||||
// Fügt Children Task mit passenden Leerzeichen hinzu
|
||||
if (!t.children.empty())
|
||||
os << " ";
|
||||
for (size_t i = 0; i < t.children.size(); ++i)
|
||||
|
|
1
Task.h
1
Task.h
|
@ -25,7 +25,6 @@ namespace models
|
|||
explicit Task(const AddTask& t);
|
||||
|
||||
ostream& write(ostream& stream) const;
|
||||
istream& read(istream&);
|
||||
|
||||
int get_id() const;
|
||||
bool is_active() const;
|
||||
|
|
1
Util.cpp
1
Util.cpp
|
@ -30,7 +30,6 @@ namespace util {
|
|||
}
|
||||
|
||||
vector<int> Util::read_numbers(istream &is) {
|
||||
int i;
|
||||
string until_eol;
|
||||
getline(is, until_eol);
|
||||
|
||||
|
|
1
main.cpp
1
main.cpp
|
@ -35,6 +35,7 @@ const vector<SubCommand*> handlers = {
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//TODO:: THis needs to be removed?
|
||||
if (argc < 2)
|
||||
{
|
||||
cout << "There are not enough args!\n";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue