/*jshint bitwise:true, browser:false, camelcase:true, curly:true, devel:false, eqeqeq:false, forin:true, immed:true, indent:4, newcap:true, noarg:true, noempty:true, nonew:true, quotmark:true, regexp:false, strict:true, trailing:true, undef:true, unused:true, node:true */
'use strict';
var express = require('express');
var rid = require('readable-id');
var sqlite3 = require('sqlite3'); //.verbose();

var router = express.Router();
var dbFile = 'hiit.db';

function _getDb(res) {
    var db = new sqlite3.Database(dbFile, function (error) {
        if (error) {
            console.error('Cannot open database "' + dbFile + '": ' + error);
            res.json({
                error: error
            });
            return;
        }
    });

    db.on('trace', function (sql) {
        console.log('db-trace  : [' + sql + ']');
    });

    return db;
}

router.param('shareKey', function (req, res, next, shareKey) {
    req.shareKey = shareKey;
    next();
});

router.get('/:shareKey', function (req, res, next) {
    var db = _getDb(res);
    db.get('SELECT _id, share_type AS shareType, data as shareData FROM table_share WHERE key=?', req.shareKey, function (err, row) {
        if (err) {
            return next(err);
        }


        if (!row) {
            res.render('share', { error: "No matching workout found, sorry!", share: JSON.stringify({shareType:"error", shareData:{}})});
            return;
        }

        var shareData = {};

        try {
            shareData = JSON.parse(row.shareData);
        } catch (e) {
            console.warn('Invalid JSON', row.shareData);
        }

        res.render('share', {
            share: JSON.stringify({
                shareType: row.shareType,
                shareData: shareData
            })
        });
        res.json();
    });
});

router.post('/', function (req, res) {

    // TODO - Sanitize req parameters?
    var shareData = JSON.stringify(req.body.shareData);

    var db = _getDb(res);

    // TODO - Check if there is alrady an entry with the same data?
    var shareKey = rid();
    db.serialize(function () {
      db.run('INSERT INTO table_share (share_type, key, data) VALUES(?, ?, ?)', req.body.shareType, shareKey, shareData, function(err){
        if (err) {
            console.error(err);
            return next(err);
        }
      });
    });

    res.json({
        shareKey: shareKey
    });
});

module.exports = router;


@@ -1333,24 +1333,41 @@
 hare', %7B
+%0D%0A               
  error: 
 %22No matc
@@ -1358,17 +1358,17 @@
  error: 
-%22
+'
 No match
@@ -1392,18 +1392,35 @@
 , sorry!
-%22,
+',%0D%0A               
  share: 
@@ -1435,16 +1435,38 @@
 ingify(%7B
+%0D%0A                    
 shareTyp
@@ -1471,16 +1471,38 @@
 ype:
-%22
+ '
 error
-%22,
+',%0D%0A                   
  sha
@@ -1512,12 +1512,45 @@
 ata:
+ 
 %7B%7D
-%7D)
+%0D%0A                %7D)%0D%0A            
 %7D);%0D
@@ -2026,16 +2026,22 @@
 req, res
+, next
 ) %7B%0D%0A%0D%0A 
@@ -2173,23 +2173,16 @@
 %0D%0A    //
- TODO -
  Check i
@@ -2195,16 +2195,17 @@
 e is alr
+e
 ady an e
@@ -2231,70 +2231,413 @@
 data
-?
 %0D%0A    
-var shareKey = rid();%0D%0A    db.serialize(function () %7B%0D%0A
+db.get('SELECT key AS shareKey FROM table_share WHERE share_type=? AND data=?', req.body.shareType, shareData, function (err, row) %7B%0D%0A        if (err) %7B%0D%0A            console.error(err);%0D%0A            return next(err);%0D%0A        %7D%0D%0A%0D%0A        if (row) %7B%0D%0A            res.json(%7B%0D%0A                shareKey: row.shareKey%0D%0A            %7D);%0D%0A            return;%0D%0A        %7D%0D%0A%0D%0A        var shareKey = rid();%0D%0A  
     
@@ -2765,16 +2765,22 @@
 tion
+ 
 (err)
+ 
 %7B%0D%0A
+    
     
@@ -2799,32 +2799,36 @@
  %7B%0D%0A            
+  
+  
 console.error(er
@@ -2824,32 +2824,36 @@
 le.error(err);%0D%0A
+    
             retu
@@ -2877,24 +2877,21 @@
     
-  %7D%0D%0A
       %7D
-);
 %0D%0A
+    
     
@@ -2897,24 +2897,28 @@
  %7D);%0D%0A%0D%0A    
+   
+ 
 res.json(%7B%0D%0A
@@ -2909,32 +2909,36 @@
     res.json(%7B%0D%0A
+    
         shareKey
@@ -2949,29 +2949,44 @@
 areKey%0D%0A    
+  
+  
 %7D);%0D%0A
+    %7D);%0D%0A%0D%0A
 %7D);%0D%0A%0D%0Amodul

Check if there is already a workout with the same data before creating a new share entry