Permalink
Browse files

Made it possible to handle addresses as well, now you can enter new, …

…change existing and remove addresses
  • Loading branch information...
1 parent 765ab9c commit cda325c4e7e2ec00557a0df5c10172a7987c163c @jettro committed Jul 2, 2011
View
@@ -78,4 +78,98 @@ ContactController.prototype.deleteContactPostForm = function(req, res) {
});
};
-module.exports = ContactController;
+/* add an Address */
+ContactController.prototype.newAddressShowForm = function(req,res) {
+ var identifier = req.params.identifier;
+ repository.obtainContact(identifier,function(contact) {
+ res.render('newaddress', {locals: {error:'', contact:contact}});
+ });
+};
+
+ContactController.prototype.newAddressPostForm = function(req,res) {
+ var address = {};
+ address.identifier = req.params.identifier;
+ address.addressType = req.body.addressType;
+ address.streetAndNumber = req.body.streetAndNumber;
+ address.zipCode = req.body.zipCode;
+ address.city = req.body.city;
+ repository.addAddress(address,function(code,message) {
+ if (code == "ok") {
+ res.redirect("/contact/"+address.identifier);
+ } else {
+ res.render('newaddress', {locals: {error:message, contact:contact}});
+ }
+ });
+};
+
+/* change an address */
+ContactController.prototype.changeAddressShowForm = function(req,res) {
+ var identifier = req.params.identifier;
+ var requestedAddressType = req.params.addressType;
+ repository.obtainContact(identifier,function(contact) {
+ var address = findRequestAddressByType(contact.addresses,requestedAddressType);
+ if (address) {
+ res.render('changeaddress', {locals:{error:'', address:address}});
+ } else {
+ res.redirect('/contact/'+contact.identifier+'/address/new');
+ }
+ });
+};
+
+ContactController.prototype.changeAddressPostForm = function(req,res) {
+ var address = {};
+ address.identifier = req.params.identifier;
+ address.addressType = req.params.addressType;
+ address.streetAndNumber = req.body.streetAndNumber;
+ address.zipCode = req.body.zipCode;
+ address.city = req.body.city;
+ repository.addAddress(address,function(code,message) {
+ if (code == "ok") {
+ res.redirect("/contact/"+address.identifier);
+ } else {
+ res.render('changeaddress', {locals: {error:message, contact:contact}});
+ }
+ });
+};
+
+/* Remove an address */
+ContactController.prototype.deleteAddressShowForm = function(req,res) {
+ var identifier = req.params.identifier;
+ var requestedAddressType = req.params.addressType;
+ repository.obtainContact(identifier,function(contact) {
+ var address = findRequestAddressByType(contact.addresses,requestedAddressType);
+ if (address) {
+ res.render('deleteaddress', {locals:{error:'', address:address}});
+ } else {
+ res.redirect('/contact/'+contact.identifier);
+ }
+ });
+};
+
+ContactController.prototype.deleteAddressPostForm = function(req,res) {
+ var address = {};
+ address.identifier = req.params.identifier;
+ address.addressType = req.params.addressType;
+ repository.removeAddress(address,function(code,message) {
+ if (code == "ok") {
+ res.redirect("/contact/"+address.identifier);
+ } else {
+ res.render('deleteaddress', {locals: {error:message, address:address}});
+ }
+ });
+};
+
+module.exports = ContactController;
+
+/* helper functions */
+function findRequestAddressByType(addresses,addressType) {
+ if (!addresses) {
+ return null;
+ }
+ for (var i = 0; i < addresses.length; i++) {
+ if (addresses[i].addressType == addressType) {
+ return addresses[i];
+ }
+ }
+ return null;
+}
View
@@ -89,7 +89,7 @@ ContactRepository.prototype.removeContact = function(identifier, callback) {
console.log(data);
callback('error', 'Did you for get to send an identifier?');
} else {
- callback('ok', 'The new contact removal process has been send')
+ callback('ok', 'The new contact removal request has been send')
}
});
});
@@ -101,6 +101,47 @@ ContactRepository.prototype.removeContact = function(identifier, callback) {
req.end();
};
+ContactRepository.prototype.addAddress = function(address,callback) {
+ var opts = createHttpRequestOpts('/contacts/'+address.identifier+'/address', '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', 'Did you for get to send an identifier?');
+ } else {
+ callback('ok', 'The new address has been send')
+ }
+ });
+ });
+
+ req.write(JSON.stringify(address));
+ req.end();
+
+};
+
+ContactRepository.prototype.removeAddress = function(address,callback) {
+ var opts = createHttpRequestOpts('/contacts/'+address.identifier+'/address', '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 or the address type?');
+ } else {
+ callback('ok', 'The address has been removed')
+ }
+ });
+ });
+
+ req.write(JSON.stringify(address));
+ req.end();
+
+};
+
+
function createHttpRequestOpts(path, method) {
return {
host: host,
View
6 app.js
@@ -27,6 +27,12 @@ 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.get('/contact/:identifier/address/new',contactController.newAddressShowForm);
+app.post('/contact/:identifier/address/new',contactController.newAddressPostForm);
+app.get('/contact/:identifier/address/:addressType/edit',contactController.changeAddressShowForm);
+app.post('/contact/:identifier/address/:addressType/edit',contactController.changeAddressPostForm);
+app.get('/contact/:identifier/address/:addressType/delete',contactController.deleteAddressShowForm);
+app.post('/contact/:identifier/address/:addressType/delete',contactController.deleteAddressPostForm);
app.listen(8018);
console.log('Express server started on port %s', app.address().port);
@@ -1,77 +1,115 @@
/**
- * Test class for ContactRepository
+ * Test class for ContactRepository. All methods in this class have been chained. This makes it easier to create a test
+ * that adds a contact, adds address, obtain items and remove them again. After every successful run the data should be
+ * left in the state where it went of from.
+ *
+ * We tried to make the methods self maintained, but they do build on the previous test case, so it might be hard to
+ * run just one test.
*
* @author Jettro Coenradie
*/
var assert = require('assert');
var ContactRepository = require("../ContactRepository");
var repository = ContactRepository.createRepo('localhost', 8080);
-// We chain the tests to make it easier to use the existing data set
+var numContacts;
+
+// Initialize the test by obtaining the existing contacts and remembering the amount of contacts available
repository.listContacts(function(contacts) {
- assert.equal(2, contacts.length, "Number of contacts is not right: " + contacts.length);
+ numContacts = contacts.length;
testNewContact();
});
function testNewContact() {
+ logTestName("New contact");
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();
+ assert.equal(numContacts + 1, contacts.length, "Number of contacts is not right, create did not work: " + contacts.length);
+ testAddAddress();
});
});
}
+function testAddAddress() {
+ logTestName("Add address");
+ findContactByName("My Test", function(contact) {
+ var address = {};
+ address.identifier = contact.identifier;
+ address.addressType = "PRIVATE";
+ address.streetAndNumber = "Teststraat 8";
+ address.zipCode = "1234ZE";
+ address.city = "Testville";
+ repository.addAddress(address, function(code, message) {
+ assert.equal("ok",code,"Problem while adding an address: " + message);
+ 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();
- });
- }
- }
+ logTestName("Obtain details");
+ findContactByName("My Test", function(listedContact) {
+ repository.obtainContact(listedContact.identifier, function(contact) {
+ assert.equal("My Test", 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("Teststraat 8", contact.addresses[0].streetAndNumber, "Street not as expected");
+ assert.equal("1234ZE", contact.addresses[0].zipCode, "Zip code not as expected");
+ assert.equal("Testville", 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();
- });
- }
- }
+ logTestName("Change name");
+ findContactByName("My Test", function(contact) {
+ 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);
+ testRemoveAddress();
+ });
+ });
+}
+
+function testRemoveAddress() {
+ logTestName("Remove address");
+ findContactByName("My Test Updated", function(listedcontact) {
+ repository.obtainContact(listedcontact.identifier, function(contact) {
+ repository.removeAddress(contact.addresses[0], 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
+ logTestName("Remove contact");
+ findContactByName("My Test Updated", function(contact) {
+ repository.removeContact(contact.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(numContacts, contacts.length, "Number of contacts is not right, remove did not work: " + contacts.length);
+ });
+ });
+ });
+}
+
+/* Helper functions */
+function findContactByName(name, callback) {
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);
- });
- });
+ if (contacts[i].name == name) {
+ callback(contacts[i]);
}
}
});
}
+
+function logTestName(testname) {
+ console.log("Running test: %s", testname);
+}
View
@@ -0,0 +1,19 @@
+h1 Change #{address.addressType} Address for #{address.name}
+- if (error)
+ div.error
+ #{error}
+p You cannot change the type of an address, that what be a create instead of a change due to the chosen model
+div
+ form(method="post")
+ input(type="hidden", value="#{address.addressType}")
+ fieldset
+ legend change #{address.addressType} address
+ p
+ label(for="streetAndNumber") Street and number
+ input(type="text", name="streetAndNumber", size="50", value="#{address.streetAndNumber}")
+ label(for="zipCode") Zip code
+ input(type="text", name="zipCode", size="10", value="#{address.zipCode}")
+ label(for="city") City
+ input(type="text", name="city", size="50", value="#{address.city}")
+ p.buttons
+ input(type="submit", value="Update")
View
@@ -3,12 +3,11 @@ h1 Change Contact Name
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")
+ 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
@@ -0,0 +1,19 @@
+h1 Delete #{address.addressType} Address for #{address.name}
+- if (error)
+ div.error
+ #{error}
+form(method="post")
+ 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}
+ p.buttons
+ input(type="submit", value="Delete")
Oops, something went wrong.

0 comments on commit cda325c

Please sign in to comment.