Browse Source

joins system done inside the travel model (with join and unjoin implemented

master
idoctnef 8 years ago
parent
commit
a51b917484
7 changed files with 59 additions and 20 deletions
  1. +1
    -0
      README.md
  2. +3
    -4
      config.js
  3. +29
    -12
      controllers/travelController.js
  4. +13
    -1
      models/travelModel.js
  5. +5
    -1
      models/userModel.js
  6. +2
    -1
      package.json
  7. +6
    -1
      server.js

+ 1
- 0
README.md

@ -15,6 +15,7 @@ frontend app code: https://github.com/idoctnef/collectivecarApp
**Backend and Frontend:**
```
-signup user --> done
-signup unique username --> done
-loggin user --> done
-update user profile --> done
-create new travel --> done

+ 3
- 4
config.js

@ -1,9 +1,8 @@
module.exports = {
/*'secret': process.env.SECRET,
/*'secret': process.env.SECRET,// production version
'database': process.env.MONGO_DSN,*/
'secret': process.env.SECRET,
'database': process.env.MONGO_DSN,
'secret': 'secretfortoken',// local version
'database': 'mongodb://localhost/comunalcar',
"port" : process.env.PORT || 3000
};

+ 29
- 12
controllers/travelController.js

@ -107,22 +107,25 @@ exports.deleteTravel = function(req, res) {
/* join */
exports.addJoin = function(req, res) {
var join = new joinModel({
travelId: req.params.travelId,
joinedUserId: req.body.joinedUserId,
joinedUsername: req.body.joinedUsername,
acceptedUserId: req.body.acceptedUserId,
joinedAvatar: req.body.joinedAvatar
});
join.save(function(err, join) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(join);
travelModel.findById(req.params.travelId, function(err, travel){
console.log(travel.title);
var join = {
joinedUserId: req.body.joinedUserId,
joinedUsername: req.body.joinedUsername,
acceptedUserId: req.body.acceptedUserId,
joinedAvatar: req.body.joinedAvatar
};
travel.joins.push(join);
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(travel);
});
});
};
exports.doUnjoin = function(req, res) {
joinModel.find({
/*joinModel.find({
travelId: req.params.travelId
}, function(err, joins) {
for(var i=0; i<joins.length; i++)
@ -137,6 +140,20 @@ exports.doUnjoin = function(req, res) {
}
}
});*/
travelModel.findById(req.params.travelId, function(err, travel){
for(var i=0; i<travel.joins.length; i++)
{
if(travel.joins[i].joinedUsername==req.body.joinedUsername)
{
travel.joins.splice(i, 1);
}
}
travel.save(function(err, travel) {
if(err) return res.send(500, err.message);
res.status(200).jsonp(travel);
});
});
};

+ 13
- 1
models/travelModel.js

@ -17,7 +17,19 @@ var travelSchema = new Schema({
phone: { type: Number },
telegram: { type: String },
collectivized: { type: Boolean },
modality: { type: String } //if is an offering travel or asking for travel
modality: { type: String }, //if is an offering travel or asking for travel
joins: [{
joinedUserId: { type: String },
joinedUsername: { type: String },
acceptedUserId: { type: String },
joinedAvatar: { type: String }
}],
comments: [{
commentUserId: { type: String },
commentUsername: { type: String },
comment: { type: String },
commentAvatar: { type: String }
}]
})
module.exports = mongoose.model('travelModel', travelSchema);

+ 5
- 1
models/userModel.js

@ -1,9 +1,11 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');
var userSchema = new Schema({
username: { type: String },
username: { type: String, unique: true },
password: { type: String },
description: { type: String },
avatar: { type: String },
@ -16,4 +18,6 @@ var userSchema = new Schema({
comment: { type: String }
}]
})
userSchema.plugin(mongooseUniqueValidator);
module.exports = mongoose.model('userModel', userSchema);

+ 2
- 1
package.json

@ -13,6 +13,7 @@
"method-override": "^2.1.2",
"mongoose": "latest",
"morgan": "latest",
"md5": "latest"
"md5": "latest",
"mongoose-unique-validator": "^1.0.2"
}
}

+ 6
- 1
server.js

@ -122,9 +122,14 @@ apiRoutes.route('/travels/:id')
.put(travelCtrl.updateTravel)
.delete(travelCtrl.deleteTravel);
apiRoutes.route('/travels/join/:travelId')
/*apiRoutes.route('/travels/join/:travelId')
.post(travelCtrl.addJoin);
apiRoutes.route('/travels/unjoin/:travelId')
.post(travelCtrl.doUnjoin);*/
apiRoutes.route('/travels/:travelId/join')
.post(travelCtrl.addJoin);
apiRoutes.route('/travels/:travelId/unjoin')
.post(travelCtrl.doUnjoin);
apiRoutes.route('/travels/comment/:travelId')

Loading…
Cancel
Save