Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

rearranged admin routes, placing them in their own files in an admin …

…subdirectory
  • Loading branch information...
commit 306cc6a5d2a808e02310b4a128f1c7a08d3abaf3 1 parent 3ca1146
Lewis Barclay authored
6  root/www/app.js
@@ -30,7 +30,10 @@ app.configure(function() {
30 30
 });
31 31
 
32 32
 //routes
33  
-//site
  33
+require('./routes/admin/default')(app, arMongoose, viewDataProvider);
  34
+require('./routes/admin/artist')(app, arMongoose, viewDataProvider);
  35
+require('./routes/admin/artwork')(app, arMongoose, viewDataProvider);
  36
+
34 37
 require('./routes/default')(app, arMongoose, viewDataProvider);
35 38
 require('./routes/artist')(app, arMongoose, viewDataProvider);
36 39
 require('./routes/artwork')(app, arMongoose, viewDataProvider);
@@ -40,7 +43,6 @@ require('./routes/basket.js')(app, arMongoose, viewDataProvider);
40 43
 
41 44
 require('./routes/product.js')(app, arMongoose, viewDataProvider);
42 45
 
43  
-
44 46
 //
45 47
 app.listen(3000);
46 48
 console.log('Art Rebellion site started on port 3000');
148  root/www/routes/admin/artist.js
... ...
@@ -0,0 +1,148 @@
  1
+function artistAdminRoutes(app, mongoose, vdp) {
  2
+
  3
+  //read all artists
  4
+  app.get('/admin/artists', function(req, res) {
  5
+    var locals = {title : 'Art Rebellion Admin: List Artists', pageTitle: 'List Artists'}
  6
+            , Artist = mongoose.model('Artist');
  7
+
  8
+    //find all artists
  9
+    Artist.find({}, function(err, artists) {
  10
+      if (err) {
  11
+        res.send(err, 500);
  12
+      }
  13
+
  14
+      locals.artists = artists;
  15
+      vdp.getAdminViewData(thenRender, 'admin/artist-list.jade', locals, req, res);
  16
+    });
  17
+  });
  18
+
  19
+
  20
+  //show single artist form
  21
+  app.get('/admin/artists/new', function(req, res) {
  22
+
  23
+    //Get an Artist Model instance
  24
+    var Artist = mongoose.model('Artist')
  25
+            , newArtist = new Artist()
  26
+            , locals = {  title : 'Art Rebellion Admin: Add Artist',
  27
+      pageTitle: 'Add Artist',
  28
+      method:"POST",
  29
+      methodOverride:null,
  30
+      formAction : "/admin/artists/",
  31
+      artist: newArtist};
  32
+
  33
+    vdp.getAdminViewData(thenRender, 'admin/artist-form.jade', locals, req, res);
  34
+  });
  35
+
  36
+
  37
+  //read single artist into form
  38
+  app.get('/admin/artists/:artistSlug', function(req, res) {
  39
+
  40
+    //Get an Artist Model instance
  41
+    var Artist = mongoose.model('Artist')
  42
+            , locals = {  title : 'Art Rebellion: [Artist Name]',
  43
+      pageTitle: '[Artist Name]',
  44
+      method:"POST",
  45
+      methodOverride:"PUT"};
  46
+
  47
+    Artist.findOne({slug:req.params.artistSlug}, function(err, artist) {
  48
+
  49
+      locals.formAction = "/admin/artists/" + artist.slug;
  50
+      locals.artist = artist;
  51
+
  52
+      vdp.getAdminViewData(thenRender, 'admin/artist-form.jade', locals, req, res);
  53
+    });
  54
+  });
  55
+
  56
+
  57
+  //create artist
  58
+  app.post('/admin/artists/', function(req, res) {
  59
+    console.log(req.body);
  60
+
  61
+    //Get an Artist Model instance
  62
+    var Artist = mongoose.model('Artist');
  63
+    var newArtist = new Artist(req.body.artist);
  64
+
  65
+    newArtist.save(function(err) {
  66
+      if (err) {
  67
+        res.send(err, 500);
  68
+        return;
  69
+      }
  70
+
  71
+      res.redirect('/admin/artists');
  72
+    });
  73
+  });
  74
+
  75
+
  76
+  //update single artist
  77
+  app.put('/admin/artists/:artistSlug', function(req, res) {
  78
+
  79
+    var Artist = mongoose.model('Artist');
  80
+    var Artwork = mongoose.model('Artwork');
  81
+
  82
+    req.body.artist.slug = mongoose.utilities.getSlug(req.body.artist.name);
  83
+
  84
+    //HACK
  85
+    //need to explicitly set the value to false?
  86
+    req.body.artist.featured = (req.body.artist.featured == "on");
  87
+
  88
+    Artist.update({slug:req.params.artistSlug}, req.body.artist, {multi:false, upsert:false}, function(err) {
  89
+
  90
+      if (err) {
  91
+        res.send(err, 500);
  92
+      }
  93
+
  94
+      //update any artwork that is associated to the old slug:
  95
+      Artwork.update({artist : req.params.artistSlug}, {artist : req.body.artist.slug}, {multi:true, upsert:false}, function(err) {
  96
+
  97
+        if (err) {
  98
+          res.send(err, 500);
  99
+        }
  100
+
  101
+        res.redirect('/admin/artists');
  102
+      });
  103
+    });
  104
+  });
  105
+
  106
+
  107
+  /* --- --- --- partials / ajax handlers --- --- --- */
  108
+  //delete single artist
  109
+  app.del('/admin/artists/:artistSlug', function(req, res) {
  110
+
  111
+    var Artist = mongoose.model('Artist');
  112
+    var Artwork = mongoose.model('Artwork');
  113
+    var result = {result : 'success'};
  114
+
  115
+    Artist.findOne({slug : artistSlug}, function(err, artist) {
  116
+      if (err) {
  117
+        //throw err;
  118
+        result.err = err;
  119
+        result.result = 'error';
  120
+        res.send(result);
  121
+      }
  122
+
  123
+      artist.remove();
  124
+
  125
+      Artwork.update({artist : artist.slug}, {artist : null}, function(updateError) {
  126
+
  127
+        if (updateError) {
  128
+          //throw err;
  129
+          result.err = err;
  130
+          result.result = 'error';
  131
+          res.send(result);
  132
+          return;
  133
+        }
  134
+
  135
+        res.send(result);
  136
+      });
  137
+    });
  138
+  });
  139
+
  140
+}
  141
+
  142
+module.exports = artistAdminRoutes;
  143
+
  144
+/* --- --- --- private helper methods --- --- --- */
  145
+
  146
+function thenRender(template, model, res) {
  147
+  res.render(template, model);
  148
+}
287  root/www/routes/admin/artwork.js
... ...
@@ -0,0 +1,287 @@
  1
+var im = require('imagemagick'),
  2
+        ImageTools = require('../../models/ImageTools.js');
  3
+
  4
+function artworkAdminRoutes(app, mongoose, vdp) {
  5
+  /* ---Admin Routes --- --- */
  6
+
  7
+  //upload artwork image form
  8
+  app.get('/admin/artwork/new.:format?', function(req, res) {
  9
+    var locals = {title : 'Art Rebellion: Add artwork images', pageTitle: 'Add artwork images'};
  10
+
  11
+    vdp.getAdminViewData(thenRender, 'admin/artwork-image-form.jade', locals, req, res);
  12
+  });
  13
+
  14
+
  15
+  //upload artwork handler
  16
+  app.post('/admin/artwork.:format?', function(req, res) {
  17
+
  18
+    var counter = 1;
  19
+
  20
+    if (req.files.artImages[0] && req.files.artImages[0].type) {
  21
+      for (var i = 0; i < req.files.artImages.length; i++) {
  22
+
  23
+        //TODO: sort our async issues 
  24
+        createArtwork(res, mongoose, req.files.artImages[i], function() {
  25
+          if (counter == req.files.artImages.length) {
  26
+            res.redirect('/admin/artwork');
  27
+          }
  28
+
  29
+          counter++;
  30
+        }, req.body.artist);
  31
+      }
  32
+    }
  33
+    else {
  34
+      createArtwork(res, mongoose, req.files.artImages, function() {
  35
+        res.redirect('/admin/artwork');
  36
+      }, req.body.artist);
  37
+    }
  38
+  });
  39
+
  40
+
  41
+  //list all artwork
  42
+  app.get('/admin/artwork.:format?', function(req, res) {
  43
+
  44
+    //Get an Artist Model instance
  45
+    var Artwork = mongoose.model('Artwork')
  46
+            , pageTitle = 'Art Rebellion : Artwork';
  47
+
  48
+    var locals = {
  49
+      title : pageTitle ,
  50
+      pageTitle : pageTitle ,
  51
+      scripts : ['/js/admin/artwork-list.js']
  52
+    };
  53
+
  54
+    //find all artists
  55
+    Artwork.find({}, function(err, artworks) {
  56
+      if (err) {
  57
+        res.send(err, 500);
  58
+        return;
  59
+      }
  60
+
  61
+      locals.artworks = artworks;
  62
+
  63
+      vdp.getAdminViewData(thenRender, 'admin/artwork-list.jade', locals, req, res);
  64
+    });
  65
+  });
  66
+
  67
+
  68
+  //update artwork form
  69
+  app.get('/admin/artwork/:artworkSlug', function(req, res) {
  70
+    var Artwork = mongoose.model('Artwork')
  71
+            , pageTitle
  72
+            , locals = {  method:"POST",
  73
+      methodOverride:"PUT",
  74
+      scripts : ['/js/admin/artwork-details-form.js']};
  75
+
  76
+    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
  77
+
  78
+      locals.visitor = req.session.visitor;
  79
+
  80
+      locals.artwork = artwork;
  81
+      locals.formAction = "/admin/artwork/" + artwork.slug;
  82
+      locals.title = "Art Rebellion : Edit Artwork : " + artwork.title;
  83
+      locals.pageTitle = locals.title;
  84
+
  85
+      vdp.getAdminViewData(thenRender, 'admin/artwork-details-form.jade', locals, req, res);
  86
+
  87
+    });
  88
+
  89
+  });
  90
+
  91
+
  92
+  //update artwork handler
  93
+  app.put('/admin/artwork/:artworkSlug', function(req, res) {
  94
+
  95
+    var Artwork = mongoose.model('Artwork'), pageTitle;
  96
+
  97
+    //create a new slug if needed.
  98
+    req.body.artwork.slug = mongoose.utilities.getSlug(req.body.artwork.title);
  99
+    req.body.artwork.tag = req.body.artwork.tag.split(',');
  100
+    req.body.artwork.featured = (req.body.artwork.featured == "on");
  101
+
  102
+    //fix tags - trim etc
  103
+    for (var i = req.body.artwork.tag.length - 1; i >= 0; i--) {
  104
+      req.body.artwork.tag[i] = req.body.artwork.tag[i].trim().toLowerCase();
  105
+      //if(typeof req.body.artwork.tag[i] == 'string' && req.body.artwork.tag[i] === ''){
  106
+      if (!req.body.artwork.tag[i]) {
  107
+        req.body.artwork.tag.splice(i, 1);
  108
+      }
  109
+    }
  110
+
  111
+    //mongoose.utilities.getFormatHash
  112
+    //TODO: figure out a way to add this as a Mongoose plugin.
  113
+    for (var i = 0; i < req.body.artwork.format.length; i++) {
  114
+      req.body.artwork.format[i].hash = mongoose.utilities.getFormatHash(req.body.artwork.format[i]);
  115
+    }
  116
+
  117
+    Artwork.update({slug:req.params.artworkSlug}, req.body.artwork, {multi:false, upsert:false}, function(err) {
  118
+
  119
+      if (err) {
  120
+        res.send(err, 500);
  121
+        return;
  122
+      }
  123
+
  124
+      res.redirect('/admin/artwork');
  125
+    });
  126
+
  127
+  });
  128
+
  129
+
  130
+  /* --- --- --- partials / ajax handlers --- --- --- */
  131
+  app.del('/admin/artwork/:artworkSlug', function(req, res) {
  132
+
  133
+    var Artwork = mongoose.model('Artwork');
  134
+
  135
+    Artwork.remove({slug : req.params.artworkSlug}, function(err) {
  136
+      var result = {};
  137
+
  138
+      if (err) {
  139
+        result = {result: 'error', error:err};
  140
+      } else {
  141
+        result = {result: 'success'};
  142
+      }
  143
+      res.send(result);
  144
+      result = null;
  145
+    })
  146
+  });
  147
+
  148
+
  149
+  //add new format - partial ajax handler
  150
+  app.post('/admin/artwork/:artworkSlug/format/new.:format', function(req, res) {
  151
+    var Artwork = mongoose.model('Artwork'),
  152
+            Format = mongoose.model('Format');
  153
+
  154
+    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
  155
+      if (err) {
  156
+        res.send(err, 500);
  157
+        return;
  158
+      }
  159
+
  160
+      var format = new Format(
  161
+              {
  162
+                type :  "*unspecified*",
  163
+                detail : '*unspecified*',
  164
+                printsRun : 0,
  165
+                height : 0,
  166
+                width : 0,
  167
+                price : 999999.99,
  168
+                stock : 0
  169
+              });
  170
+
  171
+      artwork.format.push(format);
  172
+
  173
+      artwork.save(function(err) {
  174
+        if (err) {
  175
+          res.send(err, 500);
  176
+          return;
  177
+        }
  178
+
  179
+        if (req.params.format === 'html') {
  180
+          var partials = {layout:false, i : artwork.format.length - 1, format:format};
  181
+
  182
+          res.contentType('text/html');
  183
+          res.render('admin/artwork-format.jade', partials);
  184
+
  185
+          return;
  186
+        }
  187
+
  188
+        res.contentType('application/json');
  189
+        res.send(JSON.stringify(this));
  190
+      });
  191
+    });
  192
+
  193
+  });
  194
+
  195
+
  196
+  //delete format - partial ajax handler
  197
+  app.del('/admin/artwork/:artworkSlug/format/:formatId', function(req, res) {
  198
+
  199
+    var Artwork = mongoose.model('Artwork');
  200
+
  201
+    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
  202
+      if (err) {
  203
+        res.send(err, 500);
  204
+        return;
  205
+      }
  206
+
  207
+      var fmt = artwork.format.id(req.params.formatId);
  208
+
  209
+      if (!fmt) {
  210
+        res.send(JSON.stringify({result: 'error',
  211
+          error : 'could not find format with specified id' + req.params.formatId}));
  212
+
  213
+        return;
  214
+      }
  215
+
  216
+      fmt.remove();
  217
+
  218
+      artwork.save(function(err) {
  219
+        if (err) {
  220
+          res.send(err, 500);
  221
+          return;
  222
+        }
  223
+
  224
+        res.contentType('application/json');
  225
+        res.send(JSON.stringify({result: 'success'}));
  226
+      });
  227
+
  228
+    });
  229
+  });
  230
+
  231
+}
  232
+
  233
+
  234
+module.exports = artworkAdminRoutes;
  235
+
  236
+
  237
+
  238
+/* --- --- --- private helper methods --- --- --- */
  239
+
  240
+function thenRender(template, model, res) {
  241
+  res.render(template, model);
  242
+}
  243
+
  244
+
  245
+function createArtwork(res, mongoose, image, next, artistSlug) {
  246
+  var Artwork = mongoose.model('Artwork'),
  247
+          Format = mongoose.model('Format'),
  248
+          newArtwork, newTitle, newSlug;
  249
+
  250
+  ImageTools.createCopies(image, function(imageFiles) {
  251
+    newTitle = 'unpublished art : ' + new Date().getTime();
  252
+
  253
+    var newFormat = new Format(
  254
+            {
  255
+              type :  "*unspecified*",
  256
+              detail : '*unspecified*',
  257
+              printsRun : 0,
  258
+              height : 0,
  259
+              width : 0,
  260
+              price : 999999,
  261
+              stock : 0
  262
+            });
  263
+    //TODO: figure out a way to add this as a Mongoose plugin.
  264
+    newFormat.hash = mongoose.utilities.getFormatHash(newFormat);
  265
+
  266
+    newArtwork = new Artwork({
  267
+      title : newTitle,
  268
+      image : imageFiles,
  269
+      type : 'original',
  270
+      artist : artistSlug,
  271
+      description : 'A new piece of work',
  272
+      released : new Date(),
  273
+      format : []
  274
+    });
  275
+
  276
+    newArtwork.format.push(newFormat);
  277
+
  278
+    newArtwork.save(function(err) {
  279
+      if (err) {
  280
+        //throw err;
  281
+        res.send(err, 500);
  282
+      }
  283
+
  284
+      next();
  285
+    });
  286
+  })
  287
+}
22  root/www/routes/admin/default.js
... ...
@@ -0,0 +1,22 @@
  1
+
  2
+
  3
+function defaultAdminRoute(app, mongoose, vdp){
  4
+ 
  5
+  app.get('/admin', function(req, res){
  6
+    var locals = {title : 'Art Rebellion : Administration dashboard', pageTitle: 'Administration dashboard'};
  7
+    vdp.getAdminViewData(thenRender, 'admin/dashboard.jade', locals, req, res);
  8
+  });    
  9
+  
  10
+}
  11
+
  12
+
  13
+module.exports = defaultAdminRoute;
  14
+
  15
+
  16
+/* --- --- --- private helper methods --- --- --- */
  17
+
  18
+function thenRender(template, model, res){
  19
+  res.render(template, model);
  20
+}
  21
+
  22
+
163  root/www/routes/artist.js
@@ -22,167 +22,12 @@ module.exports = function(app, mongoose, vdp) {
22 22
   app.get('/artists/:artistSlug.:format?', function(req, res) {
23 23
     var model = {};
24 24
     model.artistSlug = req.params.artistSlug;
25  
-//            , 
26  
-//        Artist = mongoose.model('Artist');
27  
-    
28  
-    //TODO: COMPLETE FROM HERE 01/02/2012    
29  
-    ArtistViewModelProvider.buildModel(mongoose, model, function(updatedModel){
30  
-      vdp.getPublicViewData(thenRender, 'artist.jade', updatedModel, req, res);
31  
-    });
32  
-
33  
-//    Artist.findOne({slug:req.params.artistSlug}, function(err, artist) {
34  
-//
35  
-//      locals.title = 'Art Rebellion : ' + artist.name;
36  
-//      locals.pageTitle = locals.title;
37  
-//      locals.artists = artist;
38  
-//
39  
-//      if (err || !artist) {
40  
-//        res.send('not found', 404);
41  
-//      }
42  
-//      else {
43  
-//        vdp.getPublicViewData(thenRender, 'artist.jade', locals, req, res);
44  
-//      }
45  
-//    });
46  
-
47  
-  });
48  
-
49  
-  /* --- --- --- 
50  
-   ADMIN >>
51  
-   REST compliant interface
52  
-   */
53  
-
54  
-  //read all artists
55  
-  app.get('/admin/artists', function(req, res) {
56  
-    var locals = {title : 'Art Rebellion Admin: List Artists', pageTitle: 'List Artists'}
57  
-            , Artist = mongoose.model('Artist');
58  
-
59  
-    //find all artists
60  
-    Artist.find({}, function(err, artists) {
61  
-      if (err) {
62  
-        res.send(err, 500);
63  
-      }
64  
-
65  
-      locals.artists = artists;
66  
-      vdp.getAdminViewData(thenRender, 'admin/artist-list.jade', locals, req, res);
67  
-    });
68  
-  });
69  
-
70  
-  //show single artist form
71  
-  app.get('/admin/artists/new', function(req, res) {
72  
-
73  
-    //Get an Artist Model instance
74  
-    var Artist = mongoose.model('Artist')
75  
-            , newArtist = new Artist()
76  
-            , locals = {  title : 'Art Rebellion Admin: Add Artist',
77  
-      pageTitle: 'Add Artist',
78  
-      method:"POST",
79  
-      methodOverride:null,
80  
-      formAction : "/admin/artists/",
81  
-      artist: newArtist};
82  
-
83  
-    vdp.getAdminViewData(thenRender, 'admin/artist-form.jade', locals, req, res);
84  
-  });
85  
-
86  
-  //read single artist into form
87  
-  app.get('/admin/artists/:artistSlug', function(req, res) {
88  
-
89  
-    //Get an Artist Model instance
90  
-    var Artist = mongoose.model('Artist')
91  
-            , locals = {  title : 'Art Rebellion: [Artist Name]',
92  
-      pageTitle: '[Artist Name]',
93  
-      method:"POST",
94  
-      methodOverride:"PUT"};
95  
-
96  
-    Artist.findOne({slug:req.params.artistSlug}, function(err, artist) {
97  
-
98  
-      locals.formAction = "/admin/artists/" + artist.slug;
99  
-      locals.artist = artist;
100  
-
101  
-      vdp.getAdminViewData(thenRender, 'admin/artist-form.jade', locals, req, res);
102  
-    });
103  
-  });
104 25
 
105  
-  //create artist
106  
-  app.post('/admin/artists/', function(req, res) {
107  
-    console.log(req.body);
108  
-
109  
-    //Get an Artist Model instance
110  
-    var Artist = mongoose.model('Artist');
111  
-    var newArtist = new Artist(req.body.artist);
112  
-
113  
-    newArtist.save(function(err) {
114  
-      if (err) {
115  
-        res.send(err, 500);
116  
-        return;
117  
-      }
118  
-
119  
-      res.redirect('/admin/artists');
120  
-    });
121  
-  });
122  
-
123  
-  //update single artist
124  
-  app.put('/admin/artists/:artistSlug', function(req, res) {
125  
-
126  
-    var Artist = mongoose.model('Artist');
127  
-    var Artwork = mongoose.model('Artwork');
128  
-
129  
-    req.body.artist.slug = mongoose.utilities.getSlug(req.body.artist.name);
130  
-
131  
-    //HACK
132  
-    //need to explicitly set the value to false?
133  
-    req.body.artist.featured = (req.body.artist.featured == "on");
134  
-
135  
-    Artist.update({slug:req.params.artistSlug}, req.body.artist, {multi:false, upsert:false}, function(err) {
136  
-
137  
-      if (err) {
138  
-        res.send(err, 500);
139  
-      }
140  
-
141  
-      //update any artwork that is associated to the old slug:
142  
-      Artwork.update({artist : req.params.artistSlug}, {artist : req.body.artist.slug}, {multi:true, upsert:false}, function(err) {
143  
-
144  
-        if (err) {
145  
-          res.send(err, 500);
146  
-        }
147  
-
148  
-        res.redirect('/admin/artists');
149  
-      });
  26
+    ArtistViewModelProvider.buildModel(mongoose, model, function(updatedModel) {
  27
+      vdp.getPublicViewData(thenRender, 'artist.jade', updatedModel, req, res);
150 28
     });
151 29
   });
152 30
 
153  
-  
154  
-/* --- --- --- partials / ajax handlers --- --- --- */  
155  
-  //delete single artist
156  
-  app.del('/admin/artists/:artistSlug', function(req, res) {
157  
-    
158  
-    var Artist = mongoose.model('Artist');
159  
-    var Artwork = mongoose.model('Artwork');
160  
-    var result = {result : 'success'};
161  
-    
162  
-    Artist.findOne({slug : artistSlug}, function(err, artist){
163  
-      if(err){
164  
-        //throw err;
165  
-        result.err = err;
166  
-        result.result = 'error';
167  
-        res.send(result);
168  
-      }
169  
-
170  
-      artist.remove();
171  
-      
172  
-      Artwork.update({artist : artist.slug}, {artist : null}, function(updateError){
173  
-        
174  
-        if(updateError){
175  
-          //throw err;
176  
-          result.err = err;
177  
-          result.result = 'error';
178  
-          res.send(result);
179  
-          return;
180  
-        }
181  
-
182  
-        res.send(result);        
183  
-      });
184  
-    });
185  
-  });
186 31
 };
187 32
 
188 33
 
@@ -191,7 +36,3 @@ module.exports = function(app, mongoose, vdp) {
191 36
 function thenRender(template, model, res) {
192 37
   res.render(template, model);
193 38
 }
194  
-
195  
-function afterArtworkSlugs(){
196  
-  
197  
-}
281  root/www/routes/artwork.js
... ...
@@ -1,6 +1,4 @@
1  
-var im = require('imagemagick'),
2  
-        ImageTools = require('../models/ImageTools.js'),
3  
-        ArtworkViewModelProvider = require('../models/ArtworkViewModelProvider');
  1
+var ArtworkViewModelProvider = require('../models/ArtworkViewModelProvider');
4 2
 
5 3
 // http://nyteshade.posterous.com/posting-files-with-node-and-expressjs
6 4
 module.exports = function(app, mongoose, vdp) {
@@ -17,282 +15,5 @@ module.exports = function(app, mongoose, vdp) {
17 15
     
18 16
   });
19 17
 
20  
-
21  
-  /* ---Admin Routes --- --- */
22  
-
23  
-  //upload artwork image form
24  
-  app.get('/admin/artwork/new.:format?', function(req, res) {
25  
-    var locals = {title : 'Art Rebellion: Add artwork images', pageTitle: 'Add artwork images'};
26  
-
27  
-    vdp.getAdminViewData(thenRender, 'admin/artwork-image-form.jade', locals, req, res);
28  
-  });
29  
-
30  
-
31  
-  //upload artwork handler
32  
-  app.post('/admin/artwork.:format?', function(req, res) {
33  
-
34  
-    var counter = 1;
35  
-
36  
-    if (req.files.artImages[0] && req.files.artImages[0].type) {
37  
-      for (var i = 0; i < req.files.artImages.length; i++) {
38  
-
39  
-        //TODO: sort our async issues 
40  
-        createArtwork(res, mongoose, req.files.artImages[i], function() {
41  
-          if (counter == req.files.artImages.length) {
42  
-            res.redirect('/admin/artwork');
43  
-          }
44  
-
45  
-          counter++;
46  
-        }, req.body.artist);
47  
-      }
48  
-    }
49  
-    else {
50  
-      createArtwork(res, mongoose, req.files.artImages, function() {
51  
-        res.redirect('/admin/artwork');
52  
-      }, req.body.artist);
53  
-    }
54  
-  });
55  
-
56  
-
57  
-  //list all artwork
58  
-  app.get('/admin/artwork.:format?', function(req, res) {
59  
-
60  
-    //Get an Artist Model instance
61  
-    var Artwork = mongoose.model('Artwork')
62  
-            , pageTitle = 'Art Rebellion : Artwork';
63  
-
64  
-    var locals = {
65  
-      title : pageTitle ,
66  
-      pageTitle : pageTitle ,
67  
-      scripts : ['/js/admin/artwork-list.js']
68  
-    };
69  
-
70  
-    //find all artists
71  
-    Artwork.find({}, function(err, artworks) {
72  
-      if (err) {
73  
-        res.send(err, 500);
74  
-        return;
75  
-      }
76  
-
77  
-      locals.artworks = artworks;
78  
-
79  
-      vdp.getAdminViewData(thenRender, 'admin/artwork-list.jade', locals, req, res);
80  
-    });
81  
-  });
82  
-
83  
-
84  
-  //update artwork form
85  
-  app.get('/admin/artwork/:artworkSlug', function(req, res) {
86  
-    var Artwork = mongoose.model('Artwork')
87  
-            , pageTitle
88  
-            , locals = {  method:"POST",
89  
-      methodOverride:"PUT",
90  
-      scripts : ['/js/admin/artwork-details-form.js']};
91  
-
92  
-    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
93  
-
94  
-      locals.visitor = req.session.visitor;
95  
-
96  
-      locals.artwork = artwork;
97  
-      locals.formAction = "/admin/artwork/" + artwork.slug;
98  
-      locals.title = "Art Rebellion : Edit Artwork : " + artwork.title;
99  
-      locals.pageTitle = locals.title;
100  
-
101  
-      vdp.getAdminViewData(thenRender, 'admin/artwork-details-form.jade', locals, req, res);
102  
-
103  
-    });
104  
-
105  
-  });
106  
-
107  
-
108  
-  //update artwork handler
109  
-  app.put('/admin/artwork/:artworkSlug', function(req, res) {
110  
-
111  
-    var Artwork = mongoose.model('Artwork'), pageTitle;
112  
-
113  
-    //create a new slug if needed.
114  
-    req.body.artwork.slug = mongoose.utilities.getSlug(req.body.artwork.title);
115  
-    req.body.artwork.tag = req.body.artwork.tag.split(',');
116  
-    req.body.artwork.featured = (req.body.artwork.featured == "on");
117  
-    
118  
-    //fix tags - trim etc
119  
-    for(var i = req.body.artwork.tag.length -1; i >= 0 ; i--){
120  
-      req.body.artwork.tag[i] = req.body.artwork.tag[i].trim().toLowerCase();
121  
-      //if(typeof req.body.artwork.tag[i] == 'string' && req.body.artwork.tag[i] === ''){
122  
-      if(!req.body.artwork.tag[i]){
123  
-        req.body.artwork.tag.splice(i,1);
124  
-      }
125  
-    }
126  
-
127  
-    //mongoose.utilities.getFormatHash
128  
-    //TODO: figure out a way to add this as a Mongoose plugin.
129  
-    for (var i = 0; i < req.body.artwork.format.length; i++) {
130  
-      req.body.artwork.format[i].hash = mongoose.utilities.getFormatHash(req.body.artwork.format[i]);
131  
-    }
132  
-
133  
-    Artwork.update({slug:req.params.artworkSlug}, req.body.artwork, {multi:false, upsert:false}, function(err) {
134  
-
135  
-      if (err) {
136  
-        res.send(err, 500);
137  
-        return;
138  
-      }
139  
-
140  
-      res.redirect('/admin/artwork');
141  
-    });
142  
-
143  
-  });
144  
-
145  
-
146  
-  /* --- --- --- partials / ajax handlers --- --- --- */
147  
-  app.del('/admin/artwork/:artworkSlug', function(req, res) {
148  
-
149  
-    var Artwork = mongoose.model('Artwork');
150  
-
151  
-    Artwork.remove({slug : req.params.artworkSlug}, function(err) {
152  
-      var result = {};
153  
-
154  
-      if (err) {
155  
-        result = {result: 'error', error:err};
156  
-      } else {
157  
-        result = {result: 'success'};
158  
-      }
159  
-      res.send(result);
160  
-      result = null;
161  
-    })
162  
-  });
163  
-
164  
-  
165  
-  //add new format - partial ajax handler
166  
-  app.post('/admin/artwork/:artworkSlug/format/new.:format', function(req, res) {
167  
-    var Artwork = mongoose.model('Artwork'),
168  
-            Format = mongoose.model('Format');
169  
-
170  
-    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
171  
-      if (err) {
172  
-        res.send(err, 500);
173  
-        return;
174  
-      }
175  
-
176  
-      var format = new Format(
177  
-              {
178  
-                type :  "*unspecified*",
179  
-                detail : '*unspecified*',
180  
-                printsRun : 0,
181  
-                height : 0,
182  
-                width : 0,
183  
-                price : 999999.99,
184  
-                stock : 0
185  
-              });
186  
-
187  
-      artwork.format.push(format);
188  
-
189  
-      artwork.save(function(err) {
190  
-        if (err) {
191  
-          res.send(err, 500);
192  
-          return;
193  
-        }
194  
-
195  
-        if (req.params.format === 'html') {
196  
-          var partials = {layout:false, i : artwork.format.length - 1, format:format};
197  
-
198  
-          res.contentType('text/html');
199  
-          res.render('admin/artwork-format.jade', partials);
200  
-
201  
-          return;
202  
-        }
203  
-
204  
-        res.contentType('application/json');
205  
-        res.send(JSON.stringify(this));
206  
-      });
207  
-    });
208  
-
209  
-  });
210  
-
211  
-  
212  
-  //delete format - partial ajax handler
213  
-  app.del('/admin/artwork/:artworkSlug/format/:formatId', function(req, res) {
214  
-
215  
-    var Artwork = mongoose.model('Artwork');
216  
-
217  
-    Artwork.findOne({slug : req.params.artworkSlug}, function(err, artwork) {
218  
-      if (err) {
219  
-        res.send(err, 500);
220  
-        return;
221  
-      }
222  
-
223  
-      var fmt = artwork.format.id(req.params.formatId);
224  
-
225  
-      if (!fmt) {
226  
-        res.send(JSON.stringify({result: 'error',
227  
-          error : 'could not find format with specified id' + req.params.formatId}));
228  
-
229  
-        return;
230  
-      }
231  
-
232  
-      fmt.remove();
233  
-
234  
-      artwork.save(function(err) {
235  
-        if (err) {
236  
-          res.send(err, 500);
237  
-          return;
238  
-        }
239  
-
240  
-        res.contentType('application/json');
241  
-        res.send(JSON.stringify({result: 'success'}));
242  
-      });
243  
-
244  
-    });
245  
-  });
246  
-
247 18
 };
248 19
 
249  
-/* --- --- --- private helper methods --- --- --- */
250  
-
251  
-function thenRender(template, model, res) {
252  
-  res.render(template, model);
253  
-}
254  
-
255  
-
256  
-function createArtwork(res, mongoose, image, next, artistSlug) {
257  
-  var Artwork = mongoose.model('Artwork'),
258  
-          Format = mongoose.model('Format'),
259  
-          newArtwork, newTitle, newSlug;
260  
-
261  
-  ImageTools.createCopies(image, function(imageFiles) {
262  
-    newTitle = 'unpublished art : ' + new Date().getTime();
263  
-
264  
-    var newFormat = new Format(
265  
-            {
266  
-              type :  "*unspecified*",
267  
-              detail : '*unspecified*',
268  
-              printsRun : 0,
269  
-              height : 0,
270  
-              width : 0,
271  
-              price : 999999,
272  
-              stock : 0
273  
-            });
274  
-    //TODO: figure out a way to add this as a Mongoose plugin.
275  
-    newFormat.hash = mongoose.utilities.getFormatHash(newFormat);
276  
-
277  
-    newArtwork = new Artwork({
278  
-      title : newTitle,
279  
-      image : imageFiles,
280  
-      type : 'original',
281  
-      artist : artistSlug,
282  
-      description : 'A new piece of work',
283  
-      released : new Date(),
284  
-      format : []
285  
-    });
286  
-
287  
-    newArtwork.format.push(newFormat);
288  
-
289  
-    newArtwork.save(function(err) {
290  
-      if (err) {
291  
-        //throw err;
292  
-        res.send(err, 500);
293  
-      }
294  
-
295  
-      next();
296  
-    });
297  
-  })
298  
-}

0 notes on commit 306cc6a

Please sign in to comment.
Something went wrong with that request. Please try again.