Permalink
Browse files

added episode11

  • Loading branch information...
okamuuu committed Feb 24, 2012
1 parent e02d009 commit eed154fd10b1c2d73f14fbd6b73ef52a59b2542f
View
@@ -0,0 +1,87 @@
+var express = require('express');
+
+var app = express.createServer();
+
+app.configure(function () {
+ app.use(express.logger());
+ // renamed bodyDecoder to podyParser from express v2.3.2
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ // staticProvider has gone. we can use static instead of it now.
+ app.use(express.static(__dirname + '/static'));
+});
+
+app.configure('development', function () {
+ app.use(express.errorHandler({
+ dumpExceptions: true,
+ showStack: true
+ }));
+});
+
+app.configure('production', function () {
+ app.use(express.errorHandler({}));
+});
+
+app.set('views', __dirname + '/views');
+app.set('view engine', 'jade');
+app.set('view options', {layout: true});
+
+app.get('/', function (req, res) {
+ res.render('root');
+});
+
+var products = require('./products');
+
+app.get('/products', function (req, res) {
+ res.render('products/index', {locals: {
+ products: products.all
+ }});
+});
+
+app.get('/products/new', function(req, res) {
+ res.render('products/new', {locals: {
+ product: req.body && req.body.product || products.new()
+ }});
+});
+
+app.post('/products', function(req, res) {
+ var id = products.insert(req.body.product);
+ res.redirect('/products/' + id);
+});
+
+
+
+app.get('/products/:id', function(req, res) {
+ var product = products.find(req.params.id);
+ if(product != null) {
+ res.render('products/show', {locals: {
+ product: product
+ }});
+ } else {
+ res.send('404 - Product not found!')
+ }
+});
+
+app.get('/products/:id/edit', function (req, res) {
+ var product = products.find(req.params.id);
+
+ res.render('products/edit', {locals: {
+ product: product
+ }});
+});
+
+app.put('/products/:id', function(req, res) {
+ var id = req.params.id;
+ // req.body.product comes from bodyParser
+ console.log(req.body);
+ console.log(req.body.product);
+ products.set(id, req.body.product);
+ res.redirect('/products/' + id);
+});
+
+
+
+
+
+
+app.listen(4000);
View
@@ -0,0 +1,51 @@
+products = [
+{
+ id: 1,
+ name: 'Mac Book Pro',
+ description: 'Apple 13 inch Mac Book Pro Notebook',
+ price: 1000
+},
+{
+ id: 2,
+ name: 'iPad',
+ description: 'Apple 64GB 3G iPad',
+ price: 899
+}
+];
+
+module.exports.all = products;
+
+module.exports.find = function(id) {
+ id = parseInt(id, 10);
+ var found = null;
+ productloop: for(product_index in products) {
+ var product = products[product_index];
+ if(product.id == id) {
+ found = product;
+ break productloop;
+ }
+ };
+ return found;
+}
+
+module.exports.set = function(id, product) {
+ id = parseInt(id, 10);
+ product.id = id;
+ products[id -1] = product;
+}
+
+module.exports.new = function() {
+ return {
+ name: '',
+ description: '',
+ price: 0
+ };
+}
+
+module.exports.insert = function(product) {
+ var id = products.length + 1;
+ product.id = id;
+ products[id-1] = product;
+ return id;
+}
+
@@ -0,0 +1,11 @@
+body {
+ font-family: Helvetica, Arial, Sans-Serif;
+ font-size: 16px;
+}
+
+#main {
+ width: 960px;
+ margin: 0 auto;
+ border: 1px solid #ddd;
+ padding: 1em;
+}
@@ -0,0 +1,9 @@
+!!! 5
+html
+ head
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ title My template
+ body
+ #main
+ h1 Content goes here
+ #container!= body
@@ -0,0 +1,9 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products/' + product.id) Details
+- if(product.photo)
+ p
+ img(src=product.photo)
+
@@ -0,0 +1,15 @@
+p
+ label(for='product_name') Name:
+p
+ input(type='text', id='product_name', name='product[name]', value=product.name)
+p
+ label(for='product_description') Description:
+p
+ textarea(id='product_description', name='product[description]')= product.description
+p
+ label(for='product_price') Price:
+p
+ input(type='text', id='product_price', name='product[price]', value=product.price)
+p
+ input(type='submit')
+
@@ -0,0 +1,4 @@
+h1 Editing #{product.name}
+form(action='/products/' + product.id, method='POST')
+ input(type='hidden', name='_method', value='PUT')
+ div!= partial('../partials/product_form', {locals: {product: product}})
@@ -0,0 +1,4 @@
+h1 Products:
+#products!= partial('../partials/product', {collection: products})
+p
+ a(href='/products/new') New
@@ -0,0 +1,3 @@
+h1 Editing #{product.name}
+form(action='/products', method='POST')
+ div!= partial('../partials/product_form', {locals: {product: product}})
@@ -0,0 +1,7 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products') Back
+p
+ a(href='/products/' + product.id + '/edit') Edit
@@ -0,0 +1,2 @@
+h2 hello
+p World!

0 comments on commit eed154f

Please sign in to comment.