11
22var 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+
69var isAuth = function ( req , res , next ) {
710 if ( req . isAuthenticated ( ) ) next ( ) ;
811 else res . redirect ( '/' ) ;
@@ -27,9 +30,7 @@ app.get('/', function(req, res){
2730app . 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-
6165app . 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
125139app . 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
0 commit comments