From daa0d4f42b657b9feedf6023df92eef723f2d865 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Fri, 3 Feb 2017 12:31:39 +0100 Subject: [PATCH] update --- README.md | 9 +- server/controllers/adminController.js | 109 ----- server/controllers/chefController.js | 125 ------ server/controllers/contactController.js | 42 -- server/controllers/conversationController.js | 202 --------- server/controllers/dietController.js | 251 ----------- server/controllers/routineController.js | 303 ------------- server/controllers/runController.js | 107 ----- server/controllers/trainerController.js | 433 ------------------- server/models/contactModel.js | 12 - server/models/conversationModel.js | 27 -- server/models/dietModel.js | 46 -- server/models/routineModel.js | 37 -- server/models/runModel.js | 25 -- server/server.js | 1 + www/app.js | 1 + www/css/chat.css | 21 + www/css/main.css | 0 www/index.html | 4 +- www/views/chat/chat.html | 31 +- www/views/chat/chat.js | 19 +- 21 files changed, 62 insertions(+), 1743 deletions(-) delete mode 100644 server/controllers/adminController.js delete mode 100644 server/controllers/chefController.js delete mode 100644 server/controllers/contactController.js delete mode 100644 server/controllers/conversationController.js delete mode 100644 server/controllers/dietController.js delete mode 100644 server/controllers/routineController.js delete mode 100644 server/controllers/runController.js delete mode 100644 server/controllers/trainerController.js delete mode 100644 server/models/contactModel.js delete mode 100644 server/models/conversationModel.js delete mode 100644 server/models/dietModel.js delete mode 100644 server/models/routineModel.js delete mode 100644 server/models/runModel.js create mode 100644 www/css/chat.css delete mode 100644 www/css/main.css diff --git a/README.md b/README.md index 41d364e..7f19e48 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # socketioMEANseed -seed for a MEAN webapp with socketio, angular-material for the frontend +seed for a MEAN webapp with socketio, angular-material for the frontend webapp ## seed project with * MongoDB @@ -7,6 +7,13 @@ seed for a MEAN webapp with socketio, angular-material for the frontend * Angularjs (with angular-material) * Nodejs * Socketio (angular-socket-io) + * (Ionic [not yet ]) + +## Structure + * Server backend is in the folder /server + * Web frontend, is in the folder /www + * Ionic [not yet], will be in the folder /app +Each process is independent of each other (server runs with CORS) ## To init on /server directory: diff --git a/server/controllers/adminController.js b/server/controllers/adminController.js deleted file mode 100644 index 9e42df0..0000000 --- a/server/controllers/adminController.js +++ /dev/null @@ -1,109 +0,0 @@ -var express = require('express'); -var app = express(); -var jwt = require('jsonwebtoken'); -var config = require('../config/config'); -var crypto = require('crypto'); - -app.set('superSecret', config.secret); - -/*******MODELS*********/ -//var trainerModel = require('../models/trainerModel'); -var userModel = require('../models/userModel'); -var dietModel = require('../models/dietModel'); -var routineModel = require('../models/routineModel'); - -/** GET '/admin/users/' **/ -exports.getUsers = function (req, res) { - userModel.find() - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize) * Number(req.query.page)) - .exec(function (err, users) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(users); - }); -}; -/** GET '/admin/users/:userid' **/ -exports.getUserById = function (req, res) { - userModel.findOne({_id: req.params.userid}) - .lean() - .populate('followers', 'name avatar') - .populate('following', 'name avatar') - .exec(function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'User not found.'}); - } else if (user) { - var nodes=[]; - var edges=[]; - var node={ - title: user.name, - label: user.name, - image: user.avatar, - shape: "image", - id: user._id - }; - nodes.push(node); - for(var i=0; i -1) { - /* True to day done*/ - diet.days[indexDay].done = true; - /* end of done*/ - diet.save(function (err) { - if (err) - return res.send(500, err.message); - res.status(200).jsonp(diet); - });//diet.save - }//End if when day foung - else { - res.json({success: false, message: 'Day not found'}); - } - }//End else if found diet - });//En dietModel for done = true - }//End else if (user) - });//En UserModel findOne() -};//End function diff --git a/server/controllers/routineController.js b/server/controllers/routineController.js deleted file mode 100644 index 8d329a3..0000000 --- a/server/controllers/routineController.js +++ /dev/null @@ -1,303 +0,0 @@ -var express = require('express'); -var app = express(); -var config = require('../config/config'); -var crypto = require('crypto'); - -app.set('superSecret', config.secret); -var userModel = require('../models/userModel'); -var routineModel = require('../models/routineModel'); -//var trainerModel = require('../models/trainerModel'); - -/** GET '/routines/' **/ -exports.getRoutines = function (req, res) { - routineModel.find() - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, routines) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(routines); - }); -}; - -/** GET '/routines/:routineid' **/ -exports.getRoutineById = function (req, res) { - routineModel.findOne({_id: req.params.routineid}) - .lean() - .populate('trainer', 'name avatar') - .populate('client', 'name avatar points.total') - .exec(function (err, routine) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(routine); - }); -}; - -/** DELETE '/routines/:routineid' **/ -exports.deleteRoutineById = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'trainer'}, function (err, trainer) { - if (err) return res.send(500, err.message); - if (!trainer) { - res.json({success: false, message: 'Trainer not found.'}); - } else if (trainer) { - for (var i = 0; i < trainer.routines.length; i++) { - if (trainer.routines[i].equals(req.params.routineid)) {//Solo si esa routine ha sido creada por el trainer - trainer.routines.splice(i, 1); - //també s'hauria de treure la referència al user que és client d'aquesta routine - trainer.save(function (err, trainer) {//guardem el trainer amb la rutina treta - if (err) return res.send(500, err.message); - - routineModel.findByIdAndRemove({_id: req.params.routineid}, function (err) {//elminem la routine - if (err !== null) return res.send(500, err.message); - res.status(200).jsonp('Deleted routine'); - }); - }); - } - } - } - }); -}; - -/** PUT '/routines/:routineid' **/ -exports.updateRoutineById = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token'], role:'trainer'}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'Trainer not found.'}); - } else if (user) { - for (var i = 0; i < user.routines.length; i++) { - if (user.routines[i].equals(req.params.routineid)) { - user.routines.splice(i, 1);// <-- perquè es fa l'splice de user.routines si després no es guarda el user?? - /* Solo si esa routine ha sido creada por el trainer */ - var id = req.params.routineid; - var routine = req.body; - routineModel.update({"_id": id}, routine, - function (err) { - if (err) return console.log(err); - console.log(routine); - res.status(200).jsonp(routine); - }); - } - } - } - }); -}; - -/** POST '/routines/addToClient/:clientid' **/ -exports.addRoutineToClient = function (req, res) { - userModel.findOne({ - 'tokens.token': req.headers['x-access-token'], - 'clients.client': req.params.clientid, - 'role': 'trainer' - }, function (err, trainer) { - if (err) return res.send(500, err.message); - if (!trainer) { - res.json({success: false, message: 'Routine creation failed. Trainer not found.'}); - } else if (trainer) { - var routine = new routineModel({ - title: req.body.title, - description: req.body.description, - trainer: trainer._id,//a partir del token, pillem la id - client: req.params.clientid//es guarda de quin user és la routine - }); - //guardem la routine - routine.save(function (err, routine) { - if (err) { - console.log(err.message); - return res.status(500).send(err.message); - } - //ara guardem la routineid al trainer - trainer.routines.push(routine._id); - trainer.save(function (err, trainer) { - if (err) return res.send(500, err.message); - - }); - //res.status(200).jsonp(routine); - //ara afegim la routine al client - userModel.findOne({'_id': req.params.clientid}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'adding routine to client failed. user not found.'}); - } else if (user) { - user.routines.push(routine._id); - /* gamification */ - var reward = { - concept: "new routine", - date: Date(), - value: +5 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 5; - /* end of gamification */ - - var notification = { - state: "pendent", - message: "trainer has added a routine to you", - link: "training", - icon: "newroutine.png", - date: Date() - }; - user.notifications.push(notification); - user.save(function (err) { - if (err) return res.send(500, err.message); - - res.status(200).jsonp(routine); - }); - }//end else if - }); - }); - }//else - }); -}; - -/** POST '/routines/:routineid/days' **/ -exports.addDayToRoutine = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, trainer) { - if (err) return res.send(500, err.message); - if (!trainer) { - res.json({success: false, message: 'Routine day addition failed. Trainer not found.'}); - } else if (trainer) { - routineModel.findOne({_id: req.params.routineid}, function (err, routine) { - if (err) return res.send(500, err.message); - - if (trainer._id.equals(routine.trainer)) {// si el trainer que fa el post realment és el trainer creator de la routine - routine.days.push(req.body.day); - routine.save(function (err, routine) { - if (err) { - return res.status(500).send(err.message); - } - routineModel.findOne({_id: routine._id}) - .lean() - .populate('trainer', 'name avatar') - .populate('client', 'name avatar points.total') - .exec(function (err, routine) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(routine); - }); - }); - } - }); - }// end else if - }); -}; - -/** POST '/routines/choose' **/ -exports.chooseRoutine = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'choosing routine failed. user not found.'}); - } else if (user) { - console.log(user);//aquí potser caldria comprovar que la routine és la que han creat per l'user - user.routines.push(req.body.routineid); - /* gamification */ - var reward = { - concept: "choosing routine", - date: Date(), - value: +5 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 5; - /* end of gamification */ - user.save(function (err) { - if (err) return res.send(500, err.message); - - res.status(200).jsonp(user); - }); - }//end else if - }); -}; - -/** DELETE '/routines/choose' **/ -exports.unchooseRoutine = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'choosing routine failed. user not found.'}); - } else if (user) { - for (var i = 0; i < user.routines.length; i++) { - if (user.routines[i] == req.body.routineid) {//deletes the diets of the user with the dietid - user.routines.splice(i, 1); - } - } - /* gamification */ - var reward = { - concept: "unchoosing routine", - date: Date(), - value: -7 - }; - user.points.history.push(reward); - user.points.total = user.points.total - 7; - /* end of gamification */ - user.save(function (err) { - if (err) return res.send(500, err.message); - - res.status(200).jsonp(user); - }); - }//end else if - }); -}; - -/** POST '/routines/completeDay/:routineid' **/ -exports.completeDayGamificatedRoutine = function (req, res) { - //1r intentamos darle los puntos al usuario por haber completado el día - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) - return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'choosing routine failed. user not found.'}); - } - else if (user) { - /* gamification */ - var reward = - { - concept: "routine day complete", - date: Date(), - value: +1 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 1; - /* end of gamification */ - user.save(function (err) { - if (err) - return res.send(500, err.message); - }); - //Ahora intentamos añadir done = true dentro del modelo rutina - routineModel.findOne({'_id': req.params.routineid}, function (err, routine) { - if (err) - return res.send(500, err.message); - if (!routine) { - res.json({success: false, message: 'Routine not found'}); - } - else if (routine) { - var indexDay = -1; - for (var i = 0; i < routine.days.length; i++) //routine.days - { - if (routine.days[i]._id.equals(req.body.dayid)) { - //aquí hem trobat el dia que busquem - indexDay = JSON.parse(JSON.stringify(i)); - } - }//End for looking for days - if (indexDay > -1) { - /* True to day done*/ - routine.days[indexDay].done = true; - /* end of done*/ - routine.save(function (err) { - if (err) - return res.send(500, err.message); - routineModel.findOne({_id: routine._id}) - .lean() - .populate('trainer', 'name avatar') - .populate('client', 'name avatar points.total') - .exec(function (err, routine) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(routine); - }); - });//Routine.save - }//End if when day foung - else { - res.json({success: false, message: 'Day not found'}); - } - }//End else if found routine - });//En routineModel for done = true - }//End else if (user) - });//En UserModel findOne() -};//End function diff --git a/server/controllers/runController.js b/server/controllers/runController.js deleted file mode 100644 index 8e67c76..0000000 --- a/server/controllers/runController.js +++ /dev/null @@ -1,107 +0,0 @@ -var express = require('express'); -var app = express(); -var runModel = require('../models/runModel'); -var publicationModel = require('../models/publicationModel'); -var userModel = require('../models/userModel'); -//var trainerModel = require('../models/trainerModel'); -var crypto = require('crypto'); - - -/**POST '/publications' **/ -exports.postRun = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'user not found.'}); - } else if (user) { - //aquí ja hem agafat el user a partir del seu token - var run = new runModel(req.body.newRun); - run.user=user._id; - //fins aquí tenim la variable publication amb els continguts - //ara cal 1r guardar el model publication a la base de dades - run.save(function (err, run) { - if (err) return res.send(500, err.message); - - //i 2n, afegir la id de la publicació generada al user.publications - user.runs.push(run._id); - /* gamification */ - var reward = { - concept: "added new run to user", - date: Date(), - value: +1 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 1; - /* end of gamification */ - user.save(function (err, user) { - if (err) return res.send(500, err.message); - - //res.status(200).jsonp(user); - //ara farem una publicació ensenyant que ha fet aquest run - var publication = new publicationModel({ - title: "new run '" + run.title + "'!", - content: "distance of: " + run.distance + ". View my runs at my profile", - date: new Date(), - user: user._id, - photo: run.photo - }); - //fins aquí tenim la variable publication amb els continguts - //ara cal 1r guardar el model publication a la base de dades - publication.save(function (err, publication) { - if (err) return res.send(500, err.message); - - //i 2n, afegir la id de la publicació generada al user.publications - user.publications.push(publication._id); - /* gamification */ - var reward = { - concept: "added new publication to Timeline", - date: Date(), - value: +1 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 1; - /* end of gamification */ - - if(!user.totalkm) - { - user.totalkm=0; - } - user.totalkm=user.totalkm + run.distance; - - user.save(function (err, user) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(user); - }); - }); - }); - }); - }//end else if - }); -}; - -/**GET '/users/:userid/publications' **/ -exports.getRunsByUserId = function (req, res) { - userModel.findOne({ - _id: req.params.userid - }) - .populate('runs') - .exec(function (error, user) { - if (error !== null) res.send(500, error.message); - - res.status(200).jsonp(user); - }); -}; -/** GET '/run/getById/:publicationid' **/ -exports.getRunByRunId = function (req, res) { - runModel.findOne({_id: req.params.runid}) - .lean() - .populate('user', 'name avatar') - .exec(function (err, run) { - if (err) return res.send(500, err.message); - if (!run) { - res.json({success: false, message: 'run not found.'}); - } else if (run) { - res.status(200).jsonp(run); - } - }); -}; diff --git a/server/controllers/trainerController.js b/server/controllers/trainerController.js deleted file mode 100644 index dc7350d..0000000 --- a/server/controllers/trainerController.js +++ /dev/null @@ -1,433 +0,0 @@ -var express = require('express'); -var app = express(); -var jwt = require('jsonwebtoken'); -var config = require('../config/config'); -var crypto = require('crypto'); - -app.set('superSecret', config.secret); - -/*******MODELS*********/ -//var trainerModel = require('../models/trainerModel'); -var userModel = require('../models/userModel'); -var dietModel = require('../models/dietModel'); -var routineModel = require('../models/routineModel'); - -/** GET '/trainers' **/ -exports.getTrainers = function (req, res) { - userModel.find({role: 'trainer'}) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, trainers) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainers); - }); -}; -exports.getTrainersByDisciplinesArray = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'sending petition failed. user not found.'}); - } else if (user) { - if(user.disciplines[0]) - { - userModel.find({ - role: 'trainer', - $and: [ - {_id: { $nin: user._id}}, - {_id: { $nin: user.trainers}} - ], - 'disciplines.name': user.disciplines[0].name//per ara torna els trainers que tinguin la discipline[0] del user client - }) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, trainers) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainers); - }); - }else{ - userModel.find({ - role: 'trainer', - $and: [ - {_id: { $nin: user._id}}, - {_id: { $nin: user.trainers}} - ] - }) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, trainers) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainers); - }); - } - - } - }); -}; - -/** GET '/trainers/:trainerid' **/ -exports.getTrainerById = function (req, res) { - userModel.findOne({_id: req.params.trainerid, role: 'trainer'}) - .lean() - .populate('routines', 'title description') - .populate('clients.client', 'name avatar points') - .populate('clientsPetitions.clientid', 'name avatar') - .exec(function (err, trainer) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainer); - }); -}; - -/** GET '/trainers/searchByDiscipline' **/ -exports.searchByDiscipline = function (req, res) { - userModel.find({'disciplines.name': req.params.discipline, role: 'trainer'}) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, trainers) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainers); - }); -}; - -/** POST '/trainers/register' **/ -exports.register = function (req, res) { - var trainer = new userModel({ - name: req.body.name, - password: crypto.createHash('sha256').update(req.body.password).digest('base64'), - email: req.body.email, - avatar: 'img/user.png', - background: 'img/background.png', - role: req.body.role, - discipline: req.body.discipline, - points: { - total: 0 - } - }); - /* gamification */ - var reward = { - concept: "account created", - date: Date(), - value: +1 - }; - trainer.points.history.push(reward); - trainer.points.total = trainer.points.total + 1; - /* end of gamification */ - trainer.save(function (err, trainer) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainer); - }); -}; - -/** POST '/trainers/login' **/ -exports.login = function (req, res) { - userModel.findOne({ - email: req.body.email, role: 'trainer' - }) - .select('+password') - .exec(function (err, trainer) { - if (err) throw err; - if (!trainer) { - res.json({success: false, message: 'Authentication failed. trainer not found.'}); - } else if (trainer) { - req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64'); - if (trainer.password != req.body.password) { - res.json({success: false, message: 'Authentication failed. Wrong password.'}); - } else { - var indexToken = -1; - for (var i = 0; i < trainer.tokens.length; i++) { - if (trainer.tokens[i].userAgent == req.body.userAgent) { - indexToken = JSON.parse(JSON.stringify(i)); - } - } - console.log(indexToken); - if (indexToken == -1) {//userAgent no exist - - var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { - // expiresIn: 86400 // expires in 24 hours - }); - var newToken = { - userAgent: req.body.userAgent, - token: tokenGenerated, - os: req.body.os, - browser: req.body.browser, - device: req.body.device, - os_version: req.body.os_version, - browser_version: req.body.browser_version, - ip: req.body.ip, - lastLogin: Date() - }; - trainer.tokens.push(newToken); - } else {//userAgent already exist - trainer.tokens[indexToken].token = ""; - - var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), { - // expiresIn: 86400 // expires in 24 hours - }); - trainer.tokens[indexToken].token = tokenGenerated; - trainer.tokens[indexToken].ip = req.body.ip; - trainer.tokens[indexToken].lastLogin = Date(); - } - trainer.save(function (err, trainer) { - if (err) return res.send(500, err.message); - - // return the information including token as JSON - trainer.password = ""; - res.json({ - user: trainer, - success: true, - message: 'Enjoy your token!', - token: tokenGenerated - }); - }); - } - } - }); -}; - -/** POST '/trainers/acceptClientPetition' **/ -exports.acceptClientPetition = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token'], role: 'trainer'}, function (err, trainer) { - if (err) return res.send(500, err.message); - if (!trainer) { - res.json({success: false, message: 'adding client to trainer failed. trainer not found.'}); - } else if (trainer) { - console.log(trainer);//aquí potser caldria comprovar que la routine és la que han creat per l'trainer - //busquem la petition que estem processant - for (var i = 0; i < trainer.clientsPetitions.length; i++) //routine.days - { - if (trainer.clientsPetitions[i]._id.equals(req.body.petitionid)) { - var newClient = { - client: trainer.clientsPetitions[i].clientid, - petitionMessage: trainer.clientsPetitions[i].message, - date: Date() - }; - trainer.clients.push(newClient); - - //la petició la marco com a accepted - trainer.clientsPetitions[i].state = "accepted"; - trainer.save(function (err) { - if (err) return res.send(500, err.message); - - userModel.findOne({_id: trainer._id, role: 'trainer'}) - .lean() - .populate('routines', 'title description') - .populate('clients.client', 'name avatar points') - .populate('clientsPetitions.clientid', 'name avatar') - .exec(function (err, trainer) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainer); - }); - }); - //ara afegim el trainer al user.trainer - userModel.findOne({'_id': trainer.clientsPetitions[i].clientid}, function (err, user) { - if (err) console.log(err.message); - if (!user) { - console.log('adding client to trainer failed. user not found.'); - } else if (user) { - user.trainers.push(trainer._id); - - /* gamification */ - var reward = { - concept: "new trainer", - date: Date(), - value: +5 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 5; - /* end of gamification */ - - var notification = { - state: "pendent", - message: "trainer has accepted to train you", - link: "training", - icon: "newtrainer.png", - date: Date() - }; - user.notifications.push(notification); - user.save(function (err) { - if (err) console.log(err.message); - console.log("trainer added to user"); - }); - } - }); - } - } - - }//end else if - }); -}; - -/** PUT '/trainers/:trainerid' **/ -exports.updateTrainer = function (req, res) { - var trainer = req.body; - userModel.update({'tokens.token': req.headers['x-access-token']}, trainer, - function (err) { - if (err) return console.log(err); - console.log(trainer); - res.status(200).jsonp(trainer); - }); -}; - -/** POST '/trainers/valorateTrainer/:trainerid' **/ -exports.valorateTrainer = function (req, res) { - userModel.findOne({'tokens.token': req.headers['x-access-token']}, function (err, user) { - if (err) return res.send(500, err.message); - if (!user) { - res.json({success: false, message: 'sending valoration failed. user not found.'}); - } else if (user) { - //ara busquem el trainer - userModel.findOne({_id: req.params.trainerid, role: 'trainer'}, function (err, trainer) { - if (err) return res.send(500, err.message); - if (!trainer) { - res.json({success: false, message: 'sending valoration failed. trainer not found.'}); - } else if (trainer) { - //comprovem que el client no hagi valorat ja el trainer - var javalorat = false; - var indexValoration=-1; - for (var i = 0; i < trainer.valorations.length; i++) { - - if (trainer.valorations[i].clientid.equals(user._id)) { - javalorat = true; - indexValoration=JSON.parse(JSON.stringify(i)); - } - } - - if (javalorat == false) { - var valoration = { - clientid: user._id, - date: Date(), - message: req.body.message, - value: req.body.value - }; - if(!trainer.valoration) - { - trainer.valoration=0; - } - var actual = (+trainer.valoration) * trainer.valorations.length; - var valor = ((+actual) + (+valoration.value)) / (trainer.valorations.length + 1); - trainer.valoration = valor; - trainer.valorations.push(valoration); - - var notification = { - state: "pendent", - message: "client has valorated you", - link: "dashboard", - icon: "newvaloration.png", - date: Date() - }; - trainer.notifications.push(notification); - - trainer.save(function (err) { - if (err) return res.send(500, err.message); - - //aquí la gamificació de l'user que fa la valoració per primer cop - /* gamification */ - var reward = { - concept: "valorating trainer", - date: Date(), - value: +1 - }; - user.points.history.push(reward); - user.points.total = user.points.total + 1; - /* end of gamification */ - user.save(function (err) { - /*if (err) return res.send(500, err.message); - - res.status(200).jsonp(routine);*/ - console.log("points of gamification on trainer valorating added to user"); - }); - - userModel.findOne({_id: trainer._id}) - .lean() - .populate('diets', 'title description') - .populate('routines', 'title description') - .populate('trainers', 'name avatar description') - .populate('clients.client', 'name avatar') - .populate('publications') - .exec(function (err, trainer) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainer); - }); - }); - } else {//end if javalorat==false - console.log("user already has valorated trainer, updating valoration and recalculating total"); - var valoration = { - clientid: user._id, - date: Date(), - message: req.body.message, - value: req.body.value - }; - var actual = ((+trainer.valoration) * (+trainer.valorations.length)) - (+trainer.valorations[indexValoration].value);//suma total valoracions sense la que estic canviant - var valor = ((+actual) + (+valoration.value)) / (trainer.valorations.length); - console.log(actual + ", " + valor); - trainer.valoration = valor; - trainer.valorations[indexValoration]=valoration; - - var notification = { - state: "pendent", - message: "client has updated the valoration on you", - link: "dashboard", - icon: "newvaloration.png", - date: Date() - }; - trainer.notifications.push(notification); - - trainer.save(function (err) { - if (err) return res.send(500, err.message); - - userModel.findOne({_id: trainer._id}) - .lean() - .populate('diets', 'title description') - .populate('routines', 'title description') - .populate('trainers', 'name avatar description') - .populate('clients.client', 'name avatar') - .populate('publications') - .exec(function (err, trainer) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainer); - }); - }); - } - }//end else if - }); - }//end else if - }); -}; - -/** GET '/trainers/:trainerid/getNotifications' **/ -exports.getNotifications = function (req, res) { - userModel.findOne({_id: req.params.trainerid, role: 'trainer'}) - .exec(function (err, trainer) { - if (err) return res.send(500, err.message); - for (var i = 0; i < trainer.notifications.length; i++) { - if (trainer.notifications[i].state == "pendent") { - trainer.notifications[i].state = "viewed"; - trainer.notifications[i].dateviewed = Date(); - } - } - trainer.save(function (err) { - if (err) return res.send(500, err.message); - - res.status(200).jsonp(trainer.notifications); - }); - }); -}; - -/** GET '/trainers/searchByName/:trainername' **/ -exports.searchByName = function (req, res) { - console.log("searchByName"); - userModel.find({name: new RegExp(req.params.trainername, "i"), role: 'trainer'}) - .limit(Number(req.query.pageSize)) - .skip(Number(req.query.pageSize)*Number(req.query.page)) - .exec(function (err, trainers) { - if (err) return res.send(500, err.message); - res.status(200).jsonp(trainers); - }); -}; - -/** DELETE '/trainers/:trainerid' **/ -exports.removeTrainer = function (req, res) {/* AQUESTA FUNCIÖ CREC QUE ESTÂ MAL PLANTEJADA, DIRIA QUE NO FUNCIONA, si jo també ho diria no es pot trobar despres d'eliminar */ - userModel.findByIdAndRemove({_id: req.params.trainerid}, function (err) {/**La he corregit, pero tenint en compte que tenim només un model ara potser es redundant no?**/ - if (err) return res.send(500, err.message); - res.status(200).send("Trainer Deleted"); - }); -}; diff --git a/server/models/contactModel.js b/server/models/contactModel.js deleted file mode 100644 index a151a88..0000000 --- a/server/models/contactModel.js +++ /dev/null @@ -1,12 +0,0 @@ -var mongoose = require('mongoose'); -var mongooseUniqueValidator = require('mongoose-unique-validator'); -var Schema = mongoose.Schema; - -var contactSchema = new Schema({ - name: {type: String, required: true,}, - subject: {type: String, required: true, select: false}, - email: {type: String, required: true, unique: true}, - description: {type: String} -}); -contactSchema.plugin(mongooseUniqueValidator); -module.exports = mongoose.model('contactModel', contactSchema); diff --git a/server/models/conversationModel.js b/server/models/conversationModel.js deleted file mode 100644 index b1508e6..0000000 --- a/server/models/conversationModel.js +++ /dev/null @@ -1,27 +0,0 @@ -var mongoose = require('mongoose'); -var mongooseUniqueValidator = require('mongoose-unique-validator'); -var Schema = mongoose.Schema; - -var conversationSchema = new Schema({ - userA: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - userB: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - messages: [{ - user: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - content: {type: String}, - date: {type: Date} - }], - modifiedDate: {type: Date} -}); - -conversationSchema.plugin(mongooseUniqueValidator); - -module.exports = mongoose.model('conversationModel', conversationSchema); diff --git a/server/models/dietModel.js b/server/models/dietModel.js deleted file mode 100644 index a345c19..0000000 --- a/server/models/dietModel.js +++ /dev/null @@ -1,46 +0,0 @@ -var mongoose = require('mongoose'); -var mongooseUniqueValidator = require('mongoose-unique-validator'); -var Schema = mongoose.Schema; - -var dietSchema = new Schema({ - title: {type: String}, - description: {type: String}, - startingDay: {type: Date}, - price: { type: Number }, - image: { type: String }, - clients: [{ - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }], - chef: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - days: [{ - date: {type: Date}, //Ha de ser realmente un día que empiezas por ejemplo 12/12/2016 para poder ir completando según la fecha, comer comes cada día - title: {type: String}, - description: {type: String}, - meals: [{ - title: {type: String}, - img: {type: String}, - submeals: [{ - title: {type: String}, - description: {type: String}, - amount: { - unit: {type: String}, - quantity: {type: Number} - }, - nutritional: { - kcal: {type: Number}, - proteins: {type: Number}, - carbohidrates: {type: Number}, - fats: {type: Number}, - vitamins: {type: Number} - } - }] - }], - done: {type: Boolean, default: false} - }] -}); -dietSchema.plugin(mongooseUniqueValidator); -module.exports = mongoose.model('dietModel', dietSchema); diff --git a/server/models/routineModel.js b/server/models/routineModel.js deleted file mode 100644 index 428f05d..0000000 --- a/server/models/routineModel.js +++ /dev/null @@ -1,37 +0,0 @@ -var mongoose = require('mongoose'); -var mongooseUniqueValidator = require('mongoose-unique-validator'); -var Schema = mongoose.Schema; - -var routineSchema = new Schema({ - title: {type: String}, - description: {type: String}, - startingDay: {type: Date}, - discipline: {type: String}, - price: { type: Number },//si és gratis, es posa q val 0, així els users ho veuen amb bons ulls - image: { type: String }, - client: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - trainer: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - days: [{ - title: {type: String}, - description: {type: String}, - exercises: [{ - title: {type: String}, - description: {type: String}, - img: {type: String}, - weight: {type: String}, - distance: {type: String}, - reps: {type: String}, - series: {type: String} - }], - done: {type: String, default: false}//si ha complert el dia - }] -}) -; -routineSchema.plugin(mongooseUniqueValidator); -module.exports = mongoose.model('routineModel', routineSchema); diff --git a/server/models/runModel.js b/server/models/runModel.js deleted file mode 100644 index 77d0ff6..0000000 --- a/server/models/runModel.js +++ /dev/null @@ -1,25 +0,0 @@ -var mongoose = require('mongoose'); -var mongooseUniqueValidator = require('mongoose-unique-validator'); -var Schema = mongoose.Schema; - -var runSchema = new Schema({ - title: {type: String}, - photo: {type: String}, - user: { - type: mongoose.Schema.Types.ObjectId, - ref: 'userModel' - }, - datestart: {type: Date}, - datefinish: {type: Date}, - distance: {type: Number}, - positions: [{ - date: {type: Date}, - lat: {type: Number}, - long: {type: Number}, - distance: {type: Number} - }] -}); - -runSchema.plugin(mongooseUniqueValidator); - -module.exports = mongoose.model('runModel', runSchema); diff --git a/server/server.js b/server/server.js index bd649ec..2d2e0d8 100644 --- a/server/server.js +++ b/server/server.js @@ -24,6 +24,7 @@ io.on('connection', function(socket){ socket.on('msg', function (data, callback){ console.log("msg"); + data.date= new Date(); io.sockets.emit('newmsg', data); //aqí envia la data }); }); diff --git a/www/app.js b/www/app.js index efb7dbd..a7ad71d 100644 --- a/www/app.js +++ b/www/app.js @@ -3,6 +3,7 @@ angular.module('app', [ 'ngRoute', 'btford.socket-io', + 'ngMaterial', 'app.chat' ]) .factory('socket', function(socketFactory) { diff --git a/www/css/chat.css b/www/css/chat.css new file mode 100644 index 0000000..f70ecba --- /dev/null +++ b/www/css/chat.css @@ -0,0 +1,21 @@ +.chat_backgroundImg{ + width: auto; + height: 100%; + min-height: auto; + -webkit-background-size: cover; + -moz-background-size: cover; + background-size: cover; + -o-background-size: cover; + background-position: center; + background-image: url('https://images3.alphacoders.com/106/106327.jpg'); + text-align: center; + /*color: white;*/ + + /* Create the parallax scrolling effect */ + background-attachment: fixed; + background-position: center; + background-repeat: no-repeat; + background-size: cover; + + color: #ffffff; +} diff --git a/www/css/main.css b/www/css/main.css deleted file mode 100644 index e69de29..0000000 diff --git a/www/index.html b/www/index.html index de7f011..e123947 100644 --- a/www/index.html +++ b/www/index.html @@ -5,11 +5,11 @@ - + - + diff --git a/www/views/chat/chat.html b/www/views/chat/chat.html index 1a80f0f..271673b 100644 --- a/www/views/chat/chat.html +++ b/www/views/chat/chat.html @@ -1,18 +1,23 @@ - - +
chat
-
- -
- - - 3 line item (with hover) - - -

{{msg.text}}

-
-
+ + + +

{{msg.text}}

+
{{msg.date | date:"HH:mm"}}h
+
+
+ + +
+ + + + Send +
+
diff --git a/www/views/chat/chat.js b/www/views/chat/chat.js index 2c7f044..8438372 100644 --- a/www/views/chat/chat.js +++ b/www/views/chat/chat.js @@ -1,22 +1,25 @@ 'use strict'; angular.module('app.chat', [ - 'btford.socket-io', 'ngRoute' + 'btford.socket-io', 'ngRoute', 'ngAnimate' ]) -.controller('ChatCtrl', function ($scope, socket) { +.controller('ChatCtrl', function ($scope, socket, + $filter) { $scope.msgs=[]; socket.on('newmsg', function (data) { console.log(data); $scope.msgs.push(data); }); - $scope.msg={ - text: "" - }; + $scope.msg={}; $scope.send = function(){ - console.log("emitting"); - socket.emit("msg", $scope.msg, function(data){ + if($scope.msg.text) + { + console.log("emitting"); + socket.emit("msg", $scope.msg, function(data){ - }); + }); + $scope.msg={}; + } }; });