-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_removebook.cpp
More file actions
114 lines (94 loc) · 3.75 KB
/
window_removebook.cpp
File metadata and controls
114 lines (94 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "window_removebook.h"
#include "ui_window_removebook.h"
#include <QJsonObject>
#include <QString>
#include <QFile>
#include <QDebug>
#include <QJsonParseError>
#include<QJsonArray>
#include<QJsonValueRef>
#include <QJsonDocument>
#include <QMessageBox>
#include<QDir>
//==============================================================================================
Window_RemoveBook::Window_RemoveBook(QWidget *parent) :
QDialog(parent),
ui(new Ui::Window_RemoveBook)
{
ui->setupUi(this);
ui->lineEdit_BookId->setFocus();
}
Window_RemoveBook::~Window_RemoveBook()
{
delete ui;
}
void Window_RemoveBook::on_pushButton_RemoveBook_clicked()
{
const QString & BookId = ui->lineEdit_BookId->text();
QDir AddedBook;
AddedBook.cd("..");
AddedBook.cd("Login_MY_FINAL");
AddedBook.cd("RowData");
QFile AddedBooksFile(AddedBook.path()+"/AddedBooks.json");
AddedBooksFile.open(QIODevice::ReadWrite | QIODevice::Text);
QByteArray B = AddedBooksFile.readAll();
QJsonDocument D = QJsonDocument::fromJson(B);
QJsonObject Obj = D.object();
if(Obj.find(BookId)==Obj.end()){
QMessageBox::warning(this," ","Book not found!");
return;
}
AddedBooksFile.close();
QJsonObject empty ={};
QDir account;
account.cd("..");
account.cd("Login_MY_FINAL");
account.cd("RowData");
QFile AccountsFile (account.path()+"/accounts.json");
AccountsFile.open(QIODevice::ReadWrite);
QJsonDocument jsonDocAcc = QJsonDocument::fromJson( AccountsFile.readAll() );
QJsonObject Accounts_Obj=jsonDocAcc.object();
AccountsFile.close();
foreach(QJsonValue x,Accounts_Obj){
QJsonObject RentedBook=(x.toObject())["RentedBooks"].toObject();
if(RentedBook.find(BookId)!=RentedBook.end()){
QString MemberName= (x.toObject())["Name"].toString();
QString Username= (x.toObject())["Username"].toString();
QString date_added_account= (x.toObject())["date_added"].toString();
QString ExpireDate= (x.toObject())["ExpireDate"].toString();
int AccountType= (x.toObject())["AccountType"].toInt();
QString Password=(x.toObject())["Password"].toString();
QString MemberId = (x.toObject()["id"]).toString();
RentedBook.remove(BookId);
QJsonObject newAccount = { {"Name", MemberName},
{"Username", Username},
{"id", MemberId},
{"date_added", date_added_account},
{"ExpireDate", ExpireDate},
{"AccountType", AccountType},
{"Password", Password},
{"RentedBooks",RentedBook}
};
Accounts_Obj.remove(MemberId);
Accounts_Obj[MemberId]=newAccount;
QMessageBox::information(this, "", "Book was rented,also removed from the account.");
QJsonDocument FinalAccounts(Accounts_Obj);
if( !AccountsFile.open( QIODevice::WriteOnly ) ) //write json content to file.
{
qDebug()<<"error opening file for write.\n";
}
AccountsFile.write(FinalAccounts.toJson());
AccountsFile.close();
break;
}
}
Obj.remove(BookId);
QJsonDocument FinalD(Obj);
if( !AddedBooksFile.open( QIODevice::WriteOnly ) ) //write json content to file.
{
qDebug()<<"error opening file for write.\n";
}
AddedBooksFile.write(FinalD.toJson());
AddedBooksFile.close();
QMessageBox::information(this," ","Book removed successfully!");
}