Skip to content

Commit 9351e9b

Browse files
added search engine
1 parent b872b2d commit 9351e9b

4 files changed

Lines changed: 48 additions & 11 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"passport": "0.1.x",
1212
"passport-twitter": "0.1.x",
1313
"redis": "0.7.x",
14-
"connect-redis": "1.4.x"
14+
"connect-redis": "1.4.x",
15+
"reds": "0.1.x"
1516
}
1617
}

public/styles/main.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
.projects { margin-top: 20px; }
1212

13-
.project{}
14-
13+
.form-search { margin-top: -15px }
14+
.form-search button { margin-left: 20px; }

routes/index.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
var app = module.parent.exports.app
33
, passport = require('passport')
4+
, reds = require('reds')
45
, client = module.parent.exports.client;
56

7+
var search = reds.createSearch('hhba:search');
8+
69
var isAuth = function(req, res, next){
710
if(req.isAuthenticated()) next();
811
else res.redirect('/');
@@ -27,9 +30,7 @@ app.get('/', function(req, res){
2730
app.get('/dashboard', isAuth, function(req, res){
2831
client.keys('hhba:projects:*', function(err, keys){
2932
client.mget(keys, function(err, projects){
30-
3133
projects = projects || [];
32-
3334
projects = projects.map(function(project){
3435
return JSON.parse(project);
3536
});
@@ -50,14 +51,17 @@ app.get('/projects/join/:id', function(req, res){
5051
} else {
5152
project.contributors.push(req.user.username);
5253
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
54+
search.remove(req.params.id, function(){
55+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
56+
, req.params.id);
57+
});
5358
res.end('1');
5459
});
5560
}
5661
});
5762
}
5863
});
5964

60-
6165
app.get('/projects/leave/:id', function(req, res){
6266
if(!req.isAuthenticated()){
6367
res.end('0');
@@ -68,6 +72,10 @@ app.get('/projects/leave/:id', function(req, res){
6872
project.contributors.splice(project.contributors.indexOf(req.user.username), 1);
6973
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
7074
res.end('2');
75+
search.remove(req.params.id, function(){
76+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
77+
, req.params.id);
78+
});
7179
});
7280
} else {
7381
res.end('2');
@@ -90,11 +98,13 @@ app.post('/projects/new', isAuth, function(req, res){
9098
};
9199

92100
client.set('hhba:projects:' + hash, JSON.stringify(project), function(){
93-
res.redirect('back');
101+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
102+
, hash);
103+
res.redirect('/dashboard');
94104
});
95105

96106
} else {
97-
res.redirect('back');
107+
res.redirect('/dashboard');
98108
}
99109
});
100110

@@ -114,7 +124,11 @@ app.post('/projects/edit/:id', isAuth, isOwner, function(req, res){
114124
project.description = req.body.description;
115125

116126
client.set('hhba:projects:' + req.params.id, JSON.stringify(project), function(){
117-
res.redirect('back');
127+
search.remove(req.params.id, function(){
128+
search.index(project.title + ' ' + project.description + ' ' + project.contributors.join(' ')
129+
, req.params.id);
130+
});
131+
res.redirect('/dashboard');
118132
});
119133
});
120134
} else {
@@ -124,7 +138,24 @@ app.post('/projects/edit/:id', isAuth, isOwner, function(req, res){
124138

125139
app.get('/projects/remove/:id', isAuth, isOwner, function(req, res){
126140
client.del('hhba:projects:' + req.params.id, function(err){
127-
res.redirect('back');
141+
res.redirect('/dashboard');
142+
search.remove(req.params.id);
143+
});
144+
});
145+
146+
app.get('/search', isAuth, function(req, res){
147+
search
148+
.query(req.query.q)
149+
.end(function(err, ids){
150+
ids = ids.map(function(id){ return 'hhba:projects:' + id; });
151+
client.mget(ids, function(err, projects){
152+
console.log(projects);
153+
projects = projects || [];
154+
projects = projects.map(function(project){
155+
return JSON.parse(project);
156+
});
157+
res.render('dashboard', {projects: projects, user: req.user});
158+
});
128159
});
129160
});
130161

views/dashboard.jade

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ block content
66
.span4
77
h1 Projects
88

9-
.span2.offset6
9+
.span4
10+
form.well.form-search(action="/search", method="get")
11+
input.input-medium.search-query(type="text", name="q")
12+
button.btn(type="submit") Search
13+
14+
.span2.offset2
1015
a.newProject.btn.btn-primary(href="#newProject", data-toggle="modal") New project
1116

1217
.row.projects

0 commit comments

Comments
 (0)