arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/controllers/runController.js
server/controllers/runController.jsRaw
  1var express = require('express');
  2var app = express();
  3var runModel = require('../models/runModel');
  4var publicationModel = require('../models/publicationModel');
  5var userModel = require('../models/userModel');
  6//var trainerModel = require('../models/trainerModel');
  7var crypto = require('crypto');
  8
  9
 10/**POST '/publications' **/
 11exports.postRun = function (req, res) {
 12    userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) {
 13        if (err) return res.send(500, err.message);
 14        if (!user) {
 15            res.json({success: false, message: 'user not found.'});
 16        } else if (user) {
 17            //aquí ja hem agafat el user a partir del seu token
 18            var run = new runModel(req.body.newRun);
 19            run.user=user._id;
 20            //fins aquí tenim la variable publication amb els continguts
 21            //ara cal 1r guardar el model publication a la base de dades
 22            run.save(function (err, run) {
 23                if (err) return res.send(500, err.message);
 24
 25                //i 2n, afegir la id de la publicació generada al user.publications
 26                user.runs.push(run._id);
 27                /* gamification */
 28                var reward = {
 29                    concept: "added new run to user",
 30                    date: Date(),
 31                    value: +1
 32                };
 33                user.points.history.push(reward);
 34                user.points.total = user.points.total + 1;
 35                /* end of gamification */
 36                user.save(function (err, user) {
 37                    if (err) return res.send(500, err.message);
 38
 39                    //res.status(200).jsonp(user);
 40                    //ara farem una publicació ensenyant que ha fet aquest run
 41                    var publication = new publicationModel({
 42                        title: "new run '" + run.title + "'!",
 43                        content: "distance of: " + run.distance + ". View my runs at my profile",
 44                        date: new Date(),
 45                        user: user._id,
 46                        photo: run.photo
 47                    });
 48                    //fins aquí tenim la variable publication amb els continguts
 49                    //ara cal 1r guardar el model publication a la base de dades
 50                    publication.save(function (err, publication) {
 51                        if (err) return res.send(500, err.message);
 52
 53                        //i 2n, afegir la id de la publicació generada al user.publications
 54                        user.publications.push(publication._id);
 55                        /* gamification */
 56                        var reward = {
 57                            concept: "added new publication to Timeline",
 58                            date: Date(),
 59                            value: +1
 60                        };
 61                        user.points.history.push(reward);
 62                        user.points.total = user.points.total + 1;
 63                        /* end of gamification */
 64
 65                        if(!user.totalkm)
 66                        {
 67                            user.totalkm=0;
 68                        }
 69                        user.totalkm=user.totalkm + run.distance;
 70
 71                        user.save(function (err, user) {
 72                            if (err) return res.send(500, err.message);
 73                            res.status(200).jsonp(user);
 74                        });
 75                    });
 76                });
 77            });
 78        }//end else if
 79    });
 80};
 81
 82/**GET '/users/:userid/publications' **/
 83exports.getRunsByUserId = function (req, res) {
 84    userModel.findOne({
 85        _id: req.params.userid
 86    })
 87    .populate('runs')
 88    .exec(function (error, user) {
 89        if (error !== null) res.send(500, error.message);
 90
 91        res.status(200).jsonp(user);
 92    });
 93};
 94/** GET '/run/getById/:publicationid' **/
 95exports.getRunByRunId = function (req, res) {
 96    runModel.findOne({_id: req.params.runid})
 97        .lean()
 98        .populate('user', 'name avatar')
 99        .exec(function (err, run) {
100            if (err) return res.send(500, err.message);
101            if (!run) {
102                res.json({success: false, message: 'run not found.'});
103            } else if (run) {
104                res.status(200).jsonp(run);
105            }
106        });
107};