Skip to content

Commit

Permalink
Made all normal actions for a contact work. Now you can create, updat…
Browse files Browse the repository at this point in the history
…e, delete and show contacts
  • Loading branch information
jettro committed Jul 1, 2011
1 parent 3c861a2 commit 765ab9c
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 17 deletions.
60 changes: 55 additions & 5 deletions ContactController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ function ContactController(contactsRepository) {
repository = contactsRepository;
}

ContactController.prototype.listContacts = function(req,res) {
ContactController.prototype.listContacts = function(req, res) {
repository.listContacts(function(contacts) {
res.render('index', {locals: {contacts: contacts}});
});
};

ContactController.prototype.newContactShowForm = function(req,res) {
res.render('newcontact',{locals: {error:''}});
ContactController.prototype.contact = function(req,res) {
var identifier = req.params.identifier;
repository.obtainContact(identifier,function(contact) {
res.render('detailscontact', {locals: {contact: contact}});
});
};

/* create a contact */
ContactController.prototype.newContactShowForm = function(req, res) {
res.render('newcontact', {locals: {error:''}});
};

ContactController.prototype.newContactPostForm = function(req,res) {
repository.newContact(req.body.new_name, function(code,message) {
ContactController.prototype.newContactPostForm = function(req, res) {
repository.newContact(req.body.new_name, function(code, message) {
if (code == "ok") {
res.redirect("/");
} else {
Expand All @@ -28,4 +36,46 @@ ContactController.prototype.newContactPostForm = function(req,res) {
});
};

/* change a contact */
ContactController.prototype.changeContactShowForm = function(req, res) {
var identifier = req.params.identifier;
console.log("Identifier to get details for is %s", identifier);
repository.obtainContact(identifier,function(contact) {
res.render('changecontact', {locals: {error:'', contact:contact}});
});
};

ContactController.prototype.changeContactPostForm = function(req, res) {
var contact = {};
contact.name = req.body.new_name;
contact.identifier = req.params.identifier;

repository.changeNameOfContact(contact, function(code, message) {
if (code == "ok") {
res.redirect("/");
} else {
res.render('changecontact', {locals: {error:message, contact:contact}});
}
});
};

/* Delete a contact */
ContactController.prototype.deleteContactShowForm = function(req, res) {
var identifier = req.params.identifier;
repository.obtainContact(identifier,function(contact) {
res.render('deletecontact', {locals: {error:'', contact:contact}});
});
};

ContactController.prototype.deleteContactPostForm = function(req, res) {
var identifier = req.params.identifier;
repository.removeContact(identifier, function(code, message) {
if (code == "ok") {
res.redirect("/");
} else {
res.render('deletecontact', {locals: {error:message, contact:contact}});
}
});
};

module.exports = ContactController;
57 changes: 54 additions & 3 deletions ContactRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ ContactRepository.prototype.listContacts = function(callback) {
callback(JSON.parse(data));
});
});

};

ContactRepository.prototype.newContact = function(name, callback) {
Expand All @@ -36,9 +35,9 @@ ContactRepository.prototype.newContact = function(name, callback) {
res.on('data', function(data) {
if (res.statusCode != 200) {
console.log(data);
callback('error','Maybe the name is already taken?');
callback('error', 'Maybe the name is already taken?');
} else {
callback('ok','The new contact has been send')
callback('ok', 'The new contact has been send')
}
});
});
Expand All @@ -50,6 +49,58 @@ ContactRepository.prototype.newContact = function(name, callback) {
req.end();
};

ContactRepository.prototype.changeNameOfContact = function(contact, callback) {
var opts = createHttpRequestOpts('/contacts', 'PUT');

var req = http.request(opts, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
if (res.statusCode != 200) {
console.log(data);
callback('error', 'Maybe the name is already taken?');
} else {
callback('ok', 'The contact has been updated')
}
});
});

req.write(JSON.stringify(contact));
req.end();
};

ContactRepository.prototype.obtainContact = function(identifier, callback) {
var opts = createHttpRequestOpts('/contacts/' + identifier, 'GET');

var req = http.get(opts, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
callback(JSON.parse(data));
});
});
};

ContactRepository.prototype.removeContact = function(identifier, callback) {
var opts = createHttpRequestOpts('/contacts', 'DELETE');

var req = http.request(opts, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
if (res.statusCode != 200) {
console.log(data);
callback('error', 'Did you for get to send an identifier?');
} else {
callback('ok', 'The new contact removal process has been send')
}
});
});

var contact = {};
contact.identifier = identifier;

req.write(JSON.stringify(contact));
req.end();
};

function createHttpRequestOpts(path, method) {
return {
host: host,
Expand Down
9 changes: 7 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ app.configure(function() {
});

app.get('/', contactController.listContacts);
app.get('/new', contactController.newContactShowForm);
app.post('/new', contactController.newContactPostForm);
app.get('/contact/new', contactController.newContactShowForm);
app.post('/contact/new', contactController.newContactPostForm);
app.get('/contact/:identifier',contactController.contact);
app.get('/contact/:identifier/edit',contactController.changeContactShowForm);
app.post('/contact/:identifier/edit',contactController.changeContactPostForm);
app.get('/contact/:identifier/delete',contactController.deleteContactShowForm);
app.post('/contact/:identifier/delete',contactController.deleteContactPostForm);

app.listen(8018);
console.log('Express server started on port %s', app.address().port);
8 changes: 8 additions & 0 deletions public/style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@

.error {
color: red;
}

.address {
margin-left: 100px;
}

.address table th {
text-align: left;
}
73 changes: 67 additions & 6 deletions test/TestContactRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,73 @@
*/
var assert = require('assert');
var ContactRepository = require("../ContactRepository");
var repository = ContactRepository.createRepo('localhost',8080);
var repository = ContactRepository.createRepo('localhost', 8080);

repository.listContacts(function(contacts){
assert.equal(2,contacts.length, "Number of contacts is not right: " + contacts.length);
// We chain the tests to make it easier to use the existing data set
repository.listContacts(function(contacts) {
assert.equal(2, contacts.length, "Number of contacts is not right: " + contacts.length);
testNewContact();
});

repository.newContact("My Test 2", function(code,message) {
assert.equal("ok", code, "This should be no problem and an ok should be returned: " + code);
});
function testNewContact() {
repository.newContact("My Test", function(code, message) {
assert.equal("ok", code, "This should be no problem and an ok should be returned: " + code);
// Check if the amount of contacts is increased
repository.listContacts(function(contacts) {
// Beware that we use the query database to verify, race conditions might happen and fail this test
assert.equal(3, contacts.length, "Number of contacts is not right, create did not work: " + contacts.length);
testObtainDetails();
});
});
}

function testObtainDetails() {
repository.listContacts(function(contacts) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].name == "Allard") {
repository.obtainContact(contacts[i].identifier, function(contact) {
assert.equal("Allard", contact.name, "name of contact is not as expected");
assert.equal("PRIVATE", contact.addresses[0].addressType, "AddressType of first address not as expected");
assert.equal("AxonBoulevard 1", contact.addresses[0].streetAndNumber, "Street not as expected");
assert.equal("1234AB", contact.addresses[0].zipCode, "Zip code not as expected");
assert.equal("The Hague", contact.addresses[0].city, "City not as expected");
testChangeName();
});
}
}
});
}

function testChangeName() {
//obtain id for contact with name "My Test" and remove that contact
repository.listContacts(function(contacts) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].name == "My Test") {
var contact = contacts[i];
contact.name = "My Test Updated";
repository.changeNameOfContact(contact, function(code,message) {
assert.equal("ok", code, "This should be no problem and an ok should be returned: " + code);
testRemoveContact();
});
}
}
});
}

function testRemoveContact() {
//obtain id for contact with name "My Test" and remove that contact
repository.listContacts(function(contacts) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].name == "My Test Updated") {
repository.removeContact(contacts[i].identifier, function(code, message) {
assert.equal("ok", code, "Removing the contact failed, problem with the identifier?: " + message);
// Check that the amount of contacts is now decreased
repository.listContacts(function(contacts) {
// Beware that we use the query database to verify, race conditions might happen and fail this test
assert.equal(2, contacts.length, "Number of contacts is not right, remove did not work: " + contacts.length);
});
});
}
}
});
}
14 changes: 14 additions & 0 deletions views/changecontact.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
h1 Change Contact Name
- if (error)
div.error
#{error}
div
div
form(method="post")
fieldset
legend change contact name
p
label(for="new_name") Name:
input(type="text", name="new_name", size="50", value="#{contact.name}")
p.buttons
input(type="submit", value="Update")
12 changes: 12 additions & 0 deletions views/deletecontact.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h1 Delete Contact
- if (error)
div.error
#{error}
div
div
form(method="post")
fieldset
legend change contact name
p Name: #{contact.name}
p.buttons
input(type="submit", value="Delete")
18 changes: 18 additions & 0 deletions views/detailscontact.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
h1 Contact: #{contact.name}
div
- each address in contact.addresses
tr
td(colspan="2")
div.address
h2 #{address.addressType}
table
tr
th street
td #{address.streetAndNumber}
tr
th zip code
td #{address.zipCode}
tr
th city
td #{address.city}

4 changes: 4 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ table
tr
td #{contact.identifier}
td #{contact.name}
td
a(href="/contact/#{contact.identifier}") details |
a(href="/contact/#{contact.identifier}/edit") edit |
a(href="/contact/#{contact.identifier}/delete") delete


2 changes: 1 addition & 1 deletion views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ html(lang="en")
body
div#heading
a(href="/") List
a(href="/new") New Contact
a(href="/contact/new") New Contact
div#body!= body
div#footer
p footer
Expand Down

0 comments on commit 765ab9c

Please sign in to comment.