diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/blogs.json b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/blogs.json index 846a2fcc..bdb0cae9 100644 --- a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/blogs.json +++ b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/blogs.json @@ -1,30 +1,16 @@ { - "": { - "blogId": "", - "items": [ - ], - "ownerId": "1", - "title": "" + "1": { }, "2": { "blogId": "2", "items": [ + { + "content": "fassfda", + "datetime": "16:29:27 Wed Apr 6 2022", + "title": "saa" + } ], "ownerId": "2", "title": "" - }, - "5": { - "blogId": "5", - "items": [ - ], - "ownerId": "65", - "title": "SAD" - }, - "77": { - "blogId": "77", - "items": [ - ], - "ownerId": "77", - "title": "" } } diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/lab1 b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/lab1 index 2efc6cb1..e8e55c2e 100755 Binary files a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/lab1 and b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/lab1 differ diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/mainwindow.o b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/mainwindow.o index ff678546..0bc18379 100644 Binary files a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/mainwindow.o and b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/mainwindow.o differ diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/moc_mainwindow.o b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/moc_mainwindow.o index 85da6b7d..d1eecdfe 100644 Binary files a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/moc_mainwindow.o and b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/moc_mainwindow.o differ diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/universalFunctions.o b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/universalFunctions.o index d6badb77..9bb57d0a 100644 Binary files a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/universalFunctions.o and b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/universalFunctions.o differ diff --git a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/user.json b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/user.json index 5608335f..9436ac44 100644 --- a/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/user.json +++ b/EGUI/build-lab1-Desktop_Qt_6_2_4_GCC_64bit-Debug/user.json @@ -8,15 +8,5 @@ "email": "", "password": "", "userId": "2" - }, - "65": { - "email": "", - "password": "", - "userId": "65" - }, - "77": { - "email": "", - "password": "", - "userId": "77" } } diff --git a/EGUI/lab1/mainwindow.cpp b/EGUI/lab1/mainwindow.cpp index c223aa64..53c3c95f 100644 --- a/EGUI/lab1/mainwindow.cpp +++ b/EGUI/lab1/mainwindow.cpp @@ -28,7 +28,7 @@ void MainWindow::defineConnections() const connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin); } -void MainWindow::exit() +void MainWindow::exit() const { QApplication::quit(); } @@ -38,46 +38,75 @@ MainWindow::~MainWindow() delete ui; // in the destructor, we delete the ui } -void MainWindow::saveRegisteredUser() const +QJsonObject MainWindow::createUserObject(const QString &mail, const QString &password, const QString &userId) const +{ + QJsonObject user; + user["userId"] = userId; + // userId - unique user id - text obtained from the user during user registration + user["email"] = mail; + // email - e-mail address of the user + user["password"] = password; + // password - password provided by the user + return user; +} + +QJsonObject MainWindow::insertUserObject(QJsonObject &users, const QString &id) +{ + QString mail = ui->inputMail->text(); + QString password = ui->inputPassword->text(); + QJsonObject user = createUserObject(mail, password, id); + users.insert(id, user); + return users; +} + +QJsonObject MainWindow::createBlogObject(const QString &id, const QString &ownerId) const +{ + QJsonObject blog; + blog["blogId"] = id; + QString title = ui -> inputBlogTitle -> text(); + blog["title"] = title; + blog["ownerId"] = ownerId; + QJsonArray items; + blog["items"] = items; + return blog; +} + + +QJsonObject MainWindow::insertBlogObject(QJsonObject &blogs, const QString &blogId, const QString &ownerId) +{ + QJsonObject blog = createBlogObject(blogId, ownerId); + blogs.insert(blogId, blog); + return blogs; +} + + +void MainWindow::saveFiles(QJsonObject &users, QJsonObject &blogs) +{ + saveJsonFile(blogs, "blogs.json"); + saveJsonFile(users, "user.json"); +} + +void MainWindow::registerUser(QJsonObject &users, QJsonObject &blogs, const QString &id, const QString &blogId) +{ + users = insertUserObject(users, id); + blogs = insertBlogObject(blogs, blogId, id); + saveFiles(users, blogs); +} + +void MainWindow::saveRegisteredUser() { QJsonObject blogs = readJsonFile("blogs.json"); QJsonObject users = readJsonFile("user.json"); + QString id = ui->inputId->text(); - if(id == "") - { - outputMessageBox("THIS ID IS EMPTY!"); - return; - } - if(users.find(id) == users.end()) - { - QString mail = ui->inputMail->text(); - QString password = ui->inputPassword->text(); - qDebug() << mail << " " << password; - QJsonObject user; - user["userId"] = id; - user["email"] = mail; - user["password"]=password; - users.insert(id, user); - /* - userId - unique user id - text obtained from the user during user registration - email - e-mail address of the user - password - password provided by the user - */ - QJsonObject blog; - QString blogTitle = ui -> inputBlogTitle->text(); - QString blogId = ui -> inputBlogID->text(); - if(blogs.find(blogId) == blogs.end()) - { - blog["ownerId"] = id; - blog["title"] = blogTitle; - blog["blogId"] = blogId; - QJsonArray items; - blog["items"] = items; - blogs.insert(blogId, blog); - saveJsonFile(blogs, "blogs.json"); - saveJsonFile(users, "user.json"); - }else outputMessageBox("THIS BLOG ID IS ALREADY TAKEN!"); - }else outputMessageBox("THIS ID IS ALREADY TAKEN!"); + if (stringEmpty(id, "THIS USER ID IS EMPTY!")) return; + if (idExists(id, users, "THIS USER ID IS ALREADY TAKEN!")) return; + + QString blogId = ui -> inputBlogID->text(); + if (stringEmpty(blogId, "THIS BLOG ID IS EMPTY!")) return; + if (idExists(blogId, blogs, "THIS BLOG ID IS ALREADY TAKEN!")) return; + + registerUser(users, blogs, id, blogId); } void MainWindow::goToLogin() @@ -87,9 +116,6 @@ void MainWindow::goToLogin() hide(); } -void MainWindow::test() -{ - qDebug() << "pls work"; -} + diff --git a/EGUI/lab1/mainwindow.h b/EGUI/lab1/mainwindow.h index 56431edf..ea510b0d 100644 --- a/EGUI/lab1/mainwindow.h +++ b/EGUI/lab1/mainwindow.h @@ -30,14 +30,21 @@ private slots: private: - void newDocument(); - void open(); - void saveRegisteredUser() const; + void newDocument() const; + void open() const; + void saveRegisteredUser(); void goToLogin(); void defineConnections() const; - void test(); - void exit(); + void registerUser(QJsonObject &users, QJsonObject &blogs, const QString &blogId, const QString &id); + QJsonObject createUserObject(const QString &mail, const QString &password, const QString &userId) const; + QJsonObject createBlogObject(const QString &id, const QString &ownerId) const; + void exit() const; + QJsonObject insertUserObject(QJsonObject &users, const QString &id); + void saveFiles(QJsonObject &users, QJsonObject &blogs); + QJsonObject insertBlogObject(QJsonObject &blogs, const QString &blogId, const QString &ownerId); Ui::MainWindow *ui; // we point to ui class "mainwindow.ui" QString currentFile = ""; // current file we work with + + }; #endif // MAINWINDOW_H diff --git a/EGUI/lab1/universalFunctions.cpp b/EGUI/lab1/universalFunctions.cpp index 801a56a1..d4dea0d4 100644 --- a/EGUI/lab1/universalFunctions.cpp +++ b/EGUI/lab1/universalFunctions.cpp @@ -10,7 +10,7 @@ QJsonObject readJsonFile(const QString title) { QFile file(title); - file.open(QIODevice::ReadWrite); + file.open(QIODevice::ReadWrite); // If the file does not exist we create it QByteArray bytes = file.readAll(); file.close(); QJsonDocument document = QJsonDocument::fromJson( bytes ); @@ -42,5 +42,25 @@ void exit() QApplication::quit(); } +bool stringEmpty(const QString &string, const QString &messageBoxMessage) +{ + if(string == "") + { + outputMessageBox(messageBoxMessage); + return true; + } + return false; +} + +bool idExists(const QString &id, const QJsonObject &json, const QString &message) +{ + qDebug() << id; + if (json.find(id) != json.end()) + { + outputMessageBox(message); + return true; + } + return false; +} #endif diff --git a/EGUI/lab1/universalFunctions.h b/EGUI/lab1/universalFunctions.h index 28377476..b0517b6b 100644 --- a/EGUI/lab1/universalFunctions.h +++ b/EGUI/lab1/universalFunctions.h @@ -7,6 +7,7 @@ QJsonObject readJsonFile(const QString title); void saveJsonFile(QJsonObject &users, const QString name); void outputMessageBox(const QString messageBoxText); void exit(); - +bool stringEmpty(const QString &string, const QString &messageBoxMessage); +bool idExists(const QString &id, const QJsonObject &json, const QString &message); #endif // UNIVERSALFUNCTIONS_H