Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting multi level schemas #27

Closed
boedlen opened this issue Apr 11, 2014 · 4 comments
Closed

Posting multi level schemas #27

boedlen opened this issue Apr 11, 2014 · 4 comments

Comments

@boedlen
Copy link

boedlen commented Apr 11, 2014

This might be a dumb question, but i'm trying to contruct a post that can save an "advanced" schema.

My schema looks like this:

exports.Schemas = {};
exports.Schemas.Activity = new Schema({
  key: {type:String, required: true, unique: true},
  name: {type:String, required: true},
  zipcode: Number,
  city: {type: String, required: true},
  description: String,
  image: String,
  category: [String],
  type: {type: String, required: true},
  updated: { type: Date, default: Date.now },
  created: { type: Date, default: Date.now },
  openinghours: [this.Schemas.OpeningHour]
});

exports.Schemas.OpeningHour = new Schema({
  name: String,
  from: Date,
  to: Date,
  days: [this.Schemas.OpeningHourWeekday]
});

exports.Schemas.OpeningHourWeekday = new Schema({
  weekday: Number,
  openfrom: String,
  opento: String
});

What should my post look like to create an openinghours on my Activity?

i tried this, but it just created an activity without openinghours

key: "k"
city: "k"
description: "k"
name: "q1"
openinghours.from: "2014-04-01"
openinghours.name: "Season 1"
openinghours.to: "2014-04-30"
type: "k"
zipcode: "4"`
@VARG0S
Copy link
Contributor

VARG0S commented May 9, 2014

@pehaada and I were discussing this and think there may be a bug when posting more than 2 "levels" deep. Using the example from the readme, posting to

http://localhost:3000/rest/blogpost/$id/comments/ 

will work fine, however if you add another level like

http://localhost:3000/rest/blogpost/$id/comments/$id/notes/

or in your case

/Activity/$id/OpeningHour/$id/OpeningHourWeekday/

then there seems to be some issues in the routes.js file for the router.post.
I modified the code to assume that any additional layers would follow a path of

/level1/$id/level2/$id/level3/...etc

However, I'm not sure my solution is the most elegant or best way to do so.

router.post(reGet, function (req, res, next) {
        res.contentType('json');
        var single = req.query.single;
        var stuff = [].concat(req.params);
        stuff.shift();

        var Model = m(req.params[0]);
        var qo = idObj(req.params[1]);
        var pos = req.params[2] && req.params[2].split('/');
        var find = Model.findOne(qo);
        find.exec(function (err, obj) {
            if (err)return next(err);
            var put = clean(req.body), orig = [].concat(pos);
           console.log(put);
            if (pos) {
                var o = obj
//                while (o && pos.length - 1) {
//                    o = o[pos.shift()]
//                }
//                if (o[pos[0]] instanceof Array) {
//                    o[pos[0]].push(put);
//                } else {
//                    o[pos[0]] = put;
//                }
//                obj.markModified(orig.join('.'));

//BEGIN CHANGE
                var evalString = "obj";
                while (o && pos.length > 0) {
                    var shiftVar = pos.shift();
                    if (o[shiftVar] instanceof Array) {
                        evalString += "." + shiftVar
                        o = o[shiftVar];
                    } else {
                        evalString += '.id("' + shiftVar +'")'
                        o = _u.findWhere(o, {id: shiftVar});
                    }
                }
                if (eval(evalString) instanceof Array) {
                    eval(evalString).push(put);
                } else {
                    eval(evalString) = put;
                }
//END CHANGE
            } else {
                _u.extend(obj, put);
            }

            obj.save(function (err, ret) {
                res.contentType('json');
                if (err) {
                    console.log("error saving ", err, err.stack);
                    res.send({
                        status: 1,
                        message: err.message
                    });
                } else {
                    var obj = ret.toJSON();
                    if (pos) {
                        while (obj && orig.length) {
                            obj = obj[orig.shift()];
                        }
                    }
                    res.send({
                        status: 0,
                        payload: Array.isArray(obj) ? obj.pop() : obj
                    })
                }
            });
        });
    });

@jspears Does this seem like a good solution? I've commented out the existing code and included mine above. Please forgive me if this is a poor way of requesting a change, I'm new to github and am not too familiar with etiquette around change or pull requests, except that I'm pretty sure that my this isn't the way to do it 😃

@jspears
Copy link
Owner

jspears commented May 9, 2014

Awesome I'll have to take a closer look, but thank you... I am happy
with any pull request, I almost always merge them.

Thanks!

On Fri, May 9, 2014 at 1:17 PM, Matt notifications@github.com wrote:

@pehaada https://github.com/pehaada and I were discussing this and
think there may be a bug when posting more than 2 "levels" deep. Using the
example from the readme, posting to

http://localhost:3000/rest/blogpost/$id/comments/

will work fine, however if you add another level like

http://localhost:3000/rest/blogpost/$id/comments/$id/notes/

or in your case

/Activity/$id/OpeningHour/$id/OpeningHourWeekday/

then there seems to be some issues in the routes.js file for the
router.post.
I modified the code to assume that any additional layers would follow a
path of

/level1/$id/level2/$id/level3/...etc

However, I'm not sure my solution is the most elegant or best way to do so.

router.post(reGet, function (req, res, next) {
res.contentType('json');
var single = req.query.single;
var stuff = [].concat(req.params);
stuff.shift();

    var Model = m(req.params[0]);
    var qo = idObj(req.params[1]);
    var pos = req.params[2] && req.params[2].split('/');
    var find = Model.findOne(qo);
    find.exec(function (err, obj) {
        if (err)return next(err);
        var put = clean(req.body), orig = [].concat(pos);
       console.log(put);
        if (pos) {
            var o = obj//                while (o && pos.length - 1) {//                    o = o[pos.shift()]//                }//                if (o[pos[0]] instanceof Array) {//                    o[pos[0]].push(put);//                } else {//                    o[pos[0]] = put;//                }//                obj.markModified(orig.join('.'));

//BEGIN CHANGE
var evalString = "obj";
while (o && pos.length > 0) {
var shiftVar = pos.shift();
if (o[shiftVar] instanceof Array) {
evalString += "." + shiftVar
o = o[shiftVar];
} else {
evalString += '.id("' + shiftVar +'")'
o = _u.findWhere(o, {id: shiftVar});
}
}
if (eval(evalString) instanceof Array) {
eval(evalString).push(put);
} else {
eval(evalString) = put;
}//END CHANGE
} else {
_u.extend(obj, put);
}

        obj.save(function (err, ret) {
            res.contentType('json');
            if (err) {
                console.log("error saving ", err, err.stack);
                res.send({
                    status: 1,
                    message: err.message
                });
            } else {
                var obj = ret.toJSON();
                if (pos) {
                    while (obj && orig.length) {
                        obj = obj[orig.shift()];
                    }
                }
                res.send({
                    status: 0,
                    payload: Array.isArray(obj) ? obj.pop() : obj
                })
            }
        });
    });
});

@jspears https://github.com/jspears Does this seem like a good
solution? I've commented out the existing code and included mine above.
Please forgive me if this is a poor way of requesting a change, I'm new to
github and am not too familiar with etiquette around change or pull
requests, except that I'm pretty sure that my this isn't the way to do it [image:
😃]


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-42708963
.

@VARG0S
Copy link
Contributor

VARG0S commented May 9, 2014

Thanks! I figured out how to make a pull request, so I submitted that.

@jspears
Copy link
Owner

jspears commented May 19, 2014

I believe this is fixed in 0.9 + sorry about that... I have tests, that work! Thanks for the patch

@jspears jspears closed this as completed May 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants