From d25690061d2424bacaa9246bdab431e17d7339df Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Sun, 2 Jul 2017 22:27:26 +0200 Subject: [PATCH 01/14] cluster implemented --- app.js | 122 +++++++++++++++++++++++---------- bin/www | 90 ------------------------ {routes => endpoints}/index.js | 0 {routes => endpoints}/users.js | 0 package.json | 2 +- public/.gitkeep | 0 6 files changed, 87 insertions(+), 127 deletions(-) delete mode 100755 bin/www rename {routes => endpoints}/index.js (100%) rename {routes => endpoints}/users.js (100%) delete mode 100644 public/.gitkeep diff --git a/app.js b/app.js index 06548a0..48522aa 100644 --- a/app.js +++ b/app.js @@ -4,43 +4,93 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); +var cluster = require('cluster'); +var debug = require('debug')('gapi:server'); +var http = require('http'); -var index = require('./routes/index'); -var users = require('./routes/users'); + +//----- API pages ------- +var index = require('./endpoints/index'); +var users = require('./endpoints/users'); var app = express(); +var port = 3000; + +if (cluster.isMaster) { + // Count the machine's CPUs + var cpuCount = require('os').cpus().length; + + // Create a worker for each CPU + for (var i = 0; i < cpuCount; i += 1) { + cluster.fork(); + } + //Listen for dying workers + cluster.on('exit', function (worker) { + // Replace the dead worker + cluster.fork(); + + }); + +} else { + + // view engine setup + app.set('views', path.join(__dirname, 'views')); + app.set('view engine', 'jade'); + + // uncomment after placing your favicon in /public + //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); + app.use(logger('dev')); + app.use(bodyParser.json()); + app.use(bodyParser.urlencoded({ extended: false })); + app.use(cookieParser()); + app.use(express.static(path.join(__dirname, 'public'))); + + app.use('/', index); + app.use('/users', users); + + // catch 404 and forward to error handler + app.use(function(req, res, next){ + res.status(404); + // respond with html page + if (req.accepts('html')) { + res.render('404', { url: req.url }); + return; + } + + // respond with json + if (req.accepts('json')) { + res.send({ error: 'Not found' }); + return; + } + + // default to plain-text. send() + res.type('txt').send('Not found'); + }); + + app.use(function(req, res, next) { + var err = new Error('Not Found'); + err.status = 404; + next(err); + }); + // error handler + app.use(function(err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + res.render('error'); + }); + console.log('Application running!'); + /** + * Create HTTP server. + */ + + var server = http.createServer(app); + /** + * Listen on provided port, on all network interfaces. + */ -// view engine setup -app.set('views', path.join(__dirname, 'views')); -app.set('view engine', 'jade'); - -// uncomment after placing your favicon in /public -//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); -app.use(logger('dev')); -app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ extended: false })); -app.use(cookieParser()); -app.use(express.static(path.join(__dirname, 'public'))); - -app.use('/', index); -app.use('/users', users); - -// catch 404 and forward to error handler -app.use(function(req, res, next) { - var err = new Error('Not Found'); - err.status = 404; - next(err); -}); - -// error handler -app.use(function(err, req, res, next) { - // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = req.app.get('env') === 'development' ? err : {}; - - // render the error page - res.status(err.status || 500); - res.render('error'); -}); - -module.exports = app; + server.listen(port); +} diff --git a/bin/www b/bin/www deleted file mode 100755 index 12ffe33..0000000 --- a/bin/www +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env node - -/** - * Module dependencies. - */ - -var app = require('../app'); -var debug = require('debug')('dapi:server'); -var http = require('http'); - -/** - * Get port from environment and store in Express. - */ - -var port = normalizePort(process.env.PORT || '3000'); -app.set('port', port); - -/** - * Create HTTP server. - */ - -var server = http.createServer(app); - -/** - * Listen on provided port, on all network interfaces. - */ - -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); - -/** - * Normalize a port into a number, string, or false. - */ - -function normalizePort(val) { - var port = parseInt(val, 10); - - if (isNaN(port)) { - // named pipe - return val; - } - - if (port >= 0) { - // port number - return port; - } - - return false; -} - -/** - * Event listener for HTTP server "error" event. - */ - -function onError(error) { - if (error.syscall !== 'listen') { - throw error; - } - - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; - - // handle specific listen errors with friendly messages - switch (error.code) { - case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); - break; - case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); - break; - default: - throw error; - } -} - -/** - * Event listener for HTTP server "listening" event. - */ - -function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - debug('Listening on ' + bind); -} diff --git a/routes/index.js b/endpoints/index.js similarity index 100% rename from routes/index.js rename to endpoints/index.js diff --git a/routes/users.js b/endpoints/users.js similarity index 100% rename from routes/users.js rename to endpoints/users.js diff --git a/package.json b/package.json index 44e0187..9986564 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "private": true, "scripts": { - "start": "node ./bin/www", + "start": "nodejs app.js", "test": "mocha" }, "dependencies": { diff --git a/public/.gitkeep b/public/.gitkeep deleted file mode 100644 index e69de29..0000000 From a2736565455fcd9580b6d7c906f2281c6560fd22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Hammarstr=C3=B6m?= Date: Mon, 3 Jul 2017 04:45:50 +0200 Subject: [PATCH 02/14] app.js formatting --- app.js | 146 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 75 deletions(-) diff --git a/app.js b/app.js index 48522aa..4299689 100644 --- a/app.js +++ b/app.js @@ -17,80 +17,76 @@ var app = express(); var port = 3000; if (cluster.isMaster) { - // Count the machine's CPUs - var cpuCount = require('os').cpus().length; - - // Create a worker for each CPU - for (var i = 0; i < cpuCount; i += 1) { - cluster.fork(); - } - //Listen for dying workers - cluster.on('exit', function (worker) { - // Replace the dead worker - cluster.fork(); - - }); - + // Count the machine's CPUs + var cpuCount = require('os').cpus().length; + + // Create a worker for each CPU + for (var i = 0; i < cpuCount; i += 1) { + cluster.fork(); + } + + //Listen for dying workers + cluster.on('exit', function (worker) { + // Replace the dead worker + cluster.fork(); + }); } else { - - // view engine setup - app.set('views', path.join(__dirname, 'views')); - app.set('view engine', 'jade'); - - // uncomment after placing your favicon in /public - //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); - app.use(logger('dev')); - app.use(bodyParser.json()); - app.use(bodyParser.urlencoded({ extended: false })); - app.use(cookieParser()); - app.use(express.static(path.join(__dirname, 'public'))); - - app.use('/', index); - app.use('/users', users); - - // catch 404 and forward to error handler - app.use(function(req, res, next){ - res.status(404); - // respond with html page - if (req.accepts('html')) { - res.render('404', { url: req.url }); - return; - } - - // respond with json - if (req.accepts('json')) { - res.send({ error: 'Not found' }); - return; - } - - // default to plain-text. send() - res.type('txt').send('Not found'); - }); - - app.use(function(req, res, next) { - var err = new Error('Not Found'); - err.status = 404; - next(err); - }); - // error handler - app.use(function(err, req, res, next) { - // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = req.app.get('env') === 'development' ? err : {}; - - // render the error page - res.status(err.status || 500); - res.render('error'); - }); - console.log('Application running!'); - /** - * Create HTTP server. - */ - - var server = http.createServer(app); - /** - * Listen on provided port, on all network interfaces. - */ - - server.listen(port); + // view engine setup + app.set('views', path.join(__dirname, 'views')); + app.set('view engine', 'jade'); + + // uncomment after placing your favicon in /public + //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); + app.use(logger('dev')); + app.use(bodyParser.json()); + app.use(bodyParser.urlencoded({ extended: false })); + app.use(cookieParser()); + app.use(express.static(path.join(__dirname, 'public'))); + + app.use('/', index); + app.use('/users', users); + + // catch 404 and forward to error handler + app.use(function(req, res, next){ + res.status(404); + // respond with html page + if (req.accepts('html')) { + res.render('404', { url: req.url }); + return; + } + + // respond with json + if (req.accepts('json')) { + res.send({ error: 'Not found' }); + return; + } + + // default to plain-text. send() + res.type('txt').send('Not found'); + }); + + app.use(function(req, res, next) { + var err = new Error('Not Found'); + err.status = 404; + next(err); + }); + + // error handler + app.use(function(err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + res.render('error'); + }); + + console.log('Application running!'); + + // create HTTP-server + var server = http.createServer(app); + + // listen to port + server.listen(port); } From 01125d8b3b9f0838f03da23056648386b9233042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5ns=20Ansgariusson?= Date: Mon, 3 Jul 2017 08:54:57 +0200 Subject: [PATCH 03/14] Update app.js --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 4299689..f06d308 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var cluster = require('cluster'); -var debug = require('debug')('gapi:server'); +var debug = require('debug')('dapi:server'); var http = require('http'); From 878c53afef236f89810dc7f0e51fed80e687e0c8 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Mon, 3 Jul 2017 14:46:10 +0200 Subject: [PATCH 04/14] Created a Database handler. This commit closes in parts the issue #31 Implemented a Database handler and wrote a test, wich should fail untill database is created with a test subject with id = 1. The Database handler uses preparedstatments. Database.js exports a number of suggested skeleton functions to be implemented if @Admin agrees with its usefullness. The query function in the Database.js is a bit complicated and could use a bit of improvment, but I have faild in finding a more suitable option. --- Database.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Database.js diff --git a/Database.js b/Database.js new file mode 100644 index 0000000..09f4b03 --- /dev/null +++ b/Database.js @@ -0,0 +1,53 @@ +var mysql = require('mysql'); +// the Database connection information goes here +var dbConfig = { + host: "", + user: "", + password: "", + database: "" +}; + +var con; + +function query(sql,values,callback){ + con = mysql.createConnection(dbConfig); + con.query(sql, [values], function (err, result,fields) { + if(err) + throw err; + else + callback(result); + }); + con.end(); +} + +module.exports = { + + exampleUsage: function(body,callback){ + var sql = ""; + var params = []; + query(sql,param,callback); + }, + + createUser : function(body,callback){ + + }, + + getUser : function (params,callback) { + var sql = "select * from user where id = ?"; + var values= [params.id]; + console.log("Pre query") + query(sql,values,callback); + }, + + GiveRoleToUser : function (body,callback) { + + }, + + CreatePost : function (body,callback) { + + }, + + AddTagToPost : function (body,callback) { + + } +} From a9c3db414a1af5baa201bf31755a8ae101339bc3 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Mon, 3 Jul 2017 14:54:55 +0200 Subject: [PATCH 05/14] adds missed files from last commit --- endpoints/users.js | 10 +++++++--- test/mock.js | 23 ++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/endpoints/users.js b/endpoints/users.js index 623e430..1f734f8 100644 --- a/endpoints/users.js +++ b/endpoints/users.js @@ -1,9 +1,13 @@ var express = require('express'); var router = express.Router(); +var db = require('../Database'); -/* GET users listing. */ -router.get('/', function(req, res, next) { - res.send('respond with a resource'); +/* GET a user from the database. */ +router.get('/:id', function(req, res, next) { + db.getUser(req.query,function (callback) { + res.writeHead(200,{'Content-Type':'application/json'}); + res.end(JSON.stringify(callback)); + }) }); module.exports = router; diff --git a/test/mock.js b/test/mock.js index b745230..bae5298 100644 --- a/test/mock.js +++ b/test/mock.js @@ -1,8 +1,21 @@ const assert = require('assert'); +const db = require('../Database'); + describe('Array', function() { - describe('#indexOf()', function() { - it('should return -1 when the value is not present', function() { - assert.equal(-1, [1,2,3].indexOf(4)); - }); + describe('#indexOf()', function() { + it('should return -1 when the value is not present', function() { + assert.equal(-1, [1,2,3].indexOf(4)); }); -}); \ No newline at end of file + }); +}); + +describe('#Database.js', function () { + it('Should return a row containing all the information of a user', function () { + var query = {"id":"1"}; + var answer = 0; + db.getUser(query,function (callback) { + answer = callback; + }); + assert.equal(1,answer); + }); +}); From a26046bbb6be07ae1ef5759c62d62d8797c28f41 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 19:40:19 +0200 Subject: [PATCH 06/14] Sequalize implementation begin --- Database.js | 67 ++++++---------------- config/config.json | 23 ++++++++ database/Database.js | 71 ++++++++++++++++++++++++ migrations/20170706172758-create_user.js | 27 +++++++++ models/index.js | 36 ++++++++++++ package.json | 3 + 6 files changed, 176 insertions(+), 51 deletions(-) create mode 100644 config/config.json create mode 100644 database/Database.js create mode 100644 migrations/20170706172758-create_user.js create mode 100644 models/index.js diff --git a/Database.js b/Database.js index 09f4b03..a59e63a 100644 --- a/Database.js +++ b/Database.js @@ -1,53 +1,18 @@ -var mysql = require('mysql'); -// the Database connection information goes here -var dbConfig = { - host: "", - user: "", - password: "", - database: "" -}; - -var con; - -function query(sql,values,callback){ - con = mysql.createConnection(dbConfig); - con.query(sql, [values], function (err, result,fields) { - if(err) - throw err; - else - callback(result); - }); - con.end(); -} - -module.exports = { - - exampleUsage: function(body,callback){ - var sql = ""; - var params = []; - query(sql,param,callback); - }, - - createUser : function(body,callback){ - - }, - - getUser : function (params,callback) { - var sql = "select * from user where id = ?"; - var values= [params.id]; - console.log("Pre query") - query(sql,values,callback); - }, - - GiveRoleToUser : function (body,callback) { - - }, - - CreatePost : function (body,callback) { - - }, +const Sequelize = require ("sequelize"); +const sequelize = new Sequelize('dapi','dapi','password',{ + host: 'localhost', + dialect: 'mysql', + pool : { + max: 5, + min:0, + idle:10000 + } +}); - AddTagToPost : function (body,callback) { +sequelize.authenticate().then(()=> { + console.log("con has been established"); +}).catch(err => { + console.error("unable to connect", err); +}); - } -} +module.exports = sequelize; diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..1ff5786 --- /dev/null +++ b/config/config.json @@ -0,0 +1,23 @@ +{ + "development": { + "username": "dapi", + "password": "password", + "database": "dapi_development", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "test": { + "username": "dapi", + "password": "password", + "database": "dapi_test", + "host": "127.0.0.1", + "dialect": "mysql" + }, + "production": { + "username": "dapi", + "password": "password", + "database": "dapi_production", + "host": "127.0.0.1", + "dialect": "mysql" + } +} diff --git a/database/Database.js b/database/Database.js new file mode 100644 index 0000000..1451973 --- /dev/null +++ b/database/Database.js @@ -0,0 +1,71 @@ +var mysql = require('mysql'); +var dbConfig = require('./configuration'); + +var con; + +function query(sql,values,callback){ + con = mysql.createConnection(dbConfig); + con.query(sql, [values], function (err, result,fields) { + if(err) + throw err; + else + callback(result); + }); + con.end(); +} + +module.exports = { + + exampleUsage: function(body,callback){ + var sql = ""; + var params = []; + query(sql,param,callback); + }, + + createUser : function(body,callback){ + + }, + + getUser : function (params,callback) { + var sql = "select * from user where id = ?"; + var values= [params.id]; + console.log("Pre query") + query(sql,values,callback); + }, + + GiveRoleToUser : function (body,callback) { + + }, + + CreatePost : function (body,callback) { + + }, + + AddTagToPost : function (body,callback) { + + }, + + addCurrancyOnUser : function (body,callback){ + var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; + var params = [body.amount,body.email]; + query(sql,params, callback); + }, + + subCurrancyOnUser : function (body,callback) { + var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; + var params = [body.amount,body.email]; + query(sql,params,callback); + }, + + addCardOnUser : function (body,callback) { + var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; + var params = [body.amount,body.email]; + query(sql,params,callback); + }, + + getFunds : function (body, callback) { + var sql = 'SELECT Currancy from user WHERE email = ?'; + var param = body.email; + query(sql,param,callback); + } +} diff --git a/migrations/20170706172758-create_user.js b/migrations/20170706172758-create_user.js new file mode 100644 index 0000000..dea42bf --- /dev/null +++ b/migrations/20170706172758-create_user.js @@ -0,0 +1,27 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'users', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + firstName:{ + type : Sequelize.STRING + } + }) + }, + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('users'); + } +}; diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..7540dba --- /dev/null +++ b/models/index.js @@ -0,0 +1,36 @@ +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var Sequelize = require('sequelize'); +var basename = path.basename(module.filename); +var env = process.env.NODE_ENV || 'development'; +var config = require(__dirname + '/../config/config.json')[env]; +var db = {}; + +if (config.use_env_variable) { + var sequelize = new Sequelize(process.env[config.use_env_variable]); +} else { + var sequelize = new Sequelize(config.database, config.username, config.password, config); +} + +fs + .readdirSync(__dirname) + .filter(function(file) { + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); + }) + .forEach(function(file) { + var model = sequelize['import'](path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(function(modelName) { + if (db[modelName].associate) { + db[modelName].associate(db); + } +}); + +db.sequelize = sequelize; +db.Sequelize = Sequelize; + +module.exports = db; diff --git a/package.json b/package.json index 9986564..378f350 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,12 @@ "express": "~4.15.2", "jade": "~1.11.0", "morgan": "~1.8.1", + "mysql2": "^1.3.5", + "sequelize": "^3.2.1", "serve-favicon": "~2.4.2" }, "devDependencies": { + "gulp-sequelize": "0.0.2", "mocha": "^3.4.2" } } From f4ea467825d8ccd72045385066fc7c6aebd9e5e7 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 19:49:55 +0200 Subject: [PATCH 07/14] updated user to specifications --- migrations/20170706172758-create_user.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/migrations/20170706172758-create_user.js b/migrations/20170706172758-create_user.js index dea42bf..24e3cac 100644 --- a/migrations/20170706172758-create_user.js +++ b/migrations/20170706172758-create_user.js @@ -17,8 +17,27 @@ module.exports = { type: Sequelize.DATE }, firstName:{ - type : Sequelize.STRING - } + type : Sequelize.STRING, + allowNull: false, + }, + lastName:{ + type: Sequelize.STRING, + allowNull: false + }, + password:{ + type: Sequelize.STRING, + allowNull: false + }, + email:{ + type: Sequelize.STRING, + allowNull: false + }, + luCard:{ + type: Sequelize.STRING + }, + borderAmount:{ + type: Sequelize.INTEGER + } }) }, down: function (queryInterface, Sequelize) { From 2d94de6d08281b0aea64cddf4dfe8c5149f8b2bf Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 20:24:49 +0200 Subject: [PATCH 08/14] Created Post table --- migrations/20170706175157-create_post.js | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 migrations/20170706175157-create_post.js diff --git a/migrations/20170706175157-create_post.js b/migrations/20170706175157-create_post.js new file mode 100644 index 0000000..0023cf7 --- /dev/null +++ b/migrations/20170706175157-create_post.js @@ -0,0 +1,41 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'posts', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + creator: { + type: Sequelize.INTEGER, + references: { + model: 'users', + key: 'id' + }, + onUpdate: 'cascade', + onDelete: 'cascade' + }, + header: { + type: Sequelize.STRING, + allowNull: false + }, + text: { + type: Sequelize.STRING, + allowNull: false + } + }) + }, + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('posts'); + } +}; From 730f9309104b3b2b2f6c45a83621da69bfcca6bf Mon Sep 17 00:00:00 2001 From: Oscar Rydh Date: Thu, 6 Jul 2017 20:28:59 +0200 Subject: [PATCH 09/14] Added a create roles migration --- migrations/20170706182450-create_roles.js | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 migrations/20170706182450-create_roles.js diff --git a/migrations/20170706182450-create_roles.js b/migrations/20170706182450-create_roles.js new file mode 100644 index 0000000..0b99323 --- /dev/null +++ b/migrations/20170706182450-create_roles.js @@ -0,0 +1,35 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'roles', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + name:{ + type : Sequelize.STRING, + allowNull: false, + }, + level: { + type: Sequelize.INTEGER, + allowNull: false + }, + description:{ + type: Sequelize.STRING + }, + }); + }, + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('roles'); + } +}; From 0c10bf2009a2e410cc42a05e13c197bd94e9561f Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 20:30:57 +0200 Subject: [PATCH 10/14] Creatod database table tags --- migrations/20170706182548-create_tags.js | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 migrations/20170706182548-create_tags.js diff --git a/migrations/20170706182548-create_tags.js b/migrations/20170706182548-create_tags.js new file mode 100644 index 0000000..8737bb8 --- /dev/null +++ b/migrations/20170706182548-create_tags.js @@ -0,0 +1,32 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'tags', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + name: { + type: Sequelize.STRING, + allowNull: false + }, + prio: { + type: Sequelize.INTEGER, + allowNull:false + } + }) + }, + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('tags'); + } +}; From 7715bb13b502ce2804d7df644c8a7c13c08335d1 Mon Sep 17 00:00:00 2001 From: Oscar Rydh Date: Thu, 6 Jul 2017 20:44:22 +0200 Subject: [PATCH 11/14] Added user roles migration --- .../20170706182919-create_user_roles.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 migrations/20170706182919-create_user_roles.js diff --git a/migrations/20170706182919-create_user_roles.js b/migrations/20170706182919-create_user_roles.js new file mode 100644 index 0000000..bca367f --- /dev/null +++ b/migrations/20170706182919-create_user_roles.js @@ -0,0 +1,37 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'userRoles', + { + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + userId: { + type: Sequelize.INTEGER, + references: { + model: 'users', + key: 'id' + }, + allowNull: false, + primaryKey: true + }, + roleId: { + type: Sequelize.INTEGER, + references: { + model: 'roles', + key: 'id' + }, + allowNull: false, + primaryKey: true + } + }); + }, + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('userRoles'); + } +}; From fa079cccdd7ecd07f9a952a5b4ad10cc06e5cbd6 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 20:47:31 +0200 Subject: [PATCH 12/14] updated tables --- migrations/20170706175157-create_post.js | 1 + migrations/20170706183129-create_postTags.js | 38 ++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 migrations/20170706183129-create_postTags.js diff --git a/migrations/20170706175157-create_post.js b/migrations/20170706175157-create_post.js index 0023cf7..d5c5bfb 100644 --- a/migrations/20170706175157-create_post.js +++ b/migrations/20170706175157-create_post.js @@ -18,6 +18,7 @@ module.exports = { }, creator: { type: Sequelize.INTEGER, + allowNull: false, references: { model: 'users', key: 'id' diff --git a/migrations/20170706183129-create_postTags.js b/migrations/20170706183129-create_postTags.js new file mode 100644 index 0000000..7453f1b --- /dev/null +++ b/migrations/20170706183129-create_postTags.js @@ -0,0 +1,38 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.createTable( + 'postTags', + { + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + }, + postID:{ + type: Sequelize.INTEGER, + allowNull: false, + primaryKey: true, + references: { + model: 'posts', + key: 'id' + } + }, + tagID:{ + type: Sequelize.INTEGER, + allowNull: false, + primaryKey: true, + references: { + model: 'tags', + key: 'id' + } + } + }) + }, + + down: function (queryInterface, Sequelize) { + queryInterface.dropTable('postTags'); + } +}; From e0947662b83fd351a2f3043a135b967a58e01384 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 20:54:33 +0200 Subject: [PATCH 13/14] updated tables --- migrations/20170706175157-create_post.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/migrations/20170706175157-create_post.js b/migrations/20170706175157-create_post.js index d5c5bfb..18188df 100644 --- a/migrations/20170706175157-create_post.js +++ b/migrations/20170706175157-create_post.js @@ -22,9 +22,7 @@ module.exports = { references: { model: 'users', key: 'id' - }, - onUpdate: 'cascade', - onDelete: 'cascade' + } }, header: { type: Sequelize.STRING, From 5f3ccbc7e67ac9e2ea6328de636e73554944fa01 Mon Sep 17 00:00:00 2001 From: Mattemagikern Date: Thu, 6 Jul 2017 20:57:10 +0200 Subject: [PATCH 14/14] cleaning up branch --- database/Database.js | 71 -------------------------------------------- test/mock.js | 6 ---- 2 files changed, 77 deletions(-) delete mode 100644 database/Database.js diff --git a/database/Database.js b/database/Database.js deleted file mode 100644 index 1451973..0000000 --- a/database/Database.js +++ /dev/null @@ -1,71 +0,0 @@ -var mysql = require('mysql'); -var dbConfig = require('./configuration'); - -var con; - -function query(sql,values,callback){ - con = mysql.createConnection(dbConfig); - con.query(sql, [values], function (err, result,fields) { - if(err) - throw err; - else - callback(result); - }); - con.end(); -} - -module.exports = { - - exampleUsage: function(body,callback){ - var sql = ""; - var params = []; - query(sql,param,callback); - }, - - createUser : function(body,callback){ - - }, - - getUser : function (params,callback) { - var sql = "select * from user where id = ?"; - var values= [params.id]; - console.log("Pre query") - query(sql,values,callback); - }, - - GiveRoleToUser : function (body,callback) { - - }, - - CreatePost : function (body,callback) { - - }, - - AddTagToPost : function (body,callback) { - - }, - - addCurrancyOnUser : function (body,callback){ - var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; - var params = [body.amount,body.email]; - query(sql,params, callback); - }, - - subCurrancyOnUser : function (body,callback) { - var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; - var params = [body.amount,body.email]; - query(sql,params,callback); - }, - - addCardOnUser : function (body,callback) { - var sql = 'UPDATE user SET Currancy = Currancy + ? WHERE email = ?'; - var params = [body.amount,body.email]; - query(sql,params,callback); - }, - - getFunds : function (body, callback) { - var sql = 'SELECT Currancy from user WHERE email = ?'; - var param = body.email; - query(sql,param,callback); - } -} diff --git a/test/mock.js b/test/mock.js index bae5298..575bcef 100644 --- a/test/mock.js +++ b/test/mock.js @@ -11,11 +11,5 @@ describe('Array', function() { describe('#Database.js', function () { it('Should return a row containing all the information of a user', function () { - var query = {"id":"1"}; - var answer = 0; - db.getUser(query,function (callback) { - answer = callback; - }); - assert.equal(1,answer); }); });