Skip to content

Instantly share code, notes, and snippets.

@sandersch
Last active February 16, 2022 18:41
Show Gist options
  • Save sandersch/e1b49a9735659f0cb3917f9f1f57bd29 to your computer and use it in GitHub Desktop.
Save sandersch/e1b49a9735659f0cb3917f9f1f57bd29 to your computer and use it in GitHub Desktop.

README

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.

api

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.

POST /votes

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:

  1. A voter in the database (in this case, with name "Jack Collier")
  2. A venue in the database, in this case, "Woodstock B&B"
  3. A vote of +1 or -1 for the venue.
GET /venues

Returns something like this:

[
  {
    "id":1,
    "name":"Woodstock B\u0026B",
    "score":8
  }
]

Example Payloads

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"}}'

Other Design Choices

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment