Register CSS files via API
[grauphel.git] / controller / apicontroller.php
index 691d8abbe0cdaf99b3586c922c31194f2a2fc460..23a47e796abc0fff201ccde79bb046561137edd0 100644 (file)
@@ -16,6 +16,7 @@ namespace OCA\Grauphel\Controller;
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\JSONResponse;
 
+use \OCA\Grauphel\Lib\Client;
 use \OCA\Grauphel\Lib\NoteStorage;
 use \OCA\Grauphel\Lib\OAuth;
 use \OCA\Grauphel\Lib\OAuthException;
@@ -105,6 +106,12 @@ class ApiController extends Controller
             'api-version' => '1.0',
         );
 
+        $cl = new Client();
+        $client = $cl->getClient();
+        if ($client !== false) {
+            $data['oauth_authorize_url'] .= '?client=' . urlencode($client);
+        }
+
         if ($authenticated) {
             $data['user-ref'] = array(
                 'api-ref' => $urlGen->getAbsoluteURL(
@@ -112,7 +119,7 @@ class ApiController extends Controller
                         'grauphel.api.user', array('username' => $username)
                     )
                 ),
-                'href' => null,//FIXME
+                'href' => null,
             );
         }
 
@@ -160,7 +167,9 @@ class ApiController extends Controller
                         'grauphel.api.notes', array('username' => $username)
                     )
                 ),
-                'href'    => null,
+                'href'    => $this->deps->urlGen->getAbsoluteURL(
+                    $this->deps->urlGen->linkToRoute('grauphel.gui.index')
+                ),
             ),
             'latest-sync-revision' => $syncdata->latestSyncRevision,
             'current-sync-guid'    => $syncdata->currentSyncGuid,
@@ -207,7 +216,7 @@ class ApiController extends Controller
             )
         );
         $syncdata = $this->notes->loadSyncData();
-        
+
         $res = $this->handleNoteSave($username, $syncdata);
         if ($res instanceof \OCP\AppFramework\Http\Response) {
             return $res;
@@ -249,8 +258,8 @@ class ApiController extends Controller
             return;
         }
 
-        //note that we have more data in $arPut than just our JSON
-        // request object merges it with other data
+        //Note that we have more data in $arPut than just our JSON.
+        // The request object merges it with other data.
         $arPut = $this->request->put;
 
         //structural validation
@@ -278,23 +287,31 @@ class ApiController extends Controller
         }
 
         //update
-        ++$syncdata->latestSyncRevision;
-        foreach ($arPut['note-changes'] as $noteUpdate) {
-            //owncloud converts object to array, so we reverse
-            $noteUpdate = (object) $noteUpdate;
-
-            $note = $this->notes->load($noteUpdate->guid);
-            if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
-                $this->notes->delete($noteUpdate->guid);
-            } else {
-                $this->notes->update(
-                    $note, $noteUpdate, $syncdata->latestSyncRevision
-                );
-                $this->notes->save($note);
+        $db = \OC::$server->getDatabaseConnection();
+        $db->beginTransaction();
+        try {
+            ++$syncdata->latestSyncRevision;
+            foreach ($arPut['note-changes'] as $noteUpdate) {
+                //owncloud converts object to array, so we reverse
+                $noteUpdate = (object) $noteUpdate;
+
+                $note = $this->notes->load($noteUpdate->guid);
+                if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
+                    $this->notes->delete($noteUpdate->guid);
+                } else {
+                    $this->notes->update(
+                        $note, $noteUpdate, $syncdata->latestSyncRevision
+                    );
+                    $this->notes->save($note);
+                }
             }
-        }
 
-        $this->notes->saveSyncData($syncdata);
+            $this->notes->saveSyncData($syncdata);
+            $db->commit();
+        } catch (\DatabaseException $e) {
+            $db->rollBack();
+            throw $e;
+        }
     }
 
     /**