@@ -36,9 +36,9 @@ bool NextcloudDeckService::isEnabled() {
3636           this ->cloudConnection .getNextcloudDeckEnabled ();
3737}
3838
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 ;
4242    auto * manager = new  QNetworkAccessManager ();
4343    QEventLoop loop;
4444    QTimer timer;
@@ -51,14 +51,28 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
5151    //  10 sec timeout for the request
5252    timer.start (10000 );
5353
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+ 
5666    qDebug () << __func__ << "  - 'url': " 
67+     qDebug () << __func__ << "  - 'isUpdate': " 
5768
5869    QJsonObject bodyJson;
5970    bodyJson[" title" 
6071    bodyJson[" type" " plain" 
61-     bodyJson[" order" 0 ;
72+ 
73+     if  (!isUpdate) {
74+         bodyJson[" order" 0 ;
75+     }
6276
6377    if  (description != " " 
6478        bodyJson[" description" 
@@ -90,7 +104,11 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
90104    networkRequest.setRawHeader (" OCS-APIRequest" " true" 
91105    addAuthHeader (networkRequest);
92106
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+     }
94112
95113    loop.exec ();
96114
@@ -106,16 +124,23 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
106124
107125            QJsonDocument jsonDoc = QJsonDocument::fromJson (data);
108126            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+             }
110133
111-             qDebug () << __func__ << "  - 'cardId ': " cardId ;
134+             qDebug () << __func__ << "  - 'resultCardId ': " resultCardId ;
112135            qDebug () << __func__ << "  - 'jsonDoc': " 
113136        } else  {
114137            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 ))
117142                                    .arg (QString::number (returnStatusCode), errorString),
118-                                 " nextcloud-deck-create-failed" 
143+                                 " nextcloud-deck-create-update- failed" 
119144
120145            qDebug () << __func__ << "  - error: " 
121146            qDebug () << __func__ << "  - 'errorString': " 
@@ -125,7 +150,7 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
125150    reply->deleteLater ();
126151    delete  (manager);
127152
128-     return  cardId ;
153+     return  resultCardId ;
129154}
130155
131156QString NextcloudDeckService::getCardLinkForId (int  cardId) {
@@ -241,7 +266,7 @@ QList<NextcloudDeckService::Board> NextcloudDeckService::getBoards() {
241266    return  boards;
242267}
243268
244- QList< NextcloudDeckService::Card> NextcloudDeckService::getCards () {
269+ QHash< int ,  NextcloudDeckService::Card> NextcloudDeckService::getCards () {
245270    auto * manager = new  QNetworkAccessManager ();
246271    QEventLoop loop;
247272    QTimer timer;
@@ -280,7 +305,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
280305    reply = manager->get (networkRequest);
281306
282307    loop.exec ();
283-     QList< NextcloudDeckService::Card> cards;
308+     QHash< int ,  NextcloudDeckService::Card> cards;
284309
285310    //  if we didn't get a timeout let us return the content
286311    if  (timer.isActive ()) {
@@ -344,7 +369,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
344369                            }
345370
346371                            qDebug () << __func__ << "  - found card: " 
347-                             cards. append ( card) ;
372+                             cards[card. id ] =  card;
348373                        }
349374
350375                        //  We found our stack, no need to continue
0 commit comments