Permalink
Browse files

Implemented basic search from the home page, if matches are found the…

…y are listed, if not then the user is redirect to a "create" page where they can ask what the term means.
  • Loading branch information...
1 parent 45845ad commit bfd3d73cea8fa8d0261bbb05269622a710ba95b9 @csainty csainty committed Apr 5, 2012
Showing with 67 additions and 6 deletions.
  1. +3 −0 app.js
  2. +1 −1 data/config.js
  3. +3 −0 routes/create.js
  4. +17 −0 routes/search.js
  5. +19 −0 views/create.jade
  6. +5 −4 views/index.jade
  7. +3 −1 views/layout.jade
  8. +16 −0 views/search.jade
View
3 app.js
@@ -53,6 +53,9 @@ app.configure('production', function () {
app.get('/', require('./routes/index.js'));
app.get('/about', require('./routes/about.js'));
app.get('/ask', require('./routes/ask.js'));
+app.get('/create', require('./routes/create.js'));
+
+app.post('/search', require('./routes/search.js'));
app.listen(port, address);
View
@@ -19,7 +19,7 @@ Answer = new Schema({
Wat = mongoose.model('Wat', new Schema({
_user: { type: ObjectId, ref: 'User', required: true }
- , phrase: { type: String, trim: true, required: true }
+ , phrase: { type: String, trim: true, required: true, index: true }
, posted: { type: Date, default: Date.now }
, region: String
, answers: [Answer]
View
@@ -0,0 +1,3 @@
+module.exports = function (req, res) {
+ res.render('create', { title: 'say wat?', phrase: req.query.phrase || '' });
+};
View
@@ -0,0 +1,17 @@
+var mongoose = require('mongoose')
+ , url = require('url')
+ , Wat = mongoose.model('Wat')
+ ;
+
+module.exports = function (req, res) {
+ if (!req.body.q || req.body.q.length === 0) { return res.redirect('/'); }
+
+ Wat
+ .find({ phrase: req.body.q })
+ .populate('_user')
+ .run(function (err, results) {
+ if (err) { return res.redirect('/error'); }
+ if (results.length === 0) { return res.redirect(url.format({ pathname: '/create', query: { phrase: req.body.q } })); }
+ res.render('search', { title: 'say wat?', results: results, query: req.body.q });
+ });
+};
View
@@ -0,0 +1,19 @@
+div.row-fluid
+ div.span12
+ h1 say wat?
+ h2 want to know what #{phrase} means?
+
+div.row-fluid
+ div.span1 Phrase
+ div.span3
+ input(type='text', name='phrase', value=phrase)
+
+div.row-fluid
+ div.span1 Region
+ div.span3
+ input(type='text', name='origin')
+
+div.row-fluid
+ div.span1  
+ div.span3
+ input.btn(type='submit', value='ask!')
View
@@ -5,7 +5,8 @@ div.row-fluid
div.row-fluid
div.search
- div.span8
- input(type='text', name='q')
- div.span3
- input.btn(type='submit', value='help!')
+ form(method='POST', action='/search')
+ div.span8
+ input(type='text', name='q')
+ div.span3
+ input.btn(type='submit', value='help!')
View
@@ -16,6 +16,8 @@ html
div.container
ul.nav
li
- a(href="/about") About
+ a(href='/') Home
+ li
+ a(href='/about') About
script(type='text/javascript', src='/scripts/jquery-1.7.2.min.js')
script(type='text/javascript', src='/scripts/bootstrap.min.js')
View
@@ -0,0 +1,16 @@
+div.row-fluid
+ div.span12
+ h1 say wat?
+ h2 #{query}?
+
+each wat in results
+ div.row-fluid
+ div.span2
+ img(src=wat._user.gravatarUrl)
+ p!= wat._user.displayName
+ div.span8
+ p Phrase:
+ a(href='') #{wat.phrase}
+ p Region:
+ #{wat.region}
+

0 comments on commit bfd3d73

Please sign in to comment.