1

I'm building an RESTful API. I have a Person object in my model and every Person object has a IsStarred boolean property. So, for the Person objects, I've created a "people" resource that looks like this:

// Performs CRUD operations on this resource.
api/people/:id 

In order to preserve the RESTful model, I have the following resource that changes the IsStarred property of a given Person object:

// POST method sets IsStarred property to true, while DELETE method sets it to false.
// Example: POST api/people/2/star => Sets IsStarred = true to Person with Id == 2
// DELETE api/people/2/star => Sets IsStarred = false to Person with Id == 2
api/people/:id/star 

Now, I want to use the same strucutre in an Angular app, using $resource. I already have a Person resource that looks like this:

var Person= $resource('api/people/:id', { id: '@id' });

And I can use this Person resource in the well-known way.

By the way, now if I decide to edit the resource and make it look like this:

var Person= $resource('api/people/:id/star', { id: '@id' });

I can now use only the (un)star functionality, meaning I would have to make two different resources. Is this my only option or there's another one?

2
  • What I generally do is -> make the "star" also an optional param as :star. Then I just pass it in the object as a param. Works quite well. Commented Nov 10, 2015 at 11:54
  • @wtflux - yeap, thanks, I think Petr's answer is presenting exactly what you are saying, isn't it? Commented Nov 10, 2015 at 12:16

1 Answer 1

1

You can use additional param:

var Person = $resource('api/people/:id:mode', { id: '@id'}, 
    get : {method : 'GET', params : {mode : ''}}, 
    post : {method : 'POST', params : {mode : '/star'}});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.