Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #1

Merged
merged 10 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
40 changes: 40 additions & 0 deletions api/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
global.port = 3000;
global.config = require('./config.json');
global.fs = require('fs');
global.request = require('request');
global.validator = require('validator');
global.randtoken = require('rand-token');
global.moment = require('moment');
global.async = require('async');
global.passwordHash = require('password-hash');
global.bodyParser = require('body-parser'); //for post req
global.cookieParser = require('cookie-parser'); // for cookie req
global.express = require('express');
global.app = express();
global.path = require('path');
global.appRoot = path.resolve(__dirname);
global.server = require('http').createServer(app);
eval(fs.readFileSync(appRoot+'/db.js')+''); //retreive the db config and functions
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

global.include = function(path) {
require(path);
}

// Add headers
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8080');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

include(appRoot+'/routes.js'); //retreive routes

server.listen(port,function()
{
console.log('Peridot API Listening on port '+port);
});
60 changes: 60 additions & 0 deletions api/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"db" :
{
"host" : "localhost",
"user" : "root",
"password" : "",
"database" : "peridot"
},
"tables" :
{
"users":
{
"table" : "users",
"rows" :
[
"id","nickname","avatar_url"
],
"required" :
[
"nickname","avatar_url"
]
},
"me":
{
"table" : "users",
"rows" :
[
"id","nickname","signup_date","email","avatar_url"
]
},
"channels":
{
"table" : "channels",
"rows" :
[
"*"
]
},
"files":
{
"table" : "files",
"rows" :
[
"*"
],
"required" :
[
"url","name","channel_id"
]
},
"user_channels":
{
"table" : "user_channels",
"rows" :
[
"*"
]
}
}
}
104 changes: 104 additions & 0 deletions api/controllers/delete/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var fileId = parseInt(req.params.param);
var check = function(req,callback)
{
var r = { //json return structure
success : false,
statusCode : 403,
message : "UNDEFINED_TOKEN"
};

var token = req.query.token;
var post = req.body;
var userId;

async.series([ //exec all function async

function(callback)
{
if(!token)
{
callback(r);
return;
}
callback(null);
},

function(callback)
{
//token validation
db.query('SELECT user_id from auth_tokens where token = "'+token+'" and expire_date > NOW()', function(err, rows, fields)
{
if (err)
{
r.message = err;
callback(r);
return;
}

if(rows.length === 0) //if result
{
r.message = "INVALID_TOKEN";
callback(r);
return;
}
userId = rows[0].user_id;
callback(null);
});
},
function(callback) //check if the target exist
{
db.checkIfExist("files",fileId,function(exist)
{
if(!exist)
{
r.message = "NOT_EXIST";
callback(r);
return;
}
callback(null);
});
}

],
function(ret)
{
if(ret) //if error : display and stop
{
callback(ret);
return;
}

async.series([
function(callback) //update subtitle
{
db.query('DELETE from files where id = "'+fileId+'"', function(err, rows, fields)
{
if (err)
{
r.statusCode = 500;
r.success = false;
r.message = err;
callback(r);
return;
}
callback(null);

});
}],
function(upt)
{
if(upt)
{
callback(upt);
}
else
{
r.statusCode = 204;
r.success = true;
r.message = "OK";
callback(r);
return;
}
});
});
};
59 changes: 59 additions & 0 deletions api/controllers/get/me.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
global.check = function(req,callback)
{
var r = { //json return structure
success : false,
statusCode : 403,
message : "MISSING_TOKEN_NOT_LOGGED"
};

if(req.query.token === undefined || req.query.token === "")
{
callback(r);
return;
}

var token = req.query.token;
//token validation
db.query('SELECT user_id from auth_tokens where token = "'+token+'" and expire_date > NOW()', function(err, rows, fields)
{
if (err)
{
r.message = err;
callback(r);
return;
}

if(rows.length == 0) //if result
{
r.message = "INVALID_TOKEN";
callback(r);
return;
}

var userId = rows[0].user_id;

var rList = config.tables["me"].rows.toString();
var rTable = config.tables["me"].table;

db.query('SELECT '+rList+' FROM '+rTable+' WHERE id = '+userId+' ORDER BY id DESC LIMIT 1', function(err, rows, fields) {
if (err) {
console.error(err);
r.success = false;
r.message = err.code;
callback(r);
}

if(rows !== undefined) //if result
{
r = {
success: true,
statusCode : 200,
message: '',
me: rows,
length: rows.length
};
callback(r);
}
});
});
};
Loading