Compare commits

...

4 Commits

4 changed files with 64 additions and 82 deletions

View File

@@ -151,22 +151,8 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
} }
} }
userData := map[string]any{ responseDTO := dto.ToRegistrationResponseDTO(result.User, result.VerificationSent)
"id": result.User.ID, SendCreatedResponse(w, "Registration successful. Check your email to confirm your account.", responseDTO)
"username": result.User.Username,
"email": result.User.Email,
"email_verified": result.User.EmailVerified,
"created_at": result.User.CreatedAt,
"updated_at": result.User.UpdatedAt,
"deleted_at": result.User.DeletedAt,
}
responseData := map[string]any{
"user": userData,
"verification_sent": result.VerificationSent,
}
SendCreatedResponse(w, "Registration successful. Check your email to confirm your account.", responseData)
} }
// @Summary Confirm email address // @Summary Confirm email address
@@ -192,9 +178,7 @@ func (h *AuthHandler) ConfirmEmail(w http.ResponseWriter, r *http.Request) {
} }
userDTO := dto.ToUserDTO(user) userDTO := dto.ToUserDTO(user)
SendSuccessResponse(w, "Email confirmed successfully", map[string]any{ SendSuccessResponse(w, "Email confirmed successfully", userDTO)
"user": userDTO,
})
} }
// @Summary Resend verification email // @Summary Resend verification email
@@ -397,9 +381,7 @@ func (h *AuthHandler) UpdateEmail(w http.ResponseWriter, r *http.Request) {
} }
userDTO := dto.ToUserDTO(user) userDTO := dto.ToUserDTO(user)
SendSuccessResponse(w, "Email updated. Check your inbox to confirm the new address.", map[string]any{ SendSuccessResponse(w, "Email updated. Check your inbox to confirm the new address.", userDTO)
"user": userDTO,
})
} }
// @Summary Update username // @Summary Update username
@@ -445,9 +427,7 @@ func (h *AuthHandler) UpdateUsername(w http.ResponseWriter, r *http.Request) {
} }
userDTO := dto.ToUserDTO(user) userDTO := dto.ToUserDTO(user)
SendSuccessResponse(w, "Username updated successfully.", map[string]any{ SendSuccessResponse(w, "Username updated successfully.", userDTO)
"user": userDTO,
})
} }
// @Summary Update password // @Summary Update password
@@ -498,9 +478,7 @@ func (h *AuthHandler) UpdatePassword(w http.ResponseWriter, r *http.Request) {
} }
userDTO := dto.ToUserDTO(user) userDTO := dto.ToUserDTO(user)
SendSuccessResponse(w, "Password updated successfully.", map[string]any{ SendSuccessResponse(w, "Password updated successfully.", userDTO)
"user": userDTO,
})
} }
// @Summary Request account deletion // @Summary Request account deletion
@@ -565,18 +543,20 @@ func (h *AuthHandler) ConfirmAccountDeletion(w http.ResponseWriter, r *http.Requ
case errors.Is(err, services.ErrEmailSenderUnavailable): case errors.Is(err, services.ErrEmailSenderUnavailable):
SendErrorResponse(w, "Account deletion isn't available right now because email delivery is disabled.", http.StatusServiceUnavailable) SendErrorResponse(w, "Account deletion isn't available right now because email delivery is disabled.", http.StatusServiceUnavailable)
case errors.Is(err, services.ErrDeletionEmailFailed): case errors.Is(err, services.ErrDeletionEmailFailed):
SendSuccessResponse(w, "Your account has been deleted, but we couldn't send the confirmation email.", map[string]any{ responseDTO := dto.AccountDeletionResponseDTO{
"posts_deleted": req.DeletePosts, PostsDeleted: req.DeletePosts,
}) }
SendSuccessResponse(w, "Your account has been deleted, but we couldn't send the confirmation email.", responseDTO)
default: default:
SendErrorResponse(w, "We couldn't confirm the deletion right now.", http.StatusInternalServerError) SendErrorResponse(w, "We couldn't confirm the deletion right now.", http.StatusInternalServerError)
} }
return return
} }
SendSuccessResponse(w, "Your account has been deleted.", map[string]any{ responseDTO := dto.AccountDeletionResponseDTO{
"posts_deleted": req.DeletePosts, PostsDeleted: req.DeletePosts,
}) }
SendSuccessResponse(w, "Your account has been deleted.", responseDTO)
} }
// @Summary Logout user // @Summary Logout user

View File

@@ -64,12 +64,13 @@ func (h *PostHandler) GetPosts(w http.ResponseWriter, r *http.Request) {
} }
postDTOs := dto.ToPostDTOs(posts) postDTOs := dto.ToPostDTOs(posts)
SendSuccessResponse(w, "Posts retrieved successfully", map[string]any{ responseDTO := dto.PostListDTO{
"posts": postDTOs, Posts: postDTOs,
"count": len(postDTOs), Count: len(postDTOs),
"limit": limit, Limit: limit,
"offset": offset, Offset: offset,
}) }
SendSuccessResponse(w, "Posts retrieved successfully", responseDTO)
} }
// @Summary Get a single post // @Summary Get a single post
@@ -230,13 +231,14 @@ func (h *PostHandler) SearchPosts(w http.ResponseWriter, r *http.Request) {
} }
postDTOs := dto.ToPostDTOs(posts) postDTOs := dto.ToPostDTOs(posts)
SendSuccessResponse(w, "Search results retrieved successfully", map[string]any{ responseDTO := dto.SearchPostListDTO{
"posts": postDTOs, Posts: postDTOs,
"count": len(postDTOs), Count: len(postDTOs),
"query": query, Query: query,
"limit": limit, Limit: limit,
"offset": offset, Offset: offset,
}) }
SendSuccessResponse(w, "Search results retrieved successfully", responseDTO)
} }
// @Summary Update a post // @Summary Update a post

View File

@@ -47,13 +47,13 @@ func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
} }
userDTOs := dto.ToSanitizedUserDTOs(users) userDTOs := dto.ToSanitizedUserDTOs(users)
responseDTO := dto.UserListDTO{
SendSuccessResponse(w, "Users retrieved successfully", map[string]any{ Users: userDTOs,
"users": userDTOs, Count: len(userDTOs),
"count": len(userDTOs), Limit: limit,
"limit": limit, Offset: offset,
"offset": offset, }
}) SendSuccessResponse(w, "Users retrieved successfully", responseDTO)
} }
// @Summary Get user // @Summary Get user
@@ -132,10 +132,8 @@ func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
} }
} }
SendCreatedResponse(w, "User created successfully. Verification email sent.", map[string]any{ responseDTO := dto.ToRegistrationResponseDTO(result.User, result.VerificationSent)
"user": result.User, SendCreatedResponse(w, "User created successfully. Verification email sent.", responseDTO)
"verification_sent": result.VerificationSent,
})
} }
// @Summary Get user posts // @Summary Get user posts
@@ -167,12 +165,13 @@ func (h *UserHandler) GetUserPosts(w http.ResponseWriter, r *http.Request) {
} }
postDTOs := dto.ToPostDTOs(posts) postDTOs := dto.ToPostDTOs(posts)
SendSuccessResponse(w, "User posts retrieved successfully", map[string]any{ responseDTO := dto.PostListDTO{
"posts": postDTOs, Posts: postDTOs,
"count": len(postDTOs), Count: len(postDTOs),
"limit": limit, Limit: limit,
"offset": offset, Offset: offset,
}) }
SendSuccessResponse(w, "User posts retrieved successfully", responseDTO)
} }
func (h *UserHandler) MountRoutes(r chi.Router, config RouteModuleConfig) { func (h *UserHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {

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) {