diff --git a/app/models/nerd.js b/app/models/nerd.js index 5a09532..5efbcbc 100644 --- a/app/models/nerd.js +++ b/app/models/nerd.js @@ -5,8 +5,20 @@ var mongoose = require('mongoose'); // define our nerd model // module.exports allows us to pass this to other files when it is called module.exports = mongoose.model('Nerd', { - name: { + login: { type: String, - default: '' + default: 'login' + }, + section: { + type: String, + default: 'IMA' + }, + promo: { + type: Number, + default: 2017 + }, + role: { + type: String, + default: 'Membre' } }); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js index fd6036c..7a32c8e 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,35 +1,59 @@ - // app/routes.js - - // grab the nerd model we just created - var Nerd = require('./models/nerd'); - - module.exports = function (app) { - - // server routes =========================================================== - // handle things like api calls - // authentication routes - - // sample api route - app.get('/api/nerds', function (req, res) { - // use mongoose to get all nerds in the database - Nerd.find(function (err, nerds) { - - // if there is an error retrieving, send the error. - // nothing after res.send(err) will execute - if (err) - res.send(err); - - res.json(nerds); // return all nerds in JSON format - }); - }); - - // route to handle creating goes here (app.post) - // route to handle delete goes here (app.delete) - - // frontend routes ========================================================= - // route to handle all angular requests - app.get('*', function (req, res) { - res.sendfile('./public/views/index.html'); // load our public/index.html file - }); - - }; \ No newline at end of file +// app/routes.js + +// grab the nerd model we just created +var Nerd = require('./models/nerd'); + +module.exports = function (app) { + + // server routes =========================================================== + // handle things like api calls + // authentication routes + + // sample api route + app.get('/api/nerds', function (req, res) { + // use mongoose to get all nerds in the database + Nerd.find(function (err, nerds) { + if (err) + res.send(err); + res.json(nerds); + }); + }); + app.post('/api/nerds', function (req, res) { + Nerd.create({ + login: req.body.login, + role: req.body.role, + section: req.body.section, + }, function (err, nerd) { + if (err) + res.send(err); + Nerd.find(function (err, nerds) { + if (err) + res.send(err); + res.json(nerds); + }); + }); + }); + app.delete('/api/nerds/:nerd_id', function (req, res) { + Nerd.remove({ + _id: req.params.nerd_id + }, function (err, nerd) { + if (err) + res.send(err); + Nerd.find(function (err, nerds) { + if (err) + res.send(err); + res.json(nerds); + }); + }) + }) + + // route to handle creating goes here (app.post) + // route to handle delete goes here (app.delete) + + // frontend routes ========================================================= + // route to handle all angular requests + app.get('*', function (req, res) { + res.sendfile('./public/views/index.html'); // load our public/index.html file + }); + +}; \ No newline at end of file diff --git a/package.json b/package.json index 2c07849..f78a234 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "body-parser": "^1.12.0", "express": "^4.12.2", "method-override": "^2.3.1", - "mongoose": "^3.8.25" + "mongoose": "^3.8.25", + "morgan": "^1.5.1" } } diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 0000000..b11e73a --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,9 @@ +html { + overflow-y: scroll; +} +body { + padding-top: 50px; +} +#nerd-list { + width: 90 %; +} \ No newline at end of file diff --git a/public/js/controllers/NerdCtrl.js b/public/js/controllers/NerdCtrl.js index 4d9fe4b..67cb397 100644 --- a/public/js/controllers/NerdCtrl.js +++ b/public/js/controllers/NerdCtrl.js @@ -1,6 +1,40 @@ // public/js/controllers/NerdCtrl.js -angular.module('NerdCtrl', []).controller('NerdController', function ($scope) { +angular.module('NerdCtrl', []).controller('NerdController', function ($scope, $http) { + $scope.formData = {}; - $scope.tagline = 'Nothing beats a pocket protector!'; + // when landing on the page, get all Nerds and show them + $http.get('/api/nerds') + .success(function (data) { + $scope.nerds = data; + console.log(data); + }) + .error(function (data) { + console.log('Error: ' + data); + }); + + // when submitting the add form, send the text to the node API + $scope.createNerd = function () { + console.log('Adding', $scope.formData); + $http.post('/api/nerds', $scope.formData) + .success(function (data) { + $scope.formData = {}; // clear the form so our user is ready to enter another + $scope.nerds = data; + }) + .error(function (data) { + console.log('Error: ' + data); + }); + }; + + // delete a Nerd after checking it + $scope.deleteNerd = function (id) { + $http.delete('/api/nerds/' + id) + .success(function (data) { + $scope.nerds = data; + console.log(data); + }) + .error(function (data) { + console.log('Error: ' + data); + }); + }; }); \ No newline at end of file diff --git a/public/js/services/NerdService.js b/public/js/services/NerdService.js index 9739b86..513f0e6 100644 --- a/public/js/services/NerdService.js +++ b/public/js/services/NerdService.js @@ -7,9 +7,6 @@ angular.module('NerdService', []).factory('Nerd', ['$http', get: function () { return $http.get('/api/nerds'); }, - - - // these will work when more API routes are defined on the Node side of things // call to POST and create a new nerd create: function (nerdData) { return $http.post('/api/nerds', nerdData); diff --git a/public/views/index.html b/public/views/index.html index 3f39b5e..9125c92 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -1,17 +1,18 @@ - - + - + + - Starter Node and Angular + + Club Informatique de Polytech Lille + - @@ -20,17 +21,14 @@
- -
diff --git a/public/views/nerd.html b/public/views/nerd.html index 5df5055..4a4ecaa 100644 --- a/public/views/nerd.html +++ b/public/views/nerd.html @@ -1,7 +1,45 @@ - - -
-

Nerds and Proud

- -

{{ tagline }}

-
+
+ +
+

Il y a {{ nerds.length }} personnes au Club Info !

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
NomSectionRôleAction
{{ nerd.login }}{{ nerd.section }}{{ nerd.role }} +
+ + + + + + + +
+
\ No newline at end of file diff --git a/server.js b/server.js index 3613af1..25a867f 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,8 @@ // modules ================================================= var express = require('express'); var app = express(); +var mongoose = require('mongoose'); +var morgan = require('morgan'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); @@ -16,7 +18,7 @@ var port = process.env.PORT || 8080; // connect to our mongoDB database // (uncomment after you enter in your own credentials in config/db.js) -// mongoose.connect(db.url); +mongoose.connect(db.url); // get all data/stuff of the body (POST) parameters // parse application/json -- libgit2 0.21.2