Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made all normal actions for a contact work. Now you can create, updat…

…e, delete and show contacts
  • Loading branch information...
commit 765ab9c51f1c5d1874636e850214ce5cf2133645 1 parent 3c861a2
@jettro authored
View
60 ContactController.js
@@ -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 {
@@ -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;
View
57 ContactRepository.js
@@ -25,7 +25,6 @@ ContactRepository.prototype.listContacts = function(callback) {
callback(JSON.parse(data));
});
});
-
};
ContactRepository.prototype.newContact = function(name, callback) {
@@ -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')
}
});
});
@@ -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,
View
9 app.js
@@ -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);
View
8 public/style/main.css
@@ -13,4 +13,12 @@
.error {
color: red;
+}
+
+.address {
+ margin-left: 100px;
+}
+
+.address table th {
+ text-align: left;
}
View
73 test/TestContactRepository.js
@@ -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);
+ });
+ });
+ }
+ }
+ });
+}
View
14 views/changecontact.jade
@@ -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")
View
12 views/deletecontact.jade
@@ -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")
View
18 views/detailscontact.jade
@@ -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}
+
View
4 views/index.jade
@@ -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
View
2  views/layout.jade
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.