Reputation: 14590
Hello I need to find/update users from a mongodb collections via angular. And I need to find them by _id and by username, so I created a service like this:
// Users service used for communicating with the users REST endpoint
angular.module('users').factory('Users', ['$resource',
function($resource) {
return $resource('users/:id', {}, {
update: {
method: 'PUT'
}
});
}
]);
And on Express I have the relative API route:
app.route('/users/:userId').put(users.update);
Now, suppose I have another express route like this to check username availability:
app.route('/users/:username').get(users.check);
How can I integrate this last one in the same angular service?
UPDATE: Solved with this, is it right?
angular.module('users').factory('Users', ['$resource',
function($resource) {
return {
byId: $resource('users/:id', {}, {
update: {
method: 'PUT'
}
}),
byUsername: $resource('users/:username', {}, {
})
};
}
]);
Upvotes: 1
Views: 983
Reputation: 367
Do you want to do something like this?
Angular service:
angular.module('users').factory('Users', function($resource) {
var resource = $resource('users/:byAttr/:id', {}, {
update: {
method: 'PUT',
isArray: false,
cache: false
}
});
return {
updateById: function (id) {
resource.update({id: id, byAttr: 'id'});
},
updateByName: function (username) {
resource.update({username: username, byAttr: 'username'});
},
}
});
Routes:
app.route('/users/id/:userId').put(users.update);
app.route('/users/user/:username').get(users.check);
Upvotes: 3