Skip to content

Commit

Permalink
first iteration of node.js backend works
Browse files Browse the repository at this point in the history
  • Loading branch information
draptik committed Oct 1, 2013
1 parent 5a4d610 commit f35145e
Showing 1 changed file with 99 additions and 53 deletions.
152 changes: 99 additions & 53 deletions backend/js-node-backend/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* http://blog.modulus.io/nodejs-and-express-create-rest-api */
/*
basic idea for creating RESTful service with javascript:
http://blog.modulus.io/nodejs-and-express-create-rest-api
*/

// require('underscore');

// var userModule = require('./users.js');
var userRepository = new UserRepository();
userRepository.createUsers();

var express = require('express');

Expand All @@ -22,34 +28,13 @@ app.use(allowCrossDomain);
/* we'll use the same port as tomcat... */
var MY_PORT = 8080; // default: 4730

/* TODO: Create a Js 'class' or 'module' container for 'UserService'...*/

/* init users... ------------------------------------- */
var users = [];
var numberOfUsers = 10;
for (var i = 1; i <= numberOfUsers; i++) {
var user = { id: i, firstName: 'Foo' + i, lastName: 'Bar' + i};
users.push(user);
};

var getMaxUserId = function(array) {
return Math.max.apply(Math, array.map(function(o) {
return o.id;
}));
};

// var getById = function(id) {
// return find(users, function(user) {
// return user.id === id
// });
// };

/* REST API =========================================== */
var baseUrl = '/ngdemo/web';

/* GET ALL -------------------------------------------- */
app.get(baseUrl + '/users', function(req, res) {
res.json(users);
res.json(userRepository.getAll());
});

/* GET Dummy ------------------------------------------ */
Expand All @@ -59,12 +44,9 @@ app.get(baseUrl + '/dummy', function(req, res) {

/* GET By Id ------------------------------------------ */
app.get(baseUrl + '/users/:id', function(req, res) {
if(users.length <= req.params.id || req.params.id < 0) {
res.statusCode = 404;
return res.send('Error 404: No user found');
}

res.json(users[req.params.id - 1]);
console.log('trying to retrieve user with id: ' + req.params.id);
var user = userRepository.getById(req.params.id);
res.json(user);
});


Expand All @@ -75,13 +57,7 @@ app.post(baseUrl + '/users', function(req, res) {
return res.send('Error 400: POST syntax incorrect.');
}

var newUser = {
id: getMaxUserId(users) + 1,
firstName : req.body.firstName,
lastName : req.body.lastName
};

users.push(newUser);
var newUser = userRepository.addNewUser(req.body.firstName, req.body.lastName);
res.json(newUser);
});

Expand All @@ -91,29 +67,99 @@ app.put(baseUrl + '/users/:id', function (req, res) {
res.statusCode = 400;
return res.send('Error 400: PUT syntax incorrect.');
}

var id = req.params.id - 1;
users[id].firstName = req.body.firstName;
users[id].lastName = req.body.lastName;

res.json(users[id]);
var changedUser = userRepository.changeUser(req.params.id, req.body.firstName, req.body.lastName);
res.json(changedUser);
});

/* DELETE --------------------------------------------- */
app.delete(baseUrl + '/users/:id', function(req, res) {
if(users.length <= req.params.id) {
res.statusCode = 404;
return res.send('Error 404: No user found');
}

var index = req.params.id - 1;

// TODO find index with id...

users.splice(index, 1);
console.log('trying to delete user with id: ' + req.params.id);
userRepository.deleteUser(req.params.id);
res.json(true);
});

/* ==================================================== */

app.listen(process.env.PORT || MY_PORT);

/* Mmmhh... how can I place the code below into a seperate file and load it here? */

function User(id, firstName, lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
};


function UserRepository() {

this.users = [];

this.createUsers = function() {
var numberOfUsers = 10;
for (var i = 0; i < numberOfUsers; i++) {
var id = i + 1;
this.users.push(new User(id, 'Foo' + id, 'Bar' + id));
};
return this.users;
};

this.getMaxUserId = function() {
return Math.max.apply(Math, this.users.map(function(user) {
return user.id;
}));
};

this.getNumberOfUsers = function() {
return this.users.length;
};

this.getAll = function() {
return this.users;
};

this.getById = function(id) {
var foundUser = false;
for (var i = 0; i < this.users.length; i++) {
var user = this.users[i];
console.log('...checking user.id ' + user.id);
if (user.id == id) {
foundUser = true;
return user;
};
};
if (!foundUser) {
console.log('Could not find user with id: ' + id);
return 'user with id ' + id + ' not found.';
};
};

this.addNewUser = function(firstName, lastName) {
var newUser = new User(this.getMaxUserId() + 1, firstName, lastName);
this.users.push(newUser);
return this.getById(newUser.id);
};

this.changeUser = function(id, firstName, lastName) {
var user = this.getById(id);
user.firstName = firstName;
user.lastName = lastName;
return user;
};

this.deleteUser = function(id) {
// sorry, i'm tired and don't know javascript that well...
var indexToDelete = -1;
for (var i = 0; i < this.users.length; i++) {
var user = this.users[i];
if (user.id == id) {
indexToDelete = i;
break;
};
};

if (indexToDelete >= 0) {
this.users.splice(indexToDelete, 1);
};
};
};

0 comments on commit f35145e

Please sign in to comment.