We are building a database of Bed and Breakfasts, along with user-based votes in favor or against those venues. In order to build this list of Bed and Breakfasts, we'll provide an application that allows users to give a "+1" or a "-1" to a particular place -- a hybrid combination of Reddit-style voting on a Yelp-style database.
Our task is to build the controller and model methods required to implement the JSON API that the message processor will use to store votes. We will not need to build any front-end components to handle the voting.
The purpose of the JSON API is to provide two endpoints -- one to collect ballots from users, and the other to produce a list of all of the Bed and Breakfast venues that have been voted (good or bad).
The API will be used by whatever front end application is collecting votes. Our job is only to count and tabulate scores.
The API does not require token or other authentication.
Expects a ballot payload, which will be formatted like this:
{
"name": "Woodstock B&B",
"vote":-1,
"voter": {
"first_name":"Jack",
"last_name":"Collier"
}
}
After storing the voter, vote, and venue information, it should return HTTP_OK. If for any reason, the vote information cannot be saved, it should return a 422 status with messages.
Please note, the payload for this API endpoint contains information about 3 different entities. After this method is called, we should see:
- A voter in the database (in this case, with name "Jack Collier")
- A venue in the database, in this case, "Woodstock B&B"
- A vote of +1 or -1 for the venue.
Returns something like this:
[
{
"id":1,
"name":"Woodstock B\u0026B",
"score":8
}
]
You might also use cURL to send a test payload:
curl -X POST -H "Content-Type: application/json" http://localhost:3000/votes -d '{"name":"dutchie B and B", "vote": "1", "voter": {"first_name":"Your", "last_name":"Name"}}'
curl -X POST -H "Content-Type: application/json" http://localhost:3000/votes -d '{"name":"dutchie B and B", "vote": "-1", "voter": {"first_name":"Your", "last_name":"Name"}}'
curl -X POST -H "Content-Type: application/json" http://localhost:3000/votes -d '{"name":"dutchie B and B", "vote": "1", "voter": { } }'
curl -X POST -H "Content-Type: application/json" http://localhost:3000/votes -d '{"vote": "1", "voter": {"first_name":"Your", "last_name":"Name"}}'
Votes are expected to by +1 / -1, thumbs-up style. Voting by star-rating is not contemplated.
Strings, usernames, and passwords not yet extracted.
We do not enforce "1 person, 1 vote". Bonus points if you can implement.