mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:43:08 +02:00
feat: refactor registration code
This commit is contained in:
parent
76d040a1f1
commit
71e838a1be
@ -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": ""
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -8,15 +8,5 @@
|
||||
"email": "",
|
||||
"password": "",
|
||||
"userId": "2"
|
||||
},
|
||||
"65": {
|
||||
"email": "",
|
||||
"password": "",
|
||||
"userId": "65"
|
||||
},
|
||||
"77": {
|
||||
"email": "",
|
||||
"password": "",
|
||||
"userId": "77"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user