routes/venues.js


 

module.exports = function(app,app_secure,uuid){
    /*
     * GET venuelist.
     */
    /*
     * POST to addvenue.
     */

    app.post('/admin/postcomment/:id', function(req, res){
          var id = req.params.id;
          var resp = {
            hasError: false
          };
          if (!req.user){
            resp.hasError = true;
            resp.error = 'Please login and re-post your comment';
          } else {
                var db = req.db;
                var message = req.body;  // req.body.comment
                message._id =  uuid.generate();
                message.venue_id = id;
                message.userName = req.user.profile.name;
                message.userPic = req.user.profile.picture;
                message.userEmail = req.user.email;
                message.reviewed = 'N';
                db.collection('comment').insert(message, function(err, result){
                if(err === null){
                  res.send({msg: result });
                }else{
                res.send({ msg: err });
                }

                });
          }
          res.json(resp);
    });

    app.post('/admin/postrating/:id', function(req, res){
          var id = req.params.id;
          var resp = {
            hasError: false
          };
          if (!req.user){
            resp.hasError = true;
            resp.error = 'Please login and re-post your rating';
          } else {
                var db = req.db;
                var srating = req.body;  // req.body.comment
                srating._id =  uuid.generate();
                srating.venue_id = id;
                srating.userName = req.user.profile.name;
                srating.userEmail = req.user.email;

                db.collection('rating', function(err, collection) {
                  collection.find({'venue_id': srating.venue_id, 'userEmail': srating.userEmail}).toArray(function(err, rvalue){
                    if(err === null){
                      if (rvalue.length >0) {
                        db.collection('rating').update({'_id': rvalue[0]._id},{$set: {'rating': srating.rating}},function(err, result){
                          if(err === null){
                            res.send({msg: result });
                          }else{
                          res.send({ msg: err });
                          }
                        });
                      }
                      else{
                        db.collection('rating').insert(srating, function(err, result){
                          if(err === null){
                            res.send({msg: result });
                          }else{
                            res.send({ msg: err });
                          }
                        });
                      }
                    }else{
                      res.send(err);
                    }
                  });
                });
          }
          res.json(resp);
    });


    app.get('/admin/welcome-page', function(req, res) {
        var db = req.db;
        db.collection('venue').find({},{},{limit:4}).toArray(function (err, venue) {
            db.collection('offers').find({},{},{limit:4}).toArray(function (err, offer) {
                res.json({'venue':venue,'offer':offer});
            });
        });
    });
    app.get('/admin/welcome-page/:city', function(req, res) {
        var db = req.db;
        var city = req.params.city;
        var vObj,oObj,eObj;
        vObj = {'city':city,'extra5':'Y'};
        db.collection('venue').find(vObj,{}).sort({'version': -1}).toArray(function (err, venue) {
            db.collection('offers').find(vObj,{}).toArray(function (err, offer) {
                res.json({'venue':venue,'offer':offer});
            });
        });
    });

    app.get('/admin/welcome-page/:city/type/:type', function(req, res) {
        var db = req.db;
        var city = req.params.city;
        var type = req.params.type;
        var vObj,oObj,eObj;
        vObj = {'city':city};
        if (type && type != 'all') {
            vObj = {'city':city,'type':type};
        }
        if (type == 'tourist') {
            vObj = {'city':city,'extra5':'Y'}
        }
        db.collection('venue').find(vObj,{}).sort({'version': -1}).toArray(function (err, venue) {
            db.collection('offers').find(vObj,{}).toArray(function (err, offer) {
                res.json({'venue':venue,'offer':offer});
            });
        });
    });

    app.get('/admin/venue', function(req, res) {
        var db = req.db;
        db.collection('venue').find().toArray(function (err, items) {
            res.json(items);
        });
    });

    app.get('/admin/venue/search/city/:city/type/:type', function(req, res) {
        var db = req.db;
        var city = req.params.city;
        var type = req.params.type;
        var vObj;
        vObj = {'city':city};
        if (type && type != 'all') {
            vObj = {'city':city,'type':type};
        }
        if (type == 'tourist') {
            vObj = {'city':city,'extra5':'Y'}
        }
        db.collection('venue').find(vObj,{}).toArray(function (err, venue) {
                    res.json(venue);
        });
    });

    app.get('/admin/venue/detail/:id', function(req, res) {
        var db = req.db;
        var id = req.params.id;
        var check, guestEmail;
        if (!req.user){
            check = 'notloggedIn';
        } else {
            check = 'loggedIn';
            guestEmail = req.user.email;
        }
        db.collection('venue').findOne({'_id':id},function (err, venue) {
            db.collection('offers').find({'venue._id':id}).toArray(function (err, offers) {
                db.collection('subList').find({'city':id}).toArray(function (err, subList) {
                    db.collection('comment').find({'venue_id':id, 'reviewed':'Y'}).toArray(function (err, comment){
                      db.collection('rating').find({'venue_id':id}).toArray(function (err, rating){
                        if (rating.length>0){
                          var sumOfRating=0, totalRating, avgCurRating;
                          for (i=0; i<rating.length; i++){
                            var sumOfRating = sumOfRating + rating[i].rating;
                            totalRating= i+1;
                          }
                          avgCurRating = sumOfRating/totalRating;
                          avgCurRating = parseFloat(avgCurRating).toFixed(2);
                        }
                        else {
                          var totalRating=0, avgCurRating=0;
                        }
                        db.collection('rating').find({'venue_id':id, 'userEmail': guestEmail}).toArray(function (err, guestRating){
                          if (guestRating.length>0){
                            userRating=guestRating[0].rating;
                          }
                          else{
                            userRating=0;
                          }
                           res.json({c:check,v:venue,o:offers,s:subList,r:comment,a:avgCurRating,t:totalRating,u:userRating});
                        });
                      });
                    });
                });
            });
        });
    });

    app.get('/admin/venue/:id', function(req, res) {
        var db = req.db;
        var id = req.params.id;
        db.collection('venue').findOne(function (err, items) {
            res.json(items);
        });
    });

    app.get('/admin/venue/list/:city', function(req, res) {
        var db = req.db;
        var city = req.params.city;
        db.collection('venue').find({'city':city}).toArray(function (err, items) {
            res.json(items);
        });
    });


    /*
     * POST to addvenue.
     */

    app.post('/admin/venue/add', function(req, res) {
        var db = req.db;

        var newVen = req.body;
        var longC = parseFloat(newVen.loc.coordinates[0]);
        var latC = parseFloat(newVen.loc.coordinates[1]);
        newVen.loc.coordinates=[longC,latC];
        newVen.loc.type = "Point";
        newVen._id =  uuid.generate();
        db.collection('venue').insert(newVen, function(err, result){
            if(err === null){
                db.collection('venue').find().toArray(function (err, items) {
                  res.json(items);
                 });
            }else{
                res.send({ msg: err });
            }

        });
    });

    app.put('/admin/venue/edit/:id', function(req, res) {
        var db = req.db;
        var newVen = req.body;
        var longC = parseFloat(newVen.loc.coordinates[0]);
        var latC = parseFloat(newVen.loc.coordinates[1]);
        newVen.loc.coordinates=[longC,latC];
        newVen.loc.type = "Point";
        db.collection('venue').update({'_id':newVen._id},newVen,{'safe':true },function(err, result){
            if(err === null){
                db.collection('venue').find().toArray(function (err, items) {
                  res.json(items);
                 });
            }else{
                res.send({ msg: err });
            }

        });

    });

    /*
     * DELETE to deletevenue.
     */
    app.delete('/admin/venue/delete/:id', function(req, res) {
        var db = req.db;
        var venueToDelete = req.params.id;
        db.collection('venue').removeById(venueToDelete, function(err, result) {
            res.send((result === 1) ? { msg: '' } : { msg:'error: ' + err });
        });
    });

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *