From 37a877c0962f5fcbeae898c53c853f685f16d4a5 Mon Sep 17 00:00:00 2001 From: Vandhuy Date: Mon, 29 Sep 2014 15:04:26 -0300 Subject: [PATCH] Adiantamento na API do Wikilibras --- db/api.js | 27 +++++++++++++++++++++++++++ db/config.js | 12 ++++++++++++ db/schemas/sinal.js | 15 +++++++++++++++ endpoints/sinal.js | 32 +++++++++++++++++++++++++++++--- package.json | 1 + public/assets/css/.sass-cache/b8ff95717abe9141bb02c85d4daf286641f59094/main.scssc | Bin 0 -> 11145 bytes public/assets/css/main.css | 38 ++++++++++++++++++++++++++++++++++++++ public/assets/css/main.css.map | 7 +++++++ public/assets/css/main.scss | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ public/assets/js/main.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/videos/.gitempty | 0 server.js | 38 ++++++++++++++++++++++++++++++++++++-- videos/.gitempty | 0 views/index.html | 44 ++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 343 insertions(+), 5 deletions(-) create mode 100644 db/api.js create mode 100644 db/config.js create mode 100644 db/schemas/sinal.js create mode 100644 public/assets/css/.sass-cache/b8ff95717abe9141bb02c85d4daf286641f59094/main.scssc create mode 100644 public/assets/css/main.css create mode 100644 public/assets/css/main.css.map create mode 100644 public/assets/css/main.scss create mode 100644 public/assets/js/main.js create mode 100644 public/videos/.gitempty delete mode 100644 videos/.gitempty create mode 100644 views/index.html diff --git a/db/api.js b/db/api.js new file mode 100644 index 0000000..e0c9e09 --- /dev/null +++ b/db/api.js @@ -0,0 +1,27 @@ +function read_all(Sign, callback) { + Sign.find(function(err, signs) { + if (err) callback(null); + + callback(signs); + }); +}; + +function create(object, callback) { + object.save(function(err, sign) { + if (err) callback(null); + + callback(sign); + }); +}; + +function remove(Sign, hash, callback) { + Sign.remove({ uuid: hash }, function(err, sign) { + if (err) callback(null); + + callback(sign); + }); +}; + +module.exports.read_all = read_all; +module.exports.create = create; +module.exports.remove = remove; \ No newline at end of file diff --git a/db/config.js b/db/config.js new file mode 100644 index 0000000..7227b8d --- /dev/null +++ b/db/config.js @@ -0,0 +1,12 @@ +function connectMongo(mongoose) { + var db = mongoose.connection; + + db.on('error', console.error); + db.once('open', function() { + console.log('Conectado ao MongoDB.') + }); + + mongoose.connect('mongodb://localhost/wikilibras-api'); +}; + +module.exports.connect = connectMongo; \ No newline at end of file diff --git a/db/schemas/sinal.js b/db/schemas/sinal.js new file mode 100644 index 0000000..5d87095 --- /dev/null +++ b/db/schemas/sinal.js @@ -0,0 +1,15 @@ +function init_schema(mongoose) { + var signSchema = new mongoose.Schema({ + name: String, + uuid: String, + status: String, + created_at: { type: Date }, + updated_at: { type: Date } + }); + + var Sign = mongoose.model('Sign', signSchema); + + return Sign; +}; + +module.exports.init = init_schema; \ No newline at end of file diff --git a/endpoints/sinal.js b/endpoints/sinal.js index 05728fc..5a3a274 100644 --- a/endpoints/sinal.js +++ b/endpoints/sinal.js @@ -1,5 +1,31 @@ -function initEndpoint(req, res) { - res.send(200, { 'status': 'Estrutura Montada :)'}); +var uuid = require('node-uuid'); +var API = require('../db/api'); + +function init_endpoint(req, res, Sign) { + /* Verifica se o paramêtro [name] existe ou possui algum valor */ + if ((req.body.name === '') || (req.body.name === undefined)) { + res.send(500, { 'error': 'Especifique o nome do sinal' }); + return; + } + + var array = ['Sucesso', 'Falhou', 'Processando'] + + var sign_object = new Sign({ + name: req.body.name, + uuid: uuid.v4(), + created_at: new Date(), + updated_at: new Date(), + status: array[Math.floor(Math.random() * array.length)] + }); + + API.create(sign_object, function(result) { + if (result !== null) { + res.send(200, { 'status': 'Sinal ' + result.name + ' criado com sucesso'}); + } else { + res.send(500, { 'error': 'Erro na criação.'}); + } + }); }; -module.exports.init = initEndpoint; \ No newline at end of file +module.exports.init = init_endpoint; + diff --git a/package.json b/package.json index cd44047..de41871 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "express": "~3.5.1", "mkdirp": "^0.5.0", + "mongoose": "^3.8.16", "node-uuid": "^1.4.1" } } diff --git a/public/assets/css/.sass-cache/b8ff95717abe9141bb02c85d4daf286641f59094/main.scssc b/public/assets/css/.sass-cache/b8ff95717abe9141bb02c85d4daf286641f59094/main.scssc new file mode 100644 index 0000000..8839851 Binary files /dev/null and b/public/assets/css/.sass-cache/b8ff95717abe9141bb02c85d4daf286641f59094/main.scssc differ diff --git a/public/assets/css/main.css b/public/assets/css/main.css new file mode 100644 index 0000000..a7db885 --- /dev/null +++ b/public/assets/css/main.css @@ -0,0 +1,38 @@ +body { + margin: 0; + padding: 0; } + +header { + width: 100%; + height: 60px; + box-shadow: 1px 1px 2px #c2c2c2; } + header > p { + -webkit-font-smoothing: antialiased; + text-align: center; + position: relative; + top: 13px; + font-family: 'Open Sans Light',X-LocaleSpecific-Light,'Open Sans',X-LocaleSpecific,sans-serif; + font-weight: normal; + line-height: 100%; + text-shadow: 0 1px 0 rgba(255, 255, 255, 0.75); + color: #484848; + font-size: 30px; } + +.main-container { + width: 100%; + display: table; } + .main-container .table-wl { + width: 70%; + margin: 0 auto; + margin-top: 40px; } + .main-container .table-wl tbody tr ._success { + color: #85D42A; + font-weight: bold; } + .main-container .table-wl tbody tr ._error { + color: red; + font-weight: bold; } + .main-container .table-wl tbody tr ._processing { + color: #c2c2c2; + font-weight: bold; } + +/*# sourceMappingURL=main.css.map */ diff --git a/public/assets/css/main.css.map b/public/assets/css/main.css.map new file mode 100644 index 0000000..803ba26 --- /dev/null +++ b/public/assets/css/main.css.map @@ -0,0 +1,7 @@ +{ +"version": 3, +"mappings": "AAAA,IAAK;EACJ,MAAM,EAAE,CAAC;EACT,OAAO,EAAE,CAAC;;AAGX,MAAO;EACN,KAAK,EAAE,IAAI;EACX,MAAM,EAAE,IAAI;EACZ,UAAU,EAAE,mBAAmB;EAE/B,UAAI;IACH,sBAAsB,EAAE,WAAW;IACnC,UAAU,EAAE,MAAM;IAClB,QAAQ,EAAE,QAAQ;IAClB,GAAG,EAAE,IAAI;IACT,WAAW,EAAE,gFAAgF;IAC7F,WAAW,EAAE,MAAM;IACnB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,iCAA8B;IAC3C,KAAK,EAAE,OAAO;IACd,SAAS,EAAE,IAAI;;AAIjB,eAAgB;EACf,KAAK,EAAE,IAAI;EACX,OAAO,EAAE,KAAK;EAEd,yBAAU;IACT,KAAK,EAAE,GAAG;IACV,MAAM,EAAE,MAAM;IACd,UAAU,EAAE,IAAI;IAId,4CAAU;MACT,KAAK,EAAE,OAAO;MACd,WAAW,EAAE,IAAI;IAGlB,0CAAQ;MACP,KAAK,EAAE,GAAG;MACV,WAAW,EAAE,IAAI;IAGlB,+CAAa;MACZ,KAAK,EAAE,OAAO;MACd,WAAW,EAAE,IAAI", +"sources": ["main.scss"], +"names": [], +"file": "main.css" +} \ No newline at end of file diff --git a/public/assets/css/main.scss b/public/assets/css/main.scss new file mode 100644 index 0000000..5934ccd --- /dev/null +++ b/public/assets/css/main.scss @@ -0,0 +1,53 @@ +body { + margin: 0; + padding: 0; +} + +header { + width: 100%; + height: 60px; + box-shadow: 1px 1px 2px #c2c2c2; + + > p { + -webkit-font-smoothing: antialiased; + text-align: center; + position: relative; + top: 13px; + font-family: 'Open Sans Light',X-LocaleSpecific-Light,'Open Sans',X-LocaleSpecific,sans-serif; + font-weight: normal; + line-height: 100%; + text-shadow: 0 1px 0 rgba(255,255,255,0.75); + color: #484848; + font-size: 30px; + } +} + +.main-container { + width: 100%; + display: table; + + .table-wl { + width: 70%; + margin: 0 auto; + margin-top: 40px; + + tbody { + tr { + ._success { + color: #85D42A; + font-weight: bold; + } + + ._error { + color: red; + font-weight: bold; + } + + ._processing { + color: #c2c2c2; + font-weight: bold; + } + } + } + } +} \ No newline at end of file diff --git a/public/assets/js/main.js b/public/assets/js/main.js new file mode 100644 index 0000000..a22a454 --- /dev/null +++ b/public/assets/js/main.js @@ -0,0 +1,81 @@ +var Main = function() { + + function _init() { + + } + + function _getSigns() { + $.ajax({ + type:'GET', + url: '/api/signs', + dataType: 'json' + }).done(function(data) { + _setTable(data); + }).fail(function(err) { + var table = $('.table-wl > tbody'); + + var row = + '\ + #\ + Ajax Error\ + Ajax Error\ + Ajax Error\ + Ajax Error\ + Ajax Error\ + '; + + table.append(row); + }); + } + + function _setTable(data) { + var table = $('.table-wl > tbody'); + + for (var i = 0; i < data.length; i++) { + var row = + '\ + '+ (i+1) +'\ + '+ data[i].name +'\ + '+ data[i].uuid +'\ + '+ data[i].created_at +'\ + '+ data[i].updated_at +'\ + '+ data[i].status +'\ + '; + + table.append(row); + } + + _changeColorStatus(); + } + + function _changeColorStatus() { + var length = $('.table-wl > tbody > tr').length; + var array = $('.table-wl > tbody > tr > td:last-of-type'); + + for (var i = 0; i < length; i++) { + switch($(array[i]).text()) { + case 'Sucesso': + $(array[i]).addClass('_success'); + break; + + case 'Falhou': + $(array[i]).addClass('_error'); + break; + + case 'Processando': + $(array[i]).addClass('_processing'); + break; + } + } + } + + return { + run: function() { + _init(); + _getSigns(); + } + } +}; + +var main = new Main(); +main.run(); \ No newline at end of file diff --git a/public/videos/.gitempty b/public/videos/.gitempty new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/public/videos/.gitempty diff --git a/server.js b/server.js index 999dd77..a41c8c0 100644 --- a/server.js +++ b/server.js @@ -9,13 +9,23 @@ var path = require('path'); var express = require('express'); var app = express(); -app.use(express.static(path.join(__dirname, '/videos'))); +/* Database */ +var mongoose = require('mongoose'); +require('./db/config').connect(mongoose); +var Sign = require('./db/schemas/sinal').init(mongoose); +var API = require('./db/api'); + +app.use(express.static(path.join(__dirname, '/public'))); app.use(express.bodyParser({ keepExtensions: true })); app.get('/', function(req, res) { res.send(200, { 'status': 'server is running!' }); }); +app.get('/signs', function(req, res) { + res.sendfile('views/index.html'); +}); + app.post('/api', function(req, res) { /* Verifica se o paramêtro [servico] existe ou possui algum valor */ if ((req.body.servico === '') || (req.body.servico === undefined)) { @@ -26,11 +36,35 @@ app.post('/api', function(req, res) { /* Analisa o tipo de serviço requisitado */ switch(req.body.servico) { case 'sinal': - endpoint_sinal.init(req, res); + endpoint_sinal.init(req, res, Sign); + break; + + default: + res.send(500, { 'error': 'Tipo do serviço inválido.'}); break; } }); +app.get('/api/signs', function(req, res) { + API.read_all(Sign, function(result) { + if (result !== null) { + res.send(200, result); + } else { + res.send(500, { 'error': 'Erro na busca.'}); + } + }); +}); + +app.delete('/api/:hash', function(req, res) { + API.remove(Sign, req.params.hash, function(result) { + if (result !== null) { + res.send(200, { 'status': 'Remoção concluída.'}); + } else { + res.send(500, { 'error': 'Erro na remoção.'}); + } + }); +}); + app.listen(properties.PORT, properties.HOST, function() { console.log('Server running on ' + properties.HOST + ':' + properties.PORT); }); \ No newline at end of file diff --git a/videos/.gitempty b/videos/.gitempty deleted file mode 100644 index e69de29..0000000 --- a/videos/.gitempty +++ /dev/null diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..dcfbba7 --- /dev/null +++ b/views/index.html @@ -0,0 +1,44 @@ + + + + + + + + Wikilibras API UI + + + + + + + + +
+

Wikilibras API Interface

+
+ +
+ + + + + + + + + + + + + +
#NomeHashCriado emAtualizado emStatus
+
+ + + + + + \ No newline at end of file -- libgit2 0.21.2