@@ -14,25 +14,82 @@ router.use(function(req, res, next) {
1414
1515router . get ( '/' , function ( req , res )
1616{
17- req . checkQuery ( 'status' ) . isStatus ( ) ;
17+ req . checkQuery ( 'status' ) . optional ( ) . isStatus ( ) ;
1818 req . checkQuery ( 'token' ) . optional ( ) . isToken ( ) ;
19- if ( req . validationErrors ( ) )
19+
20+ if ( req . validationErrors ( ) )
2021 {
2122 return response . call ( res , "Invalid Arguments" ) ;
2223 }
23- group = Group . getByToken ( req . query . token ) ;
24- Game
25- . getAll ( response . bind ( res ) )
26- . select ( '-thumbnail' )
27- . populate ( {
24+
25+ var status = req . query . status || "prod" ;
26+ var token = req . query . token ;
27+
28+ var populateOptions = {
2829 path : 'releases' ,
29- select : 'status updated' ,
30- match : { 'status' : { $in : [ req . query . status ] } } ,
30+ select : 'status updated commitId ' ,
31+ match : { 'status' : { $in : [ status ] } } ,
3132 options : {
3233 sort : { updated : - 1 } ,
3334 limit : 1
3435 }
35- } ) ;
36+ } ;
37+
38+ async . waterfall ( [
39+ function ( done )
40+ {
41+ // Require token
42+ if ( status == "prod" )
43+ {
44+ return Game . getAll ( done )
45+ . select ( '-thumbnail' )
46+ . populate ( populateOptions ) ;
47+ }
48+ else if ( ! token )
49+ {
50+ return done ( "No token" ) ;
51+ }
52+ Group . getByToken ( token , function ( err , group )
53+ {
54+ if ( err )
55+ {
56+ return done ( err ) ;
57+ }
58+ else if ( ! group )
59+ {
60+ return done ( "Invalid token" ) ;
61+ }
62+ Game . getGamesByGroup ( group , done )
63+ . select ( '-thumbnail' )
64+ . populate ( populateOptions ) ;
65+ } ) ;
66+ } ] ,
67+ function ( err , games )
68+ {
69+ if ( err )
70+ {
71+ return response . call ( res , err ) ;
72+ }
73+ else if ( games . length === 0 )
74+ {
75+ return response . call ( res , "No games" ) ;
76+ }
77+
78+ // Update the url
79+ games . forEach ( function ( game )
80+ {
81+ game . releases . forEach ( function ( release )
82+ {
83+ release . url = game . location + '/' +
84+ release . commitId + '/' +
85+ ( req . query . debug == "true" ? 'debug' : 'release' ) +
86+ ( req . query . archive == "true" ? '.zip' : '/index.html' ) ;
87+ } ) ;
88+ } ) ;
89+
90+ response . call ( res , games ) ;
91+ }
92+ ) ;
3693} ) ;
3794
3895module . exports = router ;
0 commit comments