routes/subList.js


 

module.exports = function(app,app_secure,uuid){

    app.get('/admin/subList/detail/:id', function(req, res) {
        var db = req.db;
        var id = req.params.id;
        console.log("id from subList.js: " + id);
        var check, guestEmail;
        if (!req.user){
            check = 'notloggedIn';
        } else {
            check = 'loggedIn';
            guestEmail = req.user.email;

        }
        db.collection('subList').findOne({'_id':id},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,S:subList,r:comment,a:avgCurRating,t:totalRating,u:userRating});
                });
               });
            });
        });
    });

    app.get('/admin/sublist/search/city/:city', function(req, res) {
        var db = req.db;
        var city = req.params.city;
        var vObj;
        vObj = {'city':city};
        db.collection('subList').find(vObj,{}).toArray(function (err, subList) {
                    res.json(subList);
        });
    });

    /*
     * POST to addsubList.
     */

    app.post('/admin/subList/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('subList').insert(newVen, function(err, result){
            if(err === null){
                db.collection('subList').find().toArray(function (err, items) {
                  res.json(items);
                 });
            }else{
                res.send({ msg: err });
            }

        });
    });

    app.put('/admin/subList/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('subList').update({'_id':newVen._id},newVen,{'safe':true },function(err, result){
            if(err === null){
                db.collection('subList').find().toArray(function (err, items) {
                  res.json(items);
                 });
            }else{
                res.send({ msg: err });
            }

        });

    });

    /*
     * DELETE to deletesubList.
     */
    app.delete('/admin/subList/delete/:id', function(req, res) {
        var db = req.db;
        var subListToDelete = req.params.id;
        db.collection('subList').removeById(subListToDelete, 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 *