Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit ac06c0d10d725ede5e250da99dcb181b879d5c2f @pgte committed Nov 4, 2011
124 app.js
@@ -0,0 +1,124 @@
+var express = require('express');
+var multipart = require('multipart');
+var fs = require('fs');
+
+var app = express.createServer();
+
+app.configure(function() {
+ app.use(express.logger());
+ app.use(express.bodyDecoder());
+ app.use(express.methodOverride());
+ app.use(express.staticProvider(__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.get('/', function(req, res) {
+ res.render('root');
+});
+
+var products = require('./products');
+var photos = require('./photos');
+
+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);
+ res.render('products/show', {locals: {
+ product: product
+ }});
+});
+
+app.get('/products/:id/edit', function(req, res) {
+ var product = products.find(req.params.id);
+ photos.list(function(err, photo_list) {
+ if (err) {
+ throw err;
+ }
+ res.render('products/edit', {locals: {
+ product: product,
+ photos: photo_list
+ }});
+
+ });
+});
+
+app.put('/products/:id', function(req, res) {
+ var id = req.params.id;
+ products.set(id, req.body.product);
+ res.redirect('/products/'+id);
+});
+
+/* Photos */
+
+app.get('/photos', function(req, res) {
+ photos.list(function(err, photo_list) {
+ res.render('photos/index', {locals: {
+ photos: photo_list
+ }})
+ });
+});
+
+app.get('/photos/new', function(req, res) {
+ res.render('photos/new');
+});
+
+app.post('/photos', function(req, res) {
+ req.setEncoding('binary');
+
+ var parser = multipart.parser();
+
+ parser.headers = req.headers;
+ var ws;
+
+ parser.onPartBegin = function(part) {
+ ws = fs.createWriteStream(__dirname + '/static/uploads/photos/' + part.filename);
+ ws.on('error', function(err) {
+ throw err;
+ });
+ };
+
+ parser.onData = function(data) {
+ ws.write(data, 'binary');
+ };
+
+ parser.onPartEnd = function() {
+ ws.end();
+ parser.close();
+ res.redirect('/photos');
+ };
+
+ req.on('data', function(data) {
+ parser.write(data);
+ });
+
+});
+
+app.listen(4000);
@@ -0,0 +1,14 @@
+var fs = require('fs');
+
+var src_path = __dirname + '/static/uploads/photos/'
+module.exports.list = function(callback) {
+ fs.readdir(src_path, function(err, files) {
+ var ret_files = [];
+ files.forEach(function(file) {
+ ret_files.push('/uploads/photos/' + file);
+ });
+ console.log(ret_files);
+ callback(err, ret_files);
+
+ });
+};
@@ -0,0 +1,50 @@
+var 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,10 @@
+body {
+ font-family: Helvetica, Arial, Sans-Serif;
+ font-size: 16px;
+}
+#main {
+ width: 960px;
+ margin: 0 auto;
+ border: 1px solid #ddd;
+ padding: 1em;
+}
Binary file not shown.
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.
@@ -0,0 +1,8 @@
+!!! 5
+html
+ head
+ link(rel='stylesheet', href='/stylesheets/styles.css')
+ title My template
+ body
+ #main
+ #container!= body
@@ -0,0 +1,4 @@
+p
+ label(for='photo') Photo:
+p
+ input(type='file', name='photo')
@@ -0,0 +1,5 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products/'+product.id) Details
@@ -0,0 +1,20 @@
+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_name', 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
+ label(for='product_photo') Photo:
+p
+ select(id='product_photo', name='product[photo]')
+ - each photo in photos
+ option(value=photo, selected=(product.photo == photo))= photo
+p
+ input(type='submit')
@@ -0,0 +1,7 @@
+h1 Photos:
+p
+ a(href='/photos/new') Create
+ul
+ - each photo in photos
+ li
+ img(src=photo)
@@ -0,0 +1,4 @@
+h1 New Photo
+form(action='/photos', method='POST', enctype='multipart/form-data')!= partial('photo_form')
+ 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('product_form', {locals: {product: product, photos: photos}})
@@ -0,0 +1,4 @@
+h1 Products:
+#products!= partial('product', {collection: products})
+p
+ a(href='/products/new') New
@@ -0,0 +1,2 @@
+h1 New product
+form(action='/products', method='POST')!= partial('product_form', {locals: {product: product}})
@@ -0,0 +1,10 @@
+h2= product.name
+p= product.description
+- if (product.photo)
+ p
+ img(src=product.photo)
+p Price: #{product.price}
+p
+ a(href='/products') Back
+a
+ a(href='/products/'+product.id+'/edit') Edit
@@ -0,0 +1,2 @@
+h2 Hello
+p World!

0 comments on commit ac06c0d

Please sign in to comment.