challenges: get Challenges (mostly) working along the same ui-router
principles as groups. Having some $scope variable resolution timing issues
This commit is contained in:
@@ -17,26 +17,39 @@ var api = module.exports;
|
||||
------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
api.list = function(req, res) {
|
||||
var user = res.locals.user;
|
||||
Challenge.find({
|
||||
$or:[
|
||||
{leader: user._id},
|
||||
{members:{$in:[user._id]}},
|
||||
{group: 'habitrpg'}
|
||||
]
|
||||
})
|
||||
.select('name description memberCount groups')
|
||||
.populate('groups', '_id name')
|
||||
.exec(function(err, challenges){
|
||||
if (err) return res.json(500,{err:err});
|
||||
res.json(challenges);
|
||||
});
|
||||
}
|
||||
|
||||
// GET
|
||||
api.get = function(req, res) {
|
||||
var user = res.locals.user;
|
||||
Challenge.find({$or:[{leader: user._id}, {members:{$in:[user._id]}}]})
|
||||
Challenge.findById(req.params.cid)
|
||||
.populate('members', 'profile.name habits dailys rewards todos')
|
||||
.exec(function(err, challenges){
|
||||
.exec(function(err, challenge){
|
||||
if(err) return res.json(500, {err:err});
|
||||
|
||||
// slim down the return members' tasks to only the ones in the challenge
|
||||
_.each(challenges, function(challenge){
|
||||
_.each(challenge.members, function(member){
|
||||
_.each(['habits', 'dailys', 'todos', 'rewards'], function(type){
|
||||
member[type] = _.where(member[type], function(task){
|
||||
return task.challenge && task.challenge.id == challenge._id;
|
||||
})
|
||||
_.each(challenge.members, function(member){
|
||||
_.each(['habits', 'dailys', 'todos', 'rewards'], function(type){
|
||||
member[type] = _.where(member[type], function(task){
|
||||
return task.challenge && task.challenge.id == challenge._id;
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
res.json(challenges);
|
||||
res.json(challenge);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ api.getMember = function(req, res) {
|
||||
* Fetch groups list. This no longer returns party or tavern, as those can be requested indivdually
|
||||
* as /groups/party or /groups/tavern
|
||||
*/
|
||||
api.getGroups = function(req, res) {
|
||||
api.list = function(req, res) {
|
||||
var user = res.locals.user;
|
||||
var groupFields = 'name description memberCount';
|
||||
var sort = '-memberCount';
|
||||
@@ -45,7 +45,11 @@ api.getGroups = function(req, res) {
|
||||
|
||||
// unecessary given our ui-router setup
|
||||
party: function(cb){
|
||||
return cb(null, [{}]);
|
||||
Group.findOne({type: 'party', members: {'$in': [user._id]}})
|
||||
.select(groupFields).exec(function(err, party){
|
||||
if (err) return cb(err);
|
||||
cb(null, [party]); // return as an array for consistent ngResource use
|
||||
});
|
||||
},
|
||||
|
||||
guilds: function(cb) {
|
||||
@@ -70,7 +74,10 @@ api.getGroups = function(req, res) {
|
||||
|
||||
// unecessary given our ui-router setup
|
||||
tavern: function(cb) {
|
||||
return cb(null, [{}]);
|
||||
Group.findById('habitrpg').select(groupFields).exec(function(err, tavern){
|
||||
if (err) return cb(err);
|
||||
cb(null, [tavern]); // return as an array for consistent ngResource use
|
||||
});
|
||||
}
|
||||
|
||||
}, function(err, results){
|
||||
@@ -83,7 +90,7 @@ api.getGroups = function(req, res) {
|
||||
* Get group
|
||||
* TODO: implement requesting fields ?fields=chat,members
|
||||
*/
|
||||
api.getGroup = function(req, res) {
|
||||
api.get = function(req, res) {
|
||||
var user = res.locals.user;
|
||||
var gid = req.params.gid;
|
||||
|
||||
@@ -111,7 +118,7 @@ api.getGroup = function(req, res) {
|
||||
};
|
||||
|
||||
|
||||
api.createGroup = function(req, res, next) {
|
||||
api.create = function(req, res, next) {
|
||||
var group = new Group(req.body);
|
||||
var user = res.locals.user;
|
||||
|
||||
@@ -136,7 +143,7 @@ api.createGroup = function(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
api.updateGroup = function(req, res, next) {
|
||||
api.update = function(req, res, next) {
|
||||
var group = res.locals.group;
|
||||
var user = res.locals.user;
|
||||
|
||||
|
||||
@@ -19,9 +19,8 @@ var ChallengeSchema = new Schema({
|
||||
//id: group._id
|
||||
//},
|
||||
timestamp: {type: Date, 'default': Date.now},
|
||||
members: [{type: String, ref: 'User'}]
|
||||
}, {
|
||||
minimize: 'false'
|
||||
members: [{type: String, ref: 'User'}],
|
||||
memberCount: [{type: Number, 'default': 0}]
|
||||
});
|
||||
|
||||
ChallengeSchema.virtual('tasks').get(function () {
|
||||
@@ -30,5 +29,10 @@ ChallengeSchema.virtual('tasks').get(function () {
|
||||
return tasks;
|
||||
});
|
||||
|
||||
ChallengeSchema.pre('save', function(next){
|
||||
this.memberCount = _.size(this.members);
|
||||
next();
|
||||
})
|
||||
|
||||
module.exports.schema = ChallengeSchema;
|
||||
module.exports.model = mongoose.model("Challenge", ChallengeSchema);
|
||||
+6
-5
@@ -58,10 +58,10 @@ router['delete']('/user', auth.auth, user['delete']);
|
||||
router['delete']('/user/tags/:tid', auth.auth, user.deleteTag);
|
||||
|
||||
/* Groups*/
|
||||
router.get('/groups', auth.auth, groups.getGroups);
|
||||
router.post('/groups', auth.auth, groups.createGroup);
|
||||
router.get('/groups/:gid', auth.auth, groups.getGroup);
|
||||
router.post('/groups/:gid', auth.auth, groups.attachGroup, groups.updateGroup);
|
||||
router.get('/groups', auth.auth, groups.list);
|
||||
router.post('/groups', auth.auth, groups.create);
|
||||
router.get('/groups/:gid', auth.auth, groups.get);
|
||||
router.post('/groups/:gid', auth.auth, groups.attachGroup, groups.update);
|
||||
//DELETE /groups/:gid
|
||||
|
||||
router.post('/groups/:gid/join', auth.auth, groups.attachGroup, groups.join);
|
||||
@@ -84,8 +84,9 @@ router.post('/market/buy', auth.auth, user.marketBuy);
|
||||
// Note: while challenges belong to groups, and would therefore make sense as a nested resource
|
||||
// (eg /groups/:gid/challenges/:cid), they will also be referenced by users from the "challenges" tab
|
||||
// without knowing which group they belong to. So to prevent unecessary lookups, we have them as a top-level resource
|
||||
router.get('/challenges', auth.auth, challenges.get)
|
||||
router.get('/challenges', auth.auth, challenges.list)
|
||||
router.post('/challenges', auth.auth, challenges.create)
|
||||
router.get('/challenges/:cid', auth.auth, challenges.get)
|
||||
router.post('/challenges/:cid', auth.auth, challenges.update)
|
||||
router['delete']('/challenges/:cid', auth.auth, challenges['delete'])
|
||||
router.post('/challenges/:cid/join', auth.auth, challenges.join)
|
||||
|
||||
Reference in New Issue
Block a user