Compare commits
4 Commits
53da1eee2a
...
ef4a05f8a5
| Author | SHA1 | Date | |
|---|---|---|---|
| ef4a05f8a5 | |||
| 00ef0c236e | |||
| 2d58c15031 | |||
| 523dac242e |
@@ -151,22 +151,8 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
userData := map[string]any{
|
||||
"id": result.User.ID,
|
||||
"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)
|
||||
responseDTO := dto.ToRegistrationResponseDTO(result.User, result.VerificationSent)
|
||||
SendCreatedResponse(w, "Registration successful. Check your email to confirm your account.", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Confirm email address
|
||||
@@ -192,9 +178,7 @@ func (h *AuthHandler) ConfirmEmail(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
userDTO := dto.ToUserDTO(user)
|
||||
SendSuccessResponse(w, "Email confirmed successfully", map[string]any{
|
||||
"user": userDTO,
|
||||
})
|
||||
SendSuccessResponse(w, "Email confirmed successfully", userDTO)
|
||||
}
|
||||
|
||||
// @Summary Resend verification email
|
||||
@@ -397,9 +381,7 @@ func (h *AuthHandler) UpdateEmail(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
userDTO := dto.ToUserDTO(user)
|
||||
SendSuccessResponse(w, "Email updated. Check your inbox to confirm the new address.", map[string]any{
|
||||
"user": userDTO,
|
||||
})
|
||||
SendSuccessResponse(w, "Email updated. Check your inbox to confirm the new address.", userDTO)
|
||||
}
|
||||
|
||||
// @Summary Update username
|
||||
@@ -445,9 +427,7 @@ func (h *AuthHandler) UpdateUsername(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
userDTO := dto.ToUserDTO(user)
|
||||
SendSuccessResponse(w, "Username updated successfully.", map[string]any{
|
||||
"user": userDTO,
|
||||
})
|
||||
SendSuccessResponse(w, "Username updated successfully.", userDTO)
|
||||
}
|
||||
|
||||
// @Summary Update password
|
||||
@@ -498,9 +478,7 @@ func (h *AuthHandler) UpdatePassword(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
userDTO := dto.ToUserDTO(user)
|
||||
SendSuccessResponse(w, "Password updated successfully.", map[string]any{
|
||||
"user": userDTO,
|
||||
})
|
||||
SendSuccessResponse(w, "Password updated successfully.", userDTO)
|
||||
}
|
||||
|
||||
// @Summary Request account deletion
|
||||
@@ -565,18 +543,20 @@ func (h *AuthHandler) ConfirmAccountDeletion(w http.ResponseWriter, r *http.Requ
|
||||
case errors.Is(err, services.ErrEmailSenderUnavailable):
|
||||
SendErrorResponse(w, "Account deletion isn't available right now because email delivery is disabled.", http.StatusServiceUnavailable)
|
||||
case errors.Is(err, services.ErrDeletionEmailFailed):
|
||||
SendSuccessResponse(w, "Your account has been deleted, but we couldn't send the confirmation email.", map[string]any{
|
||||
"posts_deleted": req.DeletePosts,
|
||||
})
|
||||
responseDTO := dto.AccountDeletionResponseDTO{
|
||||
PostsDeleted: req.DeletePosts,
|
||||
}
|
||||
SendSuccessResponse(w, "Your account has been deleted, but we couldn't send the confirmation email.", responseDTO)
|
||||
default:
|
||||
SendErrorResponse(w, "We couldn't confirm the deletion right now.", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Your account has been deleted.", map[string]any{
|
||||
"posts_deleted": req.DeletePosts,
|
||||
})
|
||||
responseDTO := dto.AccountDeletionResponseDTO{
|
||||
PostsDeleted: req.DeletePosts,
|
||||
}
|
||||
SendSuccessResponse(w, "Your account has been deleted.", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Logout user
|
||||
|
||||
@@ -64,12 +64,13 @@ func (h *PostHandler) GetPosts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
postDTOs := dto.ToPostDTOs(posts)
|
||||
SendSuccessResponse(w, "Posts retrieved successfully", map[string]any{
|
||||
"posts": postDTOs,
|
||||
"count": len(postDTOs),
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
responseDTO := dto.PostListDTO{
|
||||
Posts: postDTOs,
|
||||
Count: len(postDTOs),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
SendSuccessResponse(w, "Posts retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Get a single post
|
||||
@@ -230,13 +231,14 @@ func (h *PostHandler) SearchPosts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
postDTOs := dto.ToPostDTOs(posts)
|
||||
SendSuccessResponse(w, "Search results retrieved successfully", map[string]any{
|
||||
"posts": postDTOs,
|
||||
"count": len(postDTOs),
|
||||
"query": query,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
responseDTO := dto.SearchPostListDTO{
|
||||
Posts: postDTOs,
|
||||
Count: len(postDTOs),
|
||||
Query: query,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
SendSuccessResponse(w, "Search results retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Update a post
|
||||
|
||||
@@ -47,13 +47,13 @@ func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
userDTOs := dto.ToSanitizedUserDTOs(users)
|
||||
|
||||
SendSuccessResponse(w, "Users retrieved successfully", map[string]any{
|
||||
"users": userDTOs,
|
||||
"count": len(userDTOs),
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
responseDTO := dto.UserListDTO{
|
||||
Users: userDTOs,
|
||||
Count: len(userDTOs),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
SendSuccessResponse(w, "Users retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
// @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{
|
||||
"user": result.User,
|
||||
"verification_sent": result.VerificationSent,
|
||||
})
|
||||
responseDTO := dto.ToRegistrationResponseDTO(result.User, result.VerificationSent)
|
||||
SendCreatedResponse(w, "User created successfully. Verification email sent.", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Get user posts
|
||||
@@ -167,12 +165,13 @@ func (h *UserHandler) GetUserPosts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
postDTOs := dto.ToPostDTOs(posts)
|
||||
SendSuccessResponse(w, "User posts retrieved successfully", map[string]any{
|
||||
"posts": postDTOs,
|
||||
"count": len(postDTOs),
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
responseDTO := dto.PostListDTO{
|
||||
Posts: postDTOs,
|
||||
Count: len(postDTOs),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
SendSuccessResponse(w, "User posts retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
func (h *UserHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {
|
||||
|
||||
@@ -213,22 +213,26 @@ func (h *VoteHandler) GetUserVote(w http.ResponseWriter, r *http.Request) {
|
||||
vote, err := h.voteService.GetUserVote(userID, postID, ipAddress, userAgent)
|
||||
if err != nil {
|
||||
if err.Error() == "record not found" {
|
||||
SendSuccessResponse(w, "No vote found", map[string]any{
|
||||
"has_vote": false,
|
||||
"vote": nil,
|
||||
"is_anonymous": false,
|
||||
})
|
||||
responseDTO := dto.VoteResponseDTO{
|
||||
HasVote: false,
|
||||
Vote: nil,
|
||||
IsAnonymous: false,
|
||||
}
|
||||
SendSuccessResponse(w, "No vote found", responseDTO)
|
||||
return
|
||||
}
|
||||
SendErrorResponse(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Vote retrieved successfully", map[string]any{
|
||||
"has_vote": true,
|
||||
"vote": vote,
|
||||
"is_anonymous": false,
|
||||
})
|
||||
voteDTO := dto.ToVoteDTO(vote)
|
||||
isAnonymous := vote.UserID == nil
|
||||
responseDTO := dto.VoteResponseDTO{
|
||||
HasVote: true,
|
||||
Vote: &voteDTO,
|
||||
IsAnonymous: isAnonymous,
|
||||
}
|
||||
SendSuccessResponse(w, "Vote retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
// @Summary Get post votes
|
||||
@@ -263,15 +267,12 @@ func (h *VoteHandler) GetPostVotes(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
allVotes := make([]any, 0, len(votes))
|
||||
for _, vote := range votes {
|
||||
allVotes = append(allVotes, vote)
|
||||
voteDTOs := dto.ToVoteDTOs(votes)
|
||||
responseDTO := dto.VoteListDTO{
|
||||
Votes: voteDTOs,
|
||||
Count: len(voteDTOs),
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Votes retrieved successfully", map[string]any{
|
||||
"votes": allVotes,
|
||||
"count": len(allVotes),
|
||||
})
|
||||
SendSuccessResponse(w, "Votes retrieved successfully", responseDTO)
|
||||
}
|
||||
|
||||
func (h *VoteHandler) MountRoutes(r chi.Router, config RouteModuleConfig) {
|
||||
|
||||
Reference in New Issue
Block a user