Permalink
Browse files

added a new feature to create a new contact. Also prepared the jquery…

…-ui stuff that I am planning on using.
  • Loading branch information...
1 parent 0c312e2 commit 3c861a29f907ab624d5a98b19b5e66776da96578 @jettro committed Jun 30, 2011
View
@@ -14,4 +14,18 @@ ContactController.prototype.listContacts = function(req,res) {
});
};
+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) {
+ if (code == "ok") {
+ res.redirect("/");
+ } else {
+ res.render('newcontact', {locals: {error:message}});
+ }
+ });
+};
+
module.exports = ContactController;
View
@@ -7,7 +7,8 @@ var http = require('http');
var host = 'localhost';
var port = 8080;
-function ContactRepository() {}
+function ContactRepository() {
+}
ContactRepository.createRepo = function(host, port) {
var repo = new ContactRepository();
@@ -17,16 +18,38 @@ ContactRepository.createRepo = function(host, port) {
};
ContactRepository.prototype.listContacts = function(callback) {
- var opts = createHttpRequestOpts('/contacts','GET');
+ var opts = createHttpRequestOpts('/contacts', 'GET');
- var req = http.get(opts, function(res){
+ var req = http.get(opts, function(res) {
res.on('data', function(data) {
callback(JSON.parse(data));
});
});
};
+ContactRepository.prototype.newContact = function(name, callback) {
+ var opts = createHttpRequestOpts('/contacts', 'POST');
+
+ 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 new contact has been send')
+ }
+ });
+ });
+
+ var contact = {};
+ contact.name = name;
+
+ req.write(JSON.stringify(contact));
+ req.end();
+};
+
function createHttpRequestOpts(path, method) {
return {
host: host,
View
2 app.js
@@ -20,6 +20,8 @@ app.configure(function() {
});
app.get('/', contactController.listContacts);
+app.get('/new', contactController.newContactShowForm);
+app.post('/new', contactController.newContactPostForm);
app.listen(8018);
console.log('Express server started on port %s', app.address().port);

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,4 @@
+/**
+ *
+ * @author Jettro Coenradie
+ */
View
@@ -0,0 +1,16 @@
+#heading {
+ display: block;
+}
+#heading a {
+ padding-right: 10px;
+}
+#body {
+ display: block;
+}
+#footer {
+ display: block;
+}
+
+.error {
+ color: red;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -8,5 +8,9 @@ var ContactRepository = require("../ContactRepository");
var repository = ContactRepository.createRepo('localhost',8080);
repository.listContacts(function(contacts){
- assert.equal(2,contacts.length, "Number of contacts is not right");
+ assert.equal(2,contacts.length, "Number of contacts is not right: " + contacts.length);
+});
+
+repository.newContact("My Test 2", function(code,message) {
+ assert.equal("ok", code, "This should be no problem and an ok should be returned: " + code);
});
View
@@ -3,11 +3,14 @@ html(lang="en")
head
title Nodejs presenter
link(href="/style/main.css", media="screen", rel="stylesheet", type="text/css")
+ link(href="/style/sunny/jquery-ui-1.8.14.custom.css", media="screen", rel="stylesheet", type="text/css")
script(src='/js/jquery-1.6.1.min.js')
+ script(src='/js/jquery-ui-1.8.14.custom.min.js')
script(src='/js/local.js')
body
div#heading
- p header
+ a(href="/") List
+ a(href="/new") New Contact
div#body!= body
div#footer
p footer
View
@@ -0,0 +1,14 @@
+h1 New Contact
+- if (error)
+ div.error
+ #{error}
+div
+ div
+ form(method="post")
+ fieldset
+ legend new contact
+ p
+ label(for="new_name") Name:
+ input(type="text", name="new_name", size="50")
+ p.buttons
+ input(type="submit", value="Create")

0 comments on commit 3c861a2

Please sign in to comment.