Permalink
Browse files

init

  • Loading branch information...
fengmk2 committed Jul 6, 2011
0 parents commit af82ab8e51ee50f190a3c92c32743df3e8a47c4e
@@ -0,0 +1,23 @@
+
+var Client = require('mysql').Client;
+
+exports.port = 8080;
+exports.email='fengmk2@gmail.com';
+exports.site_name = 'Node TODO';
+exports.site_desc = '';
+
+var db_options = {
+ host: 'localhost',
+ port: 3306,
+ user: 'root',
+ password: '123456',
+ database: 'todo'
+};
+
+var db = exports.db = new Client(db_options);
+db.connect(function(err) {
+ if(err) {
+ console.error('connect db ' + db.host + ' error: ' + err);
+ process.exit();
+ }
+});
@@ -0,0 +1,68 @@
+
+var config = require('../config')
+ , db = config.db;
+
+exports.index = function(req, res) {
+ db.query('select * from todo order by finished asc, id asc', function(err, rows) {
+ res.render('index', {todos: rows});
+ });
+};
+
+exports.new = function(req, res, next) {
+ var title = req.body.title || '';
+ title = title.trim();
+ if(!title) {
+ return res.render('error', {message: '标题是必须的'});
+ }
+ db.query('insert into todo set title=?, post_date=now()', [title], function(err, result) {
+ if(err) return next(err);
+ res.redirect('/');
+ });
+};
+
+exports.view = function(req, res, next) {
+ res.redirect('/');
+};
+
+exports.edit = function(req, res, next) {
+ var id = req.params.id;
+ db.query('select * from todo where id=?', [id], function(err, rows) {
+ if(err) return next(err);
+ if(rows && rows.length > 0) {
+ var row = rows[0];
+ res.render('todo/edit', {todo: row});
+ } else {
+ next();
+ }
+ });
+};
+
+exports.save = function(req, res, next) {
+ var id = req.params.id;
+ var title = req.body.title || '';
+ title = title.trim();
+ if(!title) {
+ return res.render('error', {message: '标题是必须的'});
+ }
+ db.query('update todo set title=? where id=?', [title, id], function(err, result) {
+ if(err) return next(err);
+ res.redirect('/');
+ });
+};
+
+exports.delete = function(req, res, next) {
+ var id = req.params.id;
+ db.query('delete from todo where id = ?', [id], function(err) {
+ if(err) return next(err);
+ res.redirect('/');
+ });
+};
+
+exports.finish = function(req, res, next) {
+ var finished = req.query.status === 'yes' ? 1 : 0;
+ var id = req.params.id;
+ db.query('update todo set finished=? where id=?', [finished, id], function(err, result) {
+ if(err) return next(err);
+ res.redirect('/');
+ });
+};
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,50 @@
+body { background:#EEE; }
+.box { margin-bottom:20px; }
+.clear { clear:both; }
+.float_left { float:left; }
+.float_right { float:right; }
+.left { text-align:left; }
+.right { text-align:right; }
+.url { background:url(images/website.png) no-repeat right; padding-right:20px; }
+.tips { color:#CCC; }
+.v_top { vertical-align:top; }
+.qq { background:url(images/qq.png) no-repeat right; padding-right:20px; }
+.msn { background:url(images/msn.png) no-repeat right; padding-right:20px; }
+.email { background:url(images/email.png) no-repeat right; padding-right:20px; }
+.tips { color:#666; }
+.long_txt { width:98%; }
+.must_be { color:red !important; }
+textarea { line-height:1.5em; }
+
+image { border:none; }
+p { line-height:1.5em; }
+
+a { color:#3366CC; text-decoration:underline; }
+a:hover { color:#FF3300; }
+
+.page { width:700px; margin:10px auto; background:#FFF; padding:10px 20px; border:1px solid #CCC; border-radius:5px; }
+
+ .header { padding-top:10px; border-bottom:1px solid #CCC; }
+ .header h1 { font-size:50px; }
+ .header a { color:#333; text-decoration:none; }
+
+ .main { }
+ .main_content { }
+
+ .box h2 { padding-bottom:5px; margin-bottom:10px; font-weight:400; color:green; font-size:28px;}
+
+ .todos {}
+ .todos ul li { list-style-type:none; margin-bottom:10px; padding-bottom:10px; border-bottom:1px dotted #CCC; }
+ .todos ul li.finished { color:#666; }
+ .todos ul li a { color:#666; }
+ .todos ul li del { padding-left:15px; background:url(images/ok.gif) no-repeat left center; }
+
+ .post { }
+ .post p { margin-bottom:10px; }
+ .post .submit { line-height:normal; -padding:0; }
+
+
+
+ .foot { border-top:1px solid #CCC; color:#666; padding-top:10px; font-size:11px; }
+ .foot img { vertical-align:top; }
+
@@ -0,0 +1,63 @@
+/*
+KISSY CSS Reset
+理念:清除和重置是紧密不可分的
+特色:1.适应中文 2.基于最新主流浏览器
+维护:玉伯(lifesinger@gmail.com), 正淳(ragecarrier@gmail.com)
+*/
+
+/* 清除内外边距 */
+body, h1, h2, h3, h4, h5, h6, p, blockquote, /* structural elements 结构元素 */
+dl, dt, dd, ul, ol, li, /* list elements 列表元素 */
+pre, /* text formatting elements 文本格式元素 */
+fieldset, lengend, button, input, textarea, /* form elements 表单元素 */
+th, td { /* table elements 表格元素 */
+ margin: 0;
+ padding: 0;
+}
+
+/* 设置默认字体 */
+body,
+button, input, select, textarea { /* for ie */
+ /*font: 12px/1 Tahoma, Helvetica, Arial, "宋体", sans-serif;*/
+ font: 14px/1.5 Arial, sans-serif;
+ padding:3px;
+}
+input { line-height:normal; }
+h1 { font-size:24px; }
+h2 { font-size:20px; }
+h3 { font-size:18px; }
+h4 { font-size:18px; }
+h5 { font-size:14px; }
+h1, h2, h3, h4, h5 { font-weight:normal; }
+
+address, cite, dfn, em, var { font-style: normal; } /* 将斜体扶正 */
+small { font-size: 11px; } /* 小于 12px 的中文很难阅读,让 small 正常化 */
+
+/* 重置文本格式元素 */
+a { color:#03C; }
+a:hover { color:#F30; }
+
+abbr[title], acronym[title] { /* 注:1.ie6 不支持 abbr; 2.这里用了属性选择符,ie6 下无效果 */
+ border-bottom: 1px dotted;
+ cursor: help;
+}
+
+q:before, q:after { content: ''; }
+
+/* 重置表单元素 */
+legend { color: #000; } /* for ie6 */
+fieldset, img { border: none; } /* img 搭车:让链接里的 img 无边框 */
+/* 注:optgroup 无法扶正 */
+button, input, select, textarea {
+ font-size: 100%; /* 使得表单元素在 ie 下能继承字体大小 */
+}
+
+/* 重置表格元素 */
+table {
+ border-collapse: collapse;
+ border-spacing: 0;
+}
+
+/* 让非ie浏览器默认也显示垂直滚动条,防止因滚动条引起的闪烁 */
+html { overflow-y: scroll; }
+
@@ -0,0 +1,32 @@
+
+var express = require('express')
+ , ejs = require('ejs')
+ , config = require('./config')
+ , todo = require('./controllers/todo');
+
+var app = express.createServer();
+app.use(express.static(__dirname + '/public', {maxAge: 3600000 * 24 * 30}));
+app.use(express.cookieParser());
+app.use(express.bodyParser());
+
+app.helpers({
+ config: config
+});
+
+/**
+ * Views settings
+ */
+app.set("view engine", "html");
+app.set("views", __dirname + '/views');
+app.register("html", ejs);
+
+app.get('/', todo.index);
+app.post('/todo/new', todo.new);
+app.get('/todo/:id', todo.view);
+app.get('/todo/:id/edit', todo.edit);
+app.post('/todo/:id/edit', todo.save);
+app.get('/todo/:id/delete', todo.delete);
+app.get('/todo/:id/finish', todo.finish);
+
+app.listen(config.port);
+console.log('Server start http://localhost:' + config.port);
@@ -0,0 +1,31 @@
+/*
+MySQL Data Transfer
+Source Host: localhost
+Source Database: todo
+Target Host: localhost
+Target Database: todo
+Date: 2011/6/25 21:57:10
+*/
+
+SET FOREIGN_KEY_CHECKS=0;
+-- ----------------------------
+-- Table structure for todo
+-- ----------------------------
+DROP TABLE IF EXISTS `todo`;
+CREATE TABLE `todo` (
+ `id` int(11) NOT NULL auto_increment,
+ `title` varchar(300) default NULL,
+ `finished` int(11) default '0',
+ `post_date` datetime default NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records
+-- ----------------------------
+INSERT INTO `todo` VALUES ('4', '增加摘要功能,除标题外还可以写描述', '0', '2011-06-03 06:08:10');
+INSERT INTO `todo` VALUES ('5', '增加分类,不同的事项划分到不同的分类中去', '0', '2011-06-03 06:08:37');
+INSERT INTO `todo` VALUES ('6', '增加协作功能,增加用户功能,可以指定转给某个人,且用邮件通知他', '0', '2011-06-03 06:09:18');
+INSERT INTO `todo` VALUES ('7', '不直接删除,改为完成,标示为该条事项已完成显示在最下方', '0', '2011-06-03 06:09:41');
+INSERT INTO `todo` VALUES ('8', '这是一条测试', '1', '2011-06-04 23:00:47');
+INSERT INTO `todo` VALUES ('9', '这是一条测试2', '1', '2011-06-04 23:01:31');
@@ -0,0 +1,13 @@
+<style type="text/css">
+ .content { background:#EEE; font: 14px/1.5 Trebuchet MS, Arial,Helvetica, sans-serif; text-align:center; margin-top:50px; }
+ .content { background:#FFF; width:400px; border:3px solid #CCC; border-radius:5px; padding:10px 20px; margin:auto; text-align:left;}
+</style>
+ <div class="content">
+ <h1 style="font-weight:400;">
+ <%= message %>
+ </h1>
+ <p>
+ <a href="javascript: history.back();">返回</a> |
+ <a href="/">首页</a>
+ </p>
+ </div>
@@ -0,0 +1,37 @@
+
+ <div class="box">
+
+ <div class="box todos">
+ <h2 class="box">待办事项</h2>
+ <ul>
+ <% for(var i = 0, len = todos.length; i < len; i++) {
+ var todo = todos[i];
+ var status = todo.finished == 1 ? 'class="finished"' : '';
+ %>
+ <li <%- status %>>
+ <% if(todo.finished == 0) { %>
+ <%= todo.title %>
+ &nbsp;
+ <a href="/todo/<%- todo.id%>/finish?status=yes">完成</a>,
+ <% } else { %>
+ <del><%= todo.title %></del>
+ &nbsp;
+ <a href="/todo/<%- todo.id%>/finish?status=no">恢复</a>,
+ <% } %>
+ <a href="/todo/<%- todo.id%>/edit">修改</a>,
+ <a href="/todo/<%- todo.id%>/delete" onclick="return confirm('删除以后不能恢复的,确定?')">删除</a>
+ </li>
+ <% } %>
+ </ul>
+ </div>
+
+ <div class="box post">
+ <h2>新增</h2>
+ <form action="/todo/new" method="post" id="post_new">
+ <p><input type="text" name="title" class="long_txt" /></p>
+ <p><input type="submit" class="submit" value="添加" /></p>
+ </form>
+ </div>
+
+ </div>
+
@@ -0,0 +1,32 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title><%= config.site_name %></title>
+<link href="/styles/reset.css" rel="stylesheet" type="text/css" />
+<link href="/styles/index/style.css" rel="stylesheet" type="text/css" />
+</head>
+
+<body>
+<div class="page">
+ <div class="header box">
+ <h1><a href="/"><%= config.site_name %></a></h1>
+ </div>
+
+ <div class="main box">
+
+ <%- body %>
+
+ </div>
+
+ <div class="foot">
+ Copyright&copy; <%= config.site_name %>
+ <a href="http://cnodejs.org/">CNode</a>
+ <br />
+ 修改自 <a href="http://simple-is-better.com/news/309">simple-todo: 一个简易的 todo 程序 - web.py 中文教程</a>
+ from <a href="http://simple-is-better.com/">Python.cn(news, jobs)</a>
+ </div>
+
+</div>
+</body>
+</html>
@@ -0,0 +1,11 @@
+
+ <div class="box post">
+ <h2>修改</h2>
+ <form action="?" method="post">
+ <p><input type="text" name="title" class="long_txt" value="<%-todo.title%>" /></p>
+ <p>
+ <input type="submit" class="submit" value="提交" />
+ </p>
+ </form>
+ </div>
+

0 comments on commit af82ab8

Please sign in to comment.