HashtagSell / posting-api

API for storage and retrieval of posting details for Hashtagsell
0 stars 0 forks source link

Angular struggling to format get request #10

Closed that1guy closed 9 years ago

that1guy commented 9 years ago

This is the GET query I'm trying to formulate with Angular's $http module

http://localhost:4043/v1/postings?filters[mandatory][exact][username]=bdavis

Here is my code:

factory.getUserItemsForSale = function () {

        var deferred = $q.defer();

        var options = {
            "filters": {
                "mandatory": {
                    "exact": {
                        "username": "bdavis"
                    }
                }
            }
        };

        $http({
            method: 'GET',
            url: ENV.postingAPI,
            params: options
        }).then(function (response) {
            console.log('Posting API Resonse', response);
            deferred.resolve(response);
        }, function (err) {
            deferred.reject(err);
        });

        return deferred.promise;
    };

This get request actually translates to:

http://localhost:4043/v1/postings/?filters=%7B%22mandatory%22:%7B%22exact%22:%7B%22username%22:%22bdavis%22%7D%7D%7D
that1guy commented 9 years ago

Josh can you send me an example of the object I should pass in to delete an item from posting api. I can't seem to figure it out.

Here's working solution that gets all a users items for sale.

factory.getUsersItemsForSale = function (username) {

       var deferred = $q.defer();

        var params = {
            filters: {
                mandatory: {
                    exact: {
                        username: username
                    }
                }
            }
        };

        $http({
            method: 'GET',
            url: ENV.postingAPI + utilsFactory.bracketNotationURL(params)
        }).then(function (response) {
            console.log('here it is', response);
            deferred.resolve(response);
        }, function (err) {
            deferred.reject(err);
        });

        return deferred.promise;
    };
factory.bracketNotationURL = function (params, bracketURL) {

        for (var prop in params) {

            if(!bracketURL) {
                bracketURL = '?'+prop;
            } else {
                bracketURL += '[' + prop + ']';
            }

            if (typeof params[prop] === "object" && params[prop] !== null) {

                return factory.bracketNotationURL(params[prop], bracketURL);

            } else {

                bracketURL += '=' + params[prop];

                return bracketURL;
            }
        }
    };
that1guy commented 9 years ago

Looked into your router code and figured this one out.

    router.delete('/:postingId', function (req, res, next) {
        console.log(req.params);
        models.postings.delete(req.params.postingId, function (err, posting) {
            if (err) {
                return next(err);
            }

            return res.status(204).json(posting);
        });
    });

therefore:

factory.deletePost = function (post) {

        var deferred = $q.defer();

        $http({
            method: 'DELETE',
            url: ENV.postingAPI + post.postingId
        }).then(function (response, status, headers, config) {

                console.log('delete response: ', response);

                deferred.resolve(response);

            }, function (response, status, headers, config) {

                deferred.reject(response);
            });

        return deferred.promise;
    };