Alex Rivera | Logout

How would you design a RESTful voting system?

Asked 2009-02-24T15:29:13.723
14

In my continuing quest to try and wrap my mind around RESTful-ness, I've come to another place where I'm not sure how to proceed. I set up a thought expiriment for myself where I'd design a simple voting system for a resource, much like how SO allows voting on questions. So, say my resource is an image, and I can get an image by an ID, like so:

http://www.mysite.com/images/123123

And in this example, that returns say, a JSON representation of an image, like so:

{
"URL":"http://www.mysite.com/images/123123.jpg",
"Rep":"100"
}

How would I design a way to "vote" on that image? I'd like two operations; up-vote and down-vote. The client shouldn't know how much weight each carries, because I'd like to have the award for an up-vote/down-vote be decided at the server level so I can change it anytime I like.

My first idea was to have something like this:

http://www.mysite.com/vote/images?image=123123

To that URL, one could POST something like the following:

{
"Vote":"UpVote"
}

But I'm wary of that - to me that says RPC in disguise. Would that be a poor way to design this? If so, what other designs could I try?

Edit
Report

1 Answer

-1

If you're working with Rails, I'd go for this GET-URLs:

http://www.mysite.com/images/123123/up_vote

and

http://www.mysite.com/images/123123/down_vote

1. Define the actions "up_vote" and "down_vote" in your Images controller and have it increase or decrease the vote value of your image-model-object.

2. Set the following route in config/routes.rb:

map.resources :images, :member => { :up_vote => :get, :down_vote => :get }

...and you're done (more or less ;-)).

answered 2009-02-24T15:43:05.203

Your Answer