feat: refactor registration code

This commit is contained in:
Krzysztof Rudnicki 2022-04-06 16:31:31 +02:00
parent 76d040a1f1
commit 71e838a1be
10 changed files with 108 additions and 78 deletions

View File

@ -1,30 +1,16 @@
{ {
"": { "1": {
"blogId": "",
"items": [
],
"ownerId": "1",
"title": ""
}, },
"2": { "2": {
"blogId": "2", "blogId": "2",
"items": [ "items": [
{
"content": "fassfda",
"datetime": "16:29:27 Wed Apr 6 2022",
"title": "saa"
}
], ],
"ownerId": "2", "ownerId": "2",
"title": "" "title": ""
},
"5": {
"blogId": "5",
"items": [
],
"ownerId": "65",
"title": "SAD"
},
"77": {
"blogId": "77",
"items": [
],
"ownerId": "77",
"title": ""
} }
} }

View File

@ -8,15 +8,5 @@
"email": "", "email": "",
"password": "", "password": "",
"userId": "2" "userId": "2"
},
"65": {
"email": "",
"password": "",
"userId": "65"
},
"77": {
"email": "",
"password": "",
"userId": "77"
} }
} }

View File

@ -28,7 +28,7 @@ void MainWindow::defineConnections() const
connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin); connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin);
} }
void MainWindow::exit() void MainWindow::exit() const
{ {
QApplication::quit(); QApplication::quit();
} }
@ -38,46 +38,75 @@ MainWindow::~MainWindow()
delete ui; // in the destructor, we delete the ui 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 blogs = readJsonFile("blogs.json");
QJsonObject users = readJsonFile("user.json"); QJsonObject users = readJsonFile("user.json");
QString id = ui->inputId->text(); QString id = ui->inputId->text();
if(id == "") if (stringEmpty(id, "THIS USER ID IS EMPTY!")) return;
{ if (idExists(id, users, "THIS USER ID IS ALREADY TAKEN!")) return;
outputMessageBox("THIS ID IS EMPTY!");
return; QString blogId = ui -> inputBlogID->text();
} if (stringEmpty(blogId, "THIS BLOG ID IS EMPTY!")) return;
if(users.find(id) == users.end()) if (idExists(blogId, blogs, "THIS BLOG ID IS ALREADY TAKEN!")) return;
{
QString mail = ui->inputMail->text(); registerUser(users, blogs, id, blogId);
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!");
} }
void MainWindow::goToLogin() void MainWindow::goToLogin()
@ -87,9 +116,6 @@ void MainWindow::goToLogin()
hide(); hide();
} }
void MainWindow::test()
{
qDebug() << "pls work";
}

View File

@ -30,14 +30,21 @@ private slots:
private: private:
void newDocument(); void newDocument() const;
void open(); void open() const;
void saveRegisteredUser() const; void saveRegisteredUser();
void goToLogin(); void goToLogin();
void defineConnections() const; void defineConnections() const;
void test(); void registerUser(QJsonObject &users, QJsonObject &blogs, const QString &blogId, const QString &id);
void exit(); 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" Ui::MainWindow *ui; // we point to ui class "mainwindow.ui"
QString currentFile = ""; // current file we work with QString currentFile = ""; // current file we work with
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -10,7 +10,7 @@
QJsonObject readJsonFile(const QString title) QJsonObject readJsonFile(const QString title)
{ {
QFile file(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(); QByteArray bytes = file.readAll();
file.close(); file.close();
QJsonDocument document = QJsonDocument::fromJson( bytes ); QJsonDocument document = QJsonDocument::fromJson( bytes );
@ -42,5 +42,25 @@ void exit()
QApplication::quit(); 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 #endif

View File

@ -7,6 +7,7 @@ QJsonObject readJsonFile(const QString title);
void saveJsonFile(QJsonObject &users, const QString name); void saveJsonFile(QJsonObject &users, const QString name);
void outputMessageBox(const QString messageBoxText); void outputMessageBox(const QString messageBoxText);
void exit(); void exit();
bool stringEmpty(const QString &string, const QString &messageBoxMessage);
bool idExists(const QString &id, const QJsonObject &json, const QString &message);
#endif // UNIVERSALFUNCTIONS_H #endif // UNIVERSALFUNCTIONS_H