Permalink
Browse files

merge

  • Loading branch information...
1 parent d317f5b commit c1850776af1743adbcd6f7f652aef85806506683 @pingan1927 pingan1927 committed Jul 25, 2011
Showing with 318 additions and 239 deletions.
  1. +146 −140 app.js
  2. +0 −24 modules/Share.js
  3. +23 −0 modules/average.js
  4. +1 −0 package.json
  5. +6 −0 public/stylesheets/rate2.css
  6. +58 −28 public/stylesheets/style.css
  7. +9 −0 runserver.sh
  8. +3 −1 views/create-ok.jade
  9. +11 −7 views/create.jade
  10. +5 −4 views/layout.jade
  11. +55 −35 views/list.jade
  12. +1 −0 views/rate2.jade
View
286 app.js
@@ -5,38 +5,21 @@
var express = require('express');
var mongod = require('mongodb');
var BSON = mongod.BSONPure;
+var apptitle = ' - 火鸟打分系统';
+console.log("r", require.resolve('./modules/average'));
+var average = require('./modules/average').average;
var app = module.exports = express.createServer(); // Configuration
var db;
-function avi (arr){
- var sum1 = 0,sum2=0,sum3=0,sum4=0,sum=[];
- var len = arr.length;
- arr.forEach(function(v){
- if(v['rate1'] && v['rate2'] && v['rate3'] && v['rate4']){
- sum1 += v['rate1'];
- sum2 += v['rate2'];
- sum3 += v['rate3'];
- sum4 += v['rate4'];
- }else{
- len --;
- }
+function ratedb(callback){
+ db.open(function(err,db){
+ db.collection('rate', callback);
});
- //console.log(sum);
- //console.log(sum2);
- //console.log(sum3);
- //console.log(sum4);
-// sum = sum1+sum2+sum3+sum4;
- if(len === 0 ) return 0;
- sum.push(Math.round(sum1 / len * 100)/100);
- sum.push(Math.round(sum2 / len * 100)/100);
- sum.push(Math.round(sum3 / len * 100)/100);
- sum.push(Math.round(sum4 / len * 100)/100);
- return sum;
-// return Math.round(sum1 / len * 100)/100;
}
+
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
@@ -47,7 +30,8 @@ app.configure(function(){
});
app.configure('development', function(){
- console.log("development");
+ console.log('development');
+ apptitle += '(dev)'
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = new mongod.Db(
'firebird_dev',
@@ -59,8 +43,9 @@ app.configure('development', function(){
});
app.configure('production', function(){
- console.log("production");
+ console.log('production');
app.use(express.errorHandler());
+ app.set("view cache", true);
db = new mongod.Db(
'firebird',
new mongod.Server('127.0.0.1', 27017),
@@ -74,99 +59,159 @@ app.configure('production', function(){
app.get('/', function(req, res){
res.render('index', {
- title: 'Express'
+ title: apptitle
});
});
app.get('/create',function(req,res){
- res.render('create', {
- title: 'Create a Cate'
- });
-});
-
-app.get('/create-ok/:id',function(req,res){
- res.render('create-ok', {
- title: 'Create a Cate',
- id : req.params.id
- });
+ res.render('create', {
+ title: 'Create a Cate' + apptitle,
+ error : [],
+ ratetitle : '',
+ rateauthor : '',
+ rateother : ''
+ });
});
app.post('/create',function(req,res){
var doc = {};
-
- db.open(function(err,db){
- doc.author = encodeURIComponent(req.param("author"));
- doc.title = encodeURIComponent(req.param("title"));
- doc.other = encodeURIComponent(req.param("other"));
- doc.rates = [];
- doc.ts_save = new Date().getTime();
-
- db.collection("rate", function(err,collection){
- collection.insert(doc,function(err, doc){
- db.close();
- res.redirect("/create-ok/"+doc[0]._id);
- });
+ var author = req.param('author').trim(),
+ title = req.param('title').trim(),
+ other = req.param('other').trim(),
+ error = [];
+ if(!author){
+ error.push("请输入 分享者");
+ }
+ if(!title){
+ error.push("请输入 分享标题");
+ }
+ if(error.length > 0){
+ res.render('create', {
+ title: 'Create a Cate' + apptitle,
+ error : error,
+ ratetitle : title,
+ rateauthor : author,
+ rateother : other
});
+ return;
+ }
+ doc.author = encodeURIComponent(author);
+ doc.title = encodeURIComponent(title);
+ doc.other = encodeURIComponent(other);
+ doc.rates = [];
+ doc.ts_save = new Date().getTime();
+ ratedb(function(err,collection){
+ collection.insert(doc,function(err, doc){
+ db.close();
+ res.redirect('/create-ok/'+doc[0]._id);
+ });
+ });
+});
+
+app.get('/create-ok/:id',function(req,res){
+ res.render('create-ok', {
+ title: 'Create a Cate' + apptitle,
+ id : req.params.id
});
});
+
//show a rate
app.get('/show/:rid', function(req, res){
var rateid = req.params.rid;
- var query = {"_id" : BSON.ObjectID(rateid)};
+ var query = {'_id' : BSON.ObjectID(rateid)};
var result = {};
- db.open(function(err,db){
- db.collection("rate", function(err,collection){
- console.log("querying : " + query);
- collection.findOne(query, function(err,doc){
- var json, date;
- if(doc) {
- date = new Date(doc.ts_save.toNumber());
- doc.rates.forEach(function(item,idx){
- item.tdate = new Date(item.ts.toNumber());
- })
- title = decodeURIComponent(doc.title);
+ ratedb(function(err,collection){
+ console.log('querying : ' + query._id);
+ collection.findOne(query, function(err,doc){
+ var json, date;
+ if(doc) {
+ date = new Date(doc.ts_save.toNumber());
+ doc.rates.forEach(function(item,idx){
+ item.tdate = new Date(item.ts.toNumber());
+ })
+ title = decodeURIComponent(doc.title);
- res.render('showrate', {
- title : title + " - rate",
- r_dateSave : date,
- r_title : title,
- r_author : decodeURIComponent(doc.author),
- rates : doc.rates,
- _id : doc._id
- });
- }else{
- res.redirect("/list");
- }
- db.close();
- });
+ res.render('showrate', {
+ title : title + apptitle,
+ r_dateSave : date,
+ r_title : title,
+ r_author : decodeURIComponent(doc.author),
+ rates : doc.rates,
+ _id : doc._id
+ });
+ }else{
+ res.redirect('/list');
+ }
+ db.close();
});
});
});
//** jsonp
app.get('/getrate/:rid', function(req, res){
var rid = req.params.rid;
- var query = {"_id" : BSON.ObjectID(rid)};
+ var query = {'_id' : BSON.ObjectID(rid)};
var result = {};
- db.open(function(err,db){
- db.collection("rate", function(err,collection){
- collection.findOne(query, function(err,doc){
- var json;
- if(doc) {
- json = JSON.stringify(doc),
- res.header('Content-Type', 'data/plain');
- res.header('Content-Disposition','attachment; filename='+decodeURIComponent(doc.title)+'.json');
- res.send(json);
- }
+ ratedb(function(err,collection){
+ collection.findOne(query, function(err,doc){
+ var json;
+ if(doc) {
+ json = JSON.stringify(doc),
+ res.header('Content-Type', 'data/plain');
+ res.header('Content-Disposition','attachment; filename='+decodeURIComponent(doc.title)+'.json');
+ res.send(json);
+ }
- db.close();
- });
+ db.close();
});
});
});
+app.get('/list/:query?',function(req,res){
+ var query = {},
+ author = req.param("author", ""),
+ title = req.param("title", "");
+ if(author.length > 0){
+ query.author = encodeURIComponent(author);
+ }
+ if(title.length > 0){
+ query.title = encodeURIComponent(title);
+ }
+ console.log("list:", query);
+ ratedb(function(err,collection){
+ collection.find(
+ query,
+ { 'sort' : [['ts_save','desc']] },
+ function(err,cursor){
+ var result = [];
+ cursor.each(function(err,doc){
+ if(doc !== null ){
+ doc.title = decodeURIComponent(doc.title);
+ doc.date = new Date(doc.ts_save.toNumber());
+ doc.author = decodeURIComponent(doc.author);
+ result.push(doc);
+
+ if(doc.rates && doc.rates.length){
+ doc.score = average(doc.rates);
+ }else{
+ doc.score = 0;
+ }
+
+ }else{
+ res.render('list', {
+ result : result,
+ query : query,
+ title : '所有分享' + apptitle
+ });
+ db.close();
+ }
+ });
+ }
+ );
+ });
+});
app.post("/ratedo", function(req, res){
@@ -190,67 +235,28 @@ app.post("/ratedo", function(req, res){
ts : new Date().getTime()
};
- db.open(function(err,db){
- db.collection("rate", function(err, collection){
- collection.update({
- _id : BSON.ObjectID(id)
- },{
- $push : {
- rates : ratedata
- }
- }, function(err, data){
- db.close();
- res.redirect("/show/"+id);
- });
- });
- });
-});
-app.get('/list',function(req,res){
- var query = {},
- result = [];
-
- db.open(function(err,db){
- db.collection("rate", function(err,collection){
- collection.find(
- query,
- {
- 'sort' : [["ts_save","desc"]]
- },
- function(err,cursor){
- cursor.each(function(err,doc){
- console.log(11111);
- if(doc !== null ){
- doc.title = decodeURIComponent(doc.title);
- doc.date = new Date(doc.ts_save.toNumber());
- doc.author = decodeURIComponent(doc.author);
- result.push(doc);
-
- if(doc.rates && doc.rates.length){
- doc.score = avi(doc.rates);
- }else{
- doc.score = 0;
- }
-
- }else{
- res.render("list", {
- result : result,
- title : "评分列表"
- });
- db.close();
- }
- });
- });
+ ratedb(function(err, collection){
+ collection.update({
+ _id : BSON.ObjectID(id)
+ },{
+ $push : {
+ rates : ratedata
+ }
+ }, function(err, data){
+ db.close();
+ res.redirect("/show/"+id);
});
});
});
app.get('/rate/:rid',function(req,res){
res.render('rate2',{
- title:'rate',
+ title:'rate' + apptitle,
+ css : 'rate',
rid : req.params.rid
});
});
app.listen(3000);
-console.log("Express server listening on port %d", app.address().port);
+console.log('Express server listening on port %d', app.address().port);
View
@@ -1,24 +0,0 @@
-var Share = new Schema({
- rates : [Number],
- title : {type:String},
- name : {type:String},
- id : {type:String},
- ts_save : {type:Date}
-});
-Share.path("ts_save")
- .default(function(){
- return new Date();
- })
- .set(function(v){
- return v == "now"? new Date():v;
- });
-
-Share.path("title")
- .set(function(v){
- return encodeURIComponent(v);
- });
-Share.path("title")
- .set(function(v){
- return encodeURIComponent(v);
- });
-mongoose.model("Share", Share);
Oops, something went wrong.

0 comments on commit c185077

Please sign in to comment.