Skip to content

Product Polls API

Polls API Documentation

Base URL

api/v1/polls


1. List Questions

Endpoint: api/v1/polls/question/list/

Method: GET

Permissions: AllowAny

This endpoint retrieves a list of questions with sorting options.

Query Parameters: - sort: Sorting criteria. Options include: - highest_vote: Sort by the highest vote count (descending). - lowest_vote: Sort by the lowest vote count (ascending). - updated_at (default): Sort by the latest update time (descending).

Response Payload:

[
  {
    "id": 1,
    "product": 10,
    "user": 5,
    "question": "What is the warranty for this product?",
    "answer": [
      {
        "user": 7,
        "answer": "The warranty is 1 year.",
        "vote": [
          {"user": 8, "value": 1},
          {"user": 9, "value": -1}
        ],
        "vote_count": 0,
        "created_at": "2024-01-01T10:00:00Z",
        "updated_at": "2024-01-01T10:00:00Z"
      }
    ],
    "vote_count": 0,
    "created_at": "2024-01-01T10:00:00Z",
    "updated_at": "2024-01-01T10:00:00Z"
  }
]


2. Create a Question

Endpoint: api/v1/polls/question/add/

Method: POST

Permissions: IsAuthenticated

This endpoint allows a user to create a question for a specific product.

Request Payload:

{
  "product": 10,
  "question": "Is this product waterproof?"
}

Response Payload (Success):

{
  "id": 2,
  "product": 10,
  "user": 5,
  "question": "Is this product waterproof?",
  "answer": [],
  "vote_count": 0,
  "created_at": "2024-01-01T10:10:00Z",
  "updated_at": "2024-01-01T10:10:00Z"
}

Response Payload (Error - Validation):

{
  "question": ["The question must be at least 10 characters long."]
}


3. Update a Question

Endpoint: api/v1/polls/question/update/<str:pk>/

Method: PATCH

Permissions: IsAuthenticated

This endpoint allows a user to update a specific question by its ID.

Request Payload:

{
  "question": "Is this product water-resistant?"
}

Response Payload (Success):

{
  "id": 2,
  "product": 10,
  "user": 5,
  "question": "Is this product water-resistant?",
  "answer": [],
  "vote_count": 0,
  "created_at": "2024-01-01T10:10:00Z",
  "updated_at": "2024-01-01T10:20:00Z"
}

Response Payload (Error - Not Found):

{
  "detail": "Not found."
}


4. Delete a Question

Endpoint: api/v1/polls/question/delete/<str:pk>/

Method: DELETE

Permissions: IsAuthenticated

This endpoint allows a user to delete a specific question by its ID.

Response Payload (Success):

{
  "message": "Question deleted successfully"
}

Response Payload (Error - Not Found):

{
  "detail": "Not found."
}


5. Retrieve Question Details

Endpoint: api/v1/polls/question/detail/<str:pk>/

Method: GET

Permissions: AllowAny

This endpoint retrieves details of a specific question by its ID, including related answers and votes.

Response Payload:

{
  "id": 1,
  "product": 10,
  "user": 5,
  "question": "What is the warranty for this product?",
  "answer": [
    {
      "user": 7,
      "answer": "The warranty is 1 year.",
      "vote": [
        {"user": 8, "value": 1},
        {"user": 9, "value": -1}
      ],
      "vote_count": 0,
      "created_at": "2024-01-01T10:00:00Z",
      "updated_at": "2024-01-01T10:00:00Z"
    }
  ],
  "vote_count": 0,
  "created_at": "2024-01-01T10:00:00Z",
  "updated_at": "2024-01-01T10:00:00Z"
}


6. Add an Answer

Endpoint: api/v1/polls/answer/add/

Method: POST

Permissions: IsAuthenticated

This endpoint allows a user to add an answer to a specific question.

Request Payload:

{
  "question": 1,
  "answer": "Yes, the warranty is for one year."
}

Response Payload (Success):

{
  "id": 3,
  "question": 1,
  "user": 7,
  "answer": "Yes, the warranty is for one year.",
  "vote_count": 0,
  "created_at": "2024-01-01T10:15:00Z",
  "updated_at": "2024-01-01T10:15:00Z"
}

Response Payload (Error - Validation):

{
  "answer": ["The answer must be at least 5 characters long."]
}


7. Vote on an Answer

Endpoint: api/v1/polls/answer/vote/

Method: POST

Permissions: IsAuthenticated

This endpoint allows a user to vote on a specific answer.

Request Payload:

{
  "answer": 3,
  "value": 1
}

Response Payload (Success):

{
  "message": "Vote registered successfully."
}

Response Payload (Error - Already Voted):

{
  "error": "You have already voted for this answer."
}


8. Remove Vote from an Answer

Endpoint: api/v1/polls/answer/vote/remove/<str:pk>/

Method: DELETE

Permissions: IsAuthenticated

This endpoint allows a user to remove their vote from a specific answer.

Response Payload (Success):

{
  "message": "Vote removed successfully."
}

Response Payload (Error - Not Found):

{
  "error": "No vote found to delete."
}


9. List Votes

Endpoint: api/v1/polls/vote/list/

Method: GET

Permissions: AllowAny

This endpoint retrieves all votes in the system.

Response Payload:

[
  {
    "id": 1,
    "user": 5,
    "answer": 3,
    "value": 1,
    "created_at": "2024-01-01T10:30:00Z",
    "updated_at": "2024-01-01T10:30:00Z"
  }
]


10. Retrieve Vote Details

Endpoint: api/v1/polls/vote/detail/<str:pk>/

Method: GET

Permissions: AllowAny

This endpoint retrieves details of a specific vote by its ID.

Response Payload:

{
  "id": 1,
  "user": 5,
  "answer": 3,
  "value": 1
}