@@ -36,9 +36,9 @@ bool NextcloudDeckService::isEnabled() {
36
36
this ->cloudConnection .getNextcloudDeckEnabled ();
37
37
}
38
38
39
- int NextcloudDeckService::createCard (const QString& title, const QString& description,
40
- QDateTime* dueDateTime) {
41
- int cardId = -1 ;
39
+ int NextcloudDeckService::storeCard (const QString& title, const QString& description,
40
+ QDateTime* dueDateTime, int cardId ) {
41
+ int resultCardId = -1 ;
42
42
auto * manager = new QNetworkAccessManager ();
43
43
QEventLoop loop;
44
44
QTimer timer;
@@ -51,14 +51,28 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
51
51
// 10 sec timeout for the request
52
52
timer.start (10000 );
53
53
54
- QUrl url (serverUrl + " /index.php/apps/deck/api/v1.1/boards/" + QString::number (this ->boardId ) +
55
- " /stacks/" + QString::number (this ->stackId ) + " /cards" );
54
+ QString urlString = serverUrl + " /index.php/apps/deck/api/v1.1/boards/" +
55
+ QString::number (this ->boardId ) + " /stacks/" +
56
+ QString::number (this ->stackId ) + " /cards" ;
57
+ bool isUpdate = (cardId > 0 );
58
+
59
+ if (isUpdate) {
60
+ // URL for updating an existing card
61
+ urlString += " /" + QString::number (cardId);
62
+ }
63
+
64
+ const QUrl url (urlString);
65
+
56
66
qDebug () << __func__ << " - 'url': " << url;
67
+ qDebug () << __func__ << " - 'isUpdate': " << isUpdate;
57
68
58
69
QJsonObject bodyJson;
59
70
bodyJson[" title" ] = title;
60
71
bodyJson[" type" ] = " plain" ;
61
- bodyJson[" order" ] = 0 ;
72
+
73
+ if (!isUpdate) {
74
+ bodyJson[" order" ] = 0 ;
75
+ }
62
76
63
77
if (description != " " ) {
64
78
bodyJson[" description" ] = description;
@@ -90,7 +104,11 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
90
104
networkRequest.setRawHeader (" OCS-APIRequest" , " true" );
91
105
addAuthHeader (networkRequest);
92
106
93
- reply = manager->post (networkRequest, bodyJsonDoc.toJson ());
107
+ if (isUpdate) {
108
+ reply = manager->put (networkRequest, bodyJsonDoc.toJson ());
109
+ } else {
110
+ reply = manager->post (networkRequest, bodyJsonDoc.toJson ());
111
+ }
94
112
95
113
loop.exec ();
96
114
@@ -106,16 +124,23 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
106
124
107
125
QJsonDocument jsonDoc = QJsonDocument::fromJson (data);
108
126
QJsonObject jsonObject = jsonDoc.object ();
109
- cardId = jsonObject[" id" ].toInt ();
127
+ resultCardId = jsonObject[" id" ].toInt ();
128
+
129
+ // If we're updating, use the original cardId if the response doesn't contain one
130
+ if (isUpdate && resultCardId <= 0 ) {
131
+ resultCardId = cardId;
132
+ }
110
133
111
- qDebug () << __func__ << " - 'cardId ': " << cardId ;
134
+ qDebug () << __func__ << " - 'resultCardId ': " << resultCardId ;
112
135
qDebug () << __func__ << " - 'jsonDoc': " << jsonDoc;
113
136
} else {
114
137
QString errorString = reply->errorString ();
115
- Utils::Gui::warning (nullptr , tr (" Error while creating card" ),
116
- tr (" Creating a card failed with status code %1 and message: %2" )
138
+ QString operation = isUpdate ? tr (" updating" ) : tr (" creating" );
139
+ Utils::Gui::warning (nullptr , tr (" Error while %1 card" ).arg (operation),
140
+ tr (" %1 a card failed with status code %2 and message: %3" )
141
+ .arg (operation.left (1 ).toUpper () + operation.mid (1 ))
117
142
.arg (QString::number (returnStatusCode), errorString),
118
- " nextcloud-deck-create-failed" );
143
+ " nextcloud-deck-create-update- failed" );
119
144
120
145
qDebug () << __func__ << " - error: " << returnStatusCode;
121
146
qDebug () << __func__ << " - 'errorString': " << errorString;
@@ -125,7 +150,7 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
125
150
reply->deleteLater ();
126
151
delete (manager);
127
152
128
- return cardId ;
153
+ return resultCardId ;
129
154
}
130
155
131
156
QString NextcloudDeckService::getCardLinkForId (int cardId) {
@@ -241,7 +266,7 @@ QList<NextcloudDeckService::Board> NextcloudDeckService::getBoards() {
241
266
return boards;
242
267
}
243
268
244
- QList< NextcloudDeckService::Card> NextcloudDeckService::getCards () {
269
+ QHash< int , NextcloudDeckService::Card> NextcloudDeckService::getCards () {
245
270
auto * manager = new QNetworkAccessManager ();
246
271
QEventLoop loop;
247
272
QTimer timer;
@@ -280,7 +305,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
280
305
reply = manager->get (networkRequest);
281
306
282
307
loop.exec ();
283
- QList< NextcloudDeckService::Card> cards;
308
+ QHash< int , NextcloudDeckService::Card> cards;
284
309
285
310
// if we didn't get a timeout let us return the content
286
311
if (timer.isActive ()) {
@@ -344,7 +369,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
344
369
}
345
370
346
371
qDebug () << __func__ << " - found card: " << card;
347
- cards. append ( card) ;
372
+ cards[card. id ] = card;
348
373
}
349
374
350
375
// We found our stack, no need to continue
0 commit comments