refactor: use DTOs instead of manual maps in vote responses

This commit is contained in:
2026-01-10 22:34:29 +01:00
parent 2d58c15031
commit 00ef0c236e

View File

@@ -213,22 +213,26 @@ func (h *VoteHandler) GetUserVote(w http.ResponseWriter, r *http.Request) {
vote, err := h.voteService.GetUserVote(userID, postID, ipAddress, userAgent) vote, err := h.voteService.GetUserVote(userID, postID, ipAddress, userAgent)
if err != nil { if err != nil {
if err.Error() == "record not found" { if err.Error() == "record not found" {
SendSuccessResponse(w, "No vote found", map[string]any{ responseDTO := dto.VoteResponseDTO{
"has_vote": false, HasVote: false,
"vote": nil, Vote: nil,
"is_anonymous": false, IsAnonymous: false,
}) }
SendSuccessResponse(w, "No vote found", responseDTO)
return return
} }
SendErrorResponse(w, "Internal server error", http.StatusInternalServerError) SendErrorResponse(w, "Internal server error", http.StatusInternalServerError)
return return
} }
SendSuccessResponse(w, "Vote retrieved successfully", map[string]any{ voteDTO := dto.ToVoteDTO(vote)
"has_vote": true, isAnonymous := vote.UserID == nil
"vote": vote, responseDTO := dto.VoteResponseDTO{
"is_anonymous": false, HasVote: true,
}) Vote: &voteDTO,
IsAnonymous: isAnonymous,
}
SendSuccessResponse(w, "Vote retrieved successfully", responseDTO)
} }
// @Summary Get post votes // @Summary Get post votes
@@ -263,15 +267,12 @@ func (h *VoteHandler) GetPostVotes(w http.ResponseWriter, r *http.Request) {
return return
} }
allVotes := make([]any, 0, len(votes)) voteDTOs := dto.ToVoteDTOs(votes)
for _, vote := range votes { responseDTO := dto.VoteListDTO{
allVotes = append(allVotes, vote) Votes: voteDTOs,
Count: len(voteDTOs),
} }
SendSuccessResponse(w, "Votes retrieved successfully", responseDTO)
SendSuccessResponse(w, "Votes retrieved successfully", map[string]any{
"votes": allVotes,
"count": len(allVotes),
})
} }
func (h *VoteHandler) MountRoutes(r chi.Router, config RouteModuleConfig) { func (h *VoteHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {