Skip to content

Commit

Permalink
refactor(api/vote): refactor vote http endpoint and logic
Browse files Browse the repository at this point in the history
The original `POST /votes/:type/:target_id` endpoint for creating votes has been updated to `POST /votes/:target_name/:target_id/:choice`. The `target_name` can be either `comment` or `page`, the `target_id` is a numeric value, and the `choice` can be `up` or `down`.

The HTTP response now includes two additional fields: `is_up` and `is_down`, which indicate the user's current voting state. The `up` and `down` fields remain unchanged and continue to be of numeric type, ensuring backward compatibility with existing clients.

- Change vote endpoint to include `{target_name}` and `{choice}`
- Update vote logic to handle `up` and `down` choices
- Modify request data models and API interfaces for new vote structure
- Ensure backward compatibility with existing vote response data
- Improve vote handling and response structure

BREAKING CHANGE: Vote HTTP API endpoint have been updated.
  • Loading branch information
qwqcode committed Oct 6, 2024
1 parent bd0db8c commit 9046996
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 132 deletions.
43 changes: 30 additions & 13 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3525,9 +3525,9 @@ const docTemplate = `{
}
}
},
"/votes/{type}/{target_id}": {
"/votes/{target_name}/{target_id}/{choice}": {
"post": {
"description": "Vote for a specific comment or page",
"description": "Create a new vote for a specific comment or page",
"consumes": [
"application/json"
],
Expand All @@ -3537,36 +3537,45 @@ const docTemplate = `{
"tags": [
"Vote"
],
"summary": "Vote",
"operationId": "Vote",
"summary": "Create Vote",
"operationId": "CreateVote",
"parameters": [
{
"enum": [
"comment_up",
"comment_down",
"page_up",
"page_down"
"comment",
"page"
],
"type": "string",
"description": "The type of vote target",
"name": "type",
"description": "The name of vote target",
"name": "target_name",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Target comment or page ID you want to vote for",
"description": "The target comment or page ID",
"name": "target_id",
"in": "path",
"required": true
},
{
"enum": [
"up",
"down"
],
"type": "string",
"description": "The vote choice",
"name": "choice",
"in": "path",
"required": true
},
{
"description": "The vote data",
"name": "vote",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handler.ParamsVote"
"$ref": "#/definitions/handler.ParamsVoteCreate"
}
}
],
Expand Down Expand Up @@ -4430,7 +4439,7 @@ const docTemplate = `{
}
}
},
"handler.ParamsVote": {
"handler.ParamsVoteCreate": {
"type": "object",
"properties": {
"email": {
Expand Down Expand Up @@ -5405,12 +5414,20 @@ const docTemplate = `{
"type": "object",
"required": [
"down",
"is_down",
"is_up",
"up"
],
"properties": {
"down": {
"type": "integer"
},
"is_down": {
"type": "boolean"
},
"is_up": {
"type": "boolean"
},
"up": {
"type": "integer"
}
Expand Down
43 changes: 30 additions & 13 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3518,9 +3518,9 @@
}
}
},
"/votes/{type}/{target_id}": {
"/votes/{target_name}/{target_id}/{choice}": {
"post": {
"description": "Vote for a specific comment or page",
"description": "Create a new vote for a specific comment or page",
"consumes": [
"application/json"
],
Expand All @@ -3530,36 +3530,45 @@
"tags": [
"Vote"
],
"summary": "Vote",
"operationId": "Vote",
"summary": "Create Vote",
"operationId": "CreateVote",
"parameters": [
{
"enum": [
"comment_up",
"comment_down",
"page_up",
"page_down"
"comment",
"page"
],
"type": "string",
"description": "The type of vote target",
"name": "type",
"description": "The name of vote target",
"name": "target_name",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Target comment or page ID you want to vote for",
"description": "The target comment or page ID",
"name": "target_id",
"in": "path",
"required": true
},
{
"enum": [
"up",
"down"
],
"type": "string",
"description": "The vote choice",
"name": "choice",
"in": "path",
"required": true
},
{
"description": "The vote data",
"name": "vote",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handler.ParamsVote"
"$ref": "#/definitions/handler.ParamsVoteCreate"
}
}
],
Expand Down Expand Up @@ -4423,7 +4432,7 @@
}
}
},
"handler.ParamsVote": {
"handler.ParamsVoteCreate": {
"type": "object",
"properties": {
"email": {
Expand Down Expand Up @@ -5398,12 +5407,20 @@
"type": "object",
"required": [
"down",
"is_down",
"is_up",
"up"
],
"properties": {
"down": {
"type": "integer"
},
"is_down": {
"type": "boolean"
},
"is_up": {
"type": "boolean"
},
"up": {
"type": "integer"
}
Expand Down
38 changes: 25 additions & 13 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ definitions:
- name
- receive_email
type: object
handler.ParamsVote:
handler.ParamsVoteCreate:
properties:
email:
description: The user email
Expand Down Expand Up @@ -1255,10 +1255,16 @@ definitions:
properties:
down:
type: integer
is_down:
type: boolean
is_up:
type: boolean
up:
type: integer
required:
- down
- is_down
- is_up
- up
type: object
info:
Expand Down Expand Up @@ -3278,34 +3284,40 @@ paths:
summary: Get Version Info
tags:
- System
/votes/{type}/{target_id}:
/votes/{target_name}/{target_id}/{choice}:
post:
consumes:
- application/json
description: Vote for a specific comment or page
operationId: Vote
description: Create a new vote for a specific comment or page
operationId: CreateVote
parameters:
- description: The type of vote target
- description: The name of vote target
enum:
- comment_up
- comment_down
- page_up
- page_down
- comment
- page
in: path
name: type
name: target_name
required: true
type: string
- description: Target comment or page ID you want to vote for
- description: The target comment or page ID
in: path
name: target_id
required: true
type: integer
- description: The vote choice
enum:
- up
- down
in: path
name: choice
required: true
type: string
- description: The vote data
in: body
name: vote
required: true
schema:
$ref: '#/definitions/handler.ParamsVote'
$ref: '#/definitions/handler.ParamsVoteCreate'
produces:
- application/json
responses:
Expand Down Expand Up @@ -3340,7 +3352,7 @@ paths:
msg:
type: string
type: object
summary: Vote
summary: Create Vote
tags:
- Vote
/votes/sync:
Expand Down
6 changes: 3 additions & 3 deletions internal/dao/query_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ func (dao *Dao) GetVoteNum(targetID uint, voteType string) int {
return int(num)
}

func (dao *Dao) GetVoteNumUpDown(targetID uint, voteTo string) (int, int) {
func (dao *Dao) GetVoteNumUpDown(targetName string, targetID uint) (int, int) {

Check warning on line 225 in internal/dao/query_find.go

View check run for this annotation

Codecov / codecov/patch

internal/dao/query_find.go#L225

Added line #L225 was not covered by tests
var up int64
var down int64
dao.DB().Model(&entity.Vote{}).Where("target_id = ? AND type = ?", targetID, voteTo+"_up").Count(&up)
dao.DB().Model(&entity.Vote{}).Where("target_id = ? AND type = ?", targetID, voteTo+"_down").Count(&down)
dao.DB().Model(&entity.Vote{}).Where("target_id = ? AND type = ?", targetID, targetName+"_up").Count(&up)
dao.DB().Model(&entity.Vote{}).Where("target_id = ? AND type = ?", targetID, targetName+"_down").Count(&down)

Check warning on line 229 in internal/dao/query_find.go

View check run for this annotation

Codecov / codecov/patch

internal/dao/query_find.go#L228-L229

Added lines #L228 - L229 were not covered by tests
return int(up), int(down)
}

Expand Down
Loading

0 comments on commit 9046996

Please sign in to comment.