Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/184539

Change subject: WIP crud for boards and widget instances
......................................................................

WIP crud for boards and widget instances

Change-Id: Ic947f5381ced0845d2871c8d97e1f2fefe688307
---
M persistence.js
M routes/user.js
A routes/widget.js
M server.js
4 files changed, 112 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash 
refs/changes/39/184539/1

diff --git a/persistence.js b/persistence.js
index e0268bc..6df6ed4 100644
--- a/persistence.js
+++ b/persistence.js
@@ -42,5 +42,62 @@
                                });
                        });
                });
+       },
+       /**
+        * Saves a widget configuration
+        * @param Object instance should have userId and configuration set.  If 
id
+        * is not set, this function creates a new instance and sets the id.
+        * @return Object Promise that fulfills on completion or rejects with 
error
+        */
+       saveWidgetInstance: function( instance ) {
+               var insert = 'INSERT INTO dash_widget_instance ( widget_id, 
owner_id, display_name, is_shared, configuration ) VALUES ( ?, ?, ?, ?, ? ); 
SELECT LAST_INSERT_ID() AS id',
+                       update = 'UPDATE dash_widget_instance set display_name 
= ?, is_shared = ?, configuration = ? WHERE id = ? AND owner_id = ?; SELECT 
row_count() AS affected',
+                       insertParams = [ instance.widgetId, instance.ownerId, 
instance.displayName, instance.isShared ? 1 : 0, instance.configuration ],
+                       updateParams = [ instance.displayName, 
instance.isShared ? 1 : 0, instance.configuration, instance.id, 
instance.ownerId ],
+                       connection = getConnection();
+
+               if ( instance.id ) {
+                       return connection.query( update, updateParams ).then( 
function( dbResults ) {
+                               if ( dbResults[0][0].affected !== 1 ) {
+                                       // Either the instance doesn't exist or 
it's not ours
+                                       console.log('problem updating!');
+                                       throw new Error('Instance ' + 
instance.id  + 'with owner ' + instance.ownerId + ' not found' );
+                               }
+                       });
+               }
+               console.log('Gonna insert!');
+               return connection.query( insert, insertParams ).then( function( 
dbResults ) {
+                       instance.id = dbResults[0][0].id;
+               });
+       },
+       saveBoard: function( board ) {
+
+       },
+       /**
+        * @param number boardId ID of board to fetch
+        * @param number userId local ID of user
+        * @returns Object Promise that resolves with a JSON representation of 
all
+        * board widgets or rejects with error
+        */
+       getBoard: function( boardId, userId ) {
+
+       },
+       listWidgets: function() {
+               var connection = getConnection();
+               return connection.query( 'SELECT id, code, display_name, 
preview_path FROM dash_widget').then( function( dbResults ) {
+                       console.log(require('util').inspect(dbResults));
+                       var rows = dbResults[0],
+                               count = rows.length,
+                               i,
+                               result = {};
+                       for ( i = 0; i < count; i++ ) {
+                               result[rows[i].code] = {
+                                       id: rows[i].id,
+                                       displayName: rows[i].display_name,
+                                       previewPath: rows[i].preview_path
+                               };
+                       }
+                       return result;
+               });
        }
 };
diff --git a/routes/user.js b/routes/user.js
index 743f8ec..5b8a56c 100644
--- a/routes/user.js
+++ b/routes/user.js
@@ -1,3 +1,5 @@
+var persistence = require( '../persistence.js' );
+
 module.exports = {
        info: function( req, res ) {
                if ( !req.session || !req.session.passport || 
!req.session.passport.user ) {
@@ -12,5 +14,15 @@
                        id: user.localId,
                        defaultBoard: user.defaultBoard
                } );
+       },
+       boards: function( req, res ) {
+               if ( !req.session || !req.session.passport || 
!req.session.passport.user ) {
+                       res.json( false );
+                       return;
+               }
+
+               var user = req.session.passport.user;
+
+               res.json( persistence.getBoards( user.id ) );
        }
 };
diff --git a/routes/widget.js b/routes/widget.js
new file mode 100644
index 0000000..de68317
--- /dev/null
+++ b/routes/widget.js
@@ -0,0 +1,34 @@
+var persistence = require( '../persistence.js' );
+
+module.exports = {
+       list: function( req, res ) {
+               persistence.listWidgets().then( function ( widgets ) {
+                       res.json( widgets );
+               });
+       },
+       saveInstance: function( req, res ) {
+               console.log('trying to save instance');
+               res.json(true);
+               return;
+               //console.log(require('util').inspect(req.body));
+               if ( !req.session || !req.session.passport || 
!req.session.passport.user ) {
+                       res.json( { error: 'Error: Not logged in' } );
+                       return;
+               }
+               var instance = {
+                       widgetId: req.body.widgetId,
+                       ownerId: req.session.passport.user.localId,
+                       displayName: req.body.displayName,
+                       isShared: req.body.isShared,
+                       configuration: JSON.stringify( req.body.configuration )
+               };
+               if ( req.params.id ) {
+                       instance.id = req.params.id;
+               }
+               persistence.saveWidgetInstance( instance ).then( function() {
+                       res.json( true );
+               }, function( error ) {
+                       res.json( error );
+               });
+       }
+};
\ No newline at end of file
diff --git a/server.js b/server.js
index 66e43d4..595aa3b 100644
--- a/server.js
+++ b/server.js
@@ -8,6 +8,7 @@
     logger            = require( './logger.js' ),
     config            = require( './config.js' ),
     persistence       = require( './persistence.js' ),
+    bodyParser        = require( 'body-parser' ),
     server,
     serverConfig;
 
@@ -65,6 +66,8 @@
 
 app.use( passport.initialize() );
 app.use( passport.session() );
+app.use( bodyParser.json() );
+app.use( bodyParser.urlencoded( { extended: true } ) );
 
 app.set( 'views', __dirname + '/src/components' );
 app.set( 'view options', { pretty: true } );
@@ -72,6 +75,12 @@
 app.get( '/data/:widget', routes.data );
 app.get( '/metadata/:widget', routes.metadata );
 app.get( '/user/info', routes.user.info );
+app.get( '/user/boards', routes.user.boards );
+app.get( '/widget', routes.widget.list );
+app.post( '/widget-instance', routes.widget.saveInstance );
+app.put( '/widget-instance/:id', routes.widget.saveInstance );
+//app.get( '/widget-instance/:id', routes.widget.getInstance );
+
 
 /*jslint -W024*/
 app.use( express.static( __dirname + ( config.debug ? '/src' : '/dist' ) ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/184539
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic947f5381ced0845d2871c8d97e1f2fefe688307
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/dash
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to