Compare commits

..

3 Commits

5 changed files with 56 additions and 53 deletions

View File

@@ -1370,7 +1370,7 @@ const docTemplate = `{
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"$ref": "#/definitions/handlers.VoteRequest" "$ref": "#/definitions/dto.VoteRequest"
} }
} }
], ],
@@ -2018,6 +2018,22 @@ const docTemplate = `{
} }
} }
}, },
"dto.VoteRequest": {
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"enum": [
"up",
"down",
"none"
]
}
}
},
"handlers.APIInfo": { "handlers.APIInfo": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -2150,21 +2166,6 @@ const docTemplate = `{
} }
} }
}, },
"handlers.VoteRequest": {
"description": "Vote request with type field. All votes are handled the same way.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"up",
"down",
"none"
],
"example": "up"
}
}
},
"handlers.VoteResponse": { "handlers.VoteResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -1367,7 +1367,7 @@
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"$ref": "#/definitions/handlers.VoteRequest" "$ref": "#/definitions/dto.VoteRequest"
} }
} }
], ],
@@ -2015,6 +2015,22 @@
} }
} }
}, },
"dto.VoteRequest": {
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"enum": [
"up",
"down",
"none"
]
}
}
},
"handlers.APIInfo": { "handlers.APIInfo": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -2147,21 +2163,6 @@
} }
} }
}, },
"handlers.VoteRequest": {
"description": "Vote request with type field. All votes are handled the same way.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"up",
"down",
"none"
],
"example": "up"
}
}
},
"handlers.VoteResponse": { "handlers.VoteResponse": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -140,6 +140,17 @@ definitions:
required: required:
- username - username
type: object type: object
dto.VoteRequest:
properties:
type:
enum:
- up
- down
- none
type: string
required:
- type
type: object
handlers.APIInfo: handlers.APIInfo:
properties: properties:
data: {} data: {}
@@ -230,17 +241,6 @@ definitions:
success: success:
type: boolean type: boolean
type: object type: object
handlers.VoteRequest:
description: Vote request with type field. All votes are handled the same way.
properties:
type:
enum:
- up
- down
- none
example: up
type: string
type: object
handlers.VoteResponse: handlers.VoteResponse:
properties: properties:
data: {} data: {}
@@ -1121,7 +1121,7 @@ paths:
name: request name: request
required: true required: true
schema: schema:
$ref: '#/definitions/handlers.VoteRequest' $ref: '#/definitions/dto.VoteRequest'
produces: produces:
- application/json - application/json
responses: responses:

View File

@@ -6,6 +6,10 @@ import (
"goyco/internal/database" "goyco/internal/database"
) )
type VoteRequest struct {
Type string `json:"type" validate:"required,oneof=up down none"`
}
type VoteDTO struct { type VoteDTO struct {
ID uint `json:"id"` ID uint `json:"id"`
UserID *uint `json:"user_id,omitempty"` UserID *uint `json:"user_id,omitempty"`

View File

@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"goyco/internal/database" "goyco/internal/database"
"goyco/internal/dto"
"goyco/internal/services" "goyco/internal/services"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
@@ -39,11 +40,6 @@ func NewVoteHandler(voteService *services.VoteService) *VoteHandler {
} }
} }
// @Description Vote request with type field. All votes are handled the same way.
type VoteRequest struct {
Type string `json:"type" example:"up" enums:"up,down,none" description:"Vote type: 'up' for upvote, 'down' for downvote, 'none' to remove vote"`
}
type VoteResponse = CommonResponse type VoteResponse = CommonResponse
// @Summary Cast a vote on a post // @Summary Cast a vote on a post
@@ -62,7 +58,7 @@ type VoteResponse = CommonResponse
// @Produce json // @Produce json
// @Security BearerAuth // @Security BearerAuth
// @Param id path int true "Post ID" // @Param id path int true "Post ID"
// @Param request body VoteRequest true "Vote data (type: 'up', 'down', or 'none' to remove)" // @Param request body dto.VoteRequest true "Vote data (type: 'up', 'down', or 'none' to remove)"
// @Success 200 {object} VoteResponse "Vote cast successfully with updated post statistics" // @Success 200 {object} VoteResponse "Vote cast successfully with updated post statistics"
// @Failure 401 {object} VoteResponse "Authentication required" // @Failure 401 {object} VoteResponse "Authentication required"
// @Failure 400 {object} VoteResponse "Invalid request data or vote type" // @Failure 400 {object} VoteResponse "Invalid request data or vote type"
@@ -82,8 +78,9 @@ func (h *VoteHandler) CastVote(w http.ResponseWriter, r *http.Request) {
return return
} }
var req VoteRequest req, ok := GetValidatedDTO[dto.VoteRequest](r)
if !DecodeJSONRequest(w, r, &req) { if !ok {
SendErrorResponse(w, "Invalid request", http.StatusBadRequest)
return return
} }
@@ -286,7 +283,7 @@ func (h *VoteHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {
protected = config.GeneralRateLimit(protected) protected = config.GeneralRateLimit(protected)
} }
protected.Post("/posts/{id}/vote", h.CastVote) protected.Post("/posts/{id}/vote", WithValidation[dto.VoteRequest](config.ValidationMiddleware, h.CastVote))
protected.Delete("/posts/{id}/vote", h.RemoveVote) protected.Delete("/posts/{id}/vote", h.RemoveVote)
protected.Get("/posts/{id}/vote", h.GetUserVote) protected.Get("/posts/{id}/vote", h.GetUserVote)
protected.Get("/posts/{id}/votes", h.GetPostVotes) protected.Get("/posts/{id}/votes", h.GetPostVotes)