Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/pvnm4/Social-Media-Backend/llms.txt

Use this file to discover all available pages before exploring further.

The vote endpoint lets authenticated users upvote or remove their vote from a post. Each user can vote on a post at most once — submitting a second upvote on the same post returns a 409 Conflict. To undo a vote, send the same request with dir: 0. Both actions are handled by a single POST /vote/ endpoint.

Endpoint

POST /vote/ — authentication required.

How voting works

Voting is controlled by the dir field in the request body:
  • dir: 1 — Adds your vote to the post. If you have already voted on this post, the server returns 409 Conflict.
  • dir: 0 — Removes your existing vote from the post. If you have not yet voted on this post, the server returns 404 Not Found.
Each vote is stored as a row in the votes table with a composite primary key of (user_id, post_id). This constraint is enforced at the database level, guaranteeing that each user can hold at most one vote per post at any given time.
# Vote ORM model
class Vote(Base):
    __tablename__ = "votes"
    user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
    post_id = Column(Integer, ForeignKey("posts.id", ondelete="CASCADE"), primary_key=True)

Request body

post_id
integer
required
The ID of the post to vote on. The post must exist — if it does not, a 404 Not Found is returned.
dir
integer
required
Direction of the vote action. Use 1 to add a vote, or 0 to remove your existing vote. The maximum accepted value is 1.

Response

HTTP 201 Created Returns a plain JSON message confirming the action. When voting (dir: 1):
{"message": "Successfully added vote"}
When removing a vote (dir: 0):
{"message": "successfully deleted vote"}
message
string
A human-readable confirmation of the action taken — either "Successfully added vote" or "successfully deleted vote".

Error responses

StatusConditionDetail Message
401 UnauthorizedMissing or invalid JWT token.(FastAPI/OAuth2 default)
404 Not FoundThe target post does not exist."Post with id: {post_id} doss not exist."
404 Not Founddir: 0 — the authenticated user has not voted on this post."vode does not exist"
409 Conflictdir: 1 — the authenticated user has already voted on this post."User {user_id} has already voted on the post {post_id}"

Examples

curl -X POST http://localhost:8000/vote/ \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"post_id": 42, "dir": 1}'

Vote counts in posts — The current vote count for each post is included in post responses. When you fetch posts via GET /posts/ or GET /posts/{id}, the vote field on the PostOut response object reflects the total number of votes that post has received.

Build docs developers (and LLMs) love