func(s*CommentService)CreateComment(ctxcontext.Context,trackIDuuid.UUID,userIDuuid.UUID,contentstring,timestampfloat64,parentID*uuid.UUID)(*models.TrackComment,error){// Updated trackID and parentID to uuid.UUID
returncomment,nil// Return comment without user info if preload fails
}
s.logger.Info("Comment created",
zap.Any("comment_id",comment.ID),// Changed to zap.Any for uuid.UUID
zap.Any("track_id",trackID),// Changed to zap.Any for uuid.UUID
zap.String("user_id",userID.String()))
returncomment,nil
}
// GetComments retrieves comments for a track
func(s*CommentService)GetComments(ctxcontext.Context,trackIDuuid.UUID,page,limitint)([]models.TrackComment,int64,error){// Updated trackID to uuid.UUID
varcomments[]models.TrackComment
vartotalint64
offset:=(page-1)*limit
// Count total top-level comments (or all comments? usually top-level for pagination, replies fetched separately or nested)
// Here we fetch all top-level comments
query:=s.db.WithContext(ctx).Model(&models.TrackComment{}).Where("track_id = ? AND parent_id IS NULL",trackID)
iferr:=query.Count(&total).Error;err!=nil{
returnnil,0,err
}
// Fetch comments with user info and replies
// Note: Deep nesting of replies might require recursive query or multiple queries.
// For simplicity, we just preload direct replies or let frontend handle threading if flat list.
// Assuming flat list of top level + preloaded replies?
// Let's just fetch top level and preload their replies one level deep for now
err:=query.
Preload("User").
Preload("Replies").
Preload("Replies.User").
Order("created_at DESC").
Limit(limit).
Offset(offset).
Find(&comments).Error
iferr!=nil{
s.logger.Error("Failed to get comments",zap.Error(err))
returnnil,0,err
}
returncomments,total,nil
}
// UpdateComment updates a comment
func(s*CommentService)UpdateComment(ctxcontext.Context,commentIDuuid.UUID,userIDuuid.UUID,contentstring)(*models.TrackComment,error){// Updated commentID to uuid.UUID
s.logger.Error("Failed to update comment",zap.Error(err))
returnnil,err
}
s.logger.Info("Comment updated",
zap.Any("comment_id",comment.ID),// Changed to zap.Any for uuid.UUID
zap.String("user_id",userID.String()))
return&comment,nil
}
// GetReplies retrieves replies for a given parent comment ID
func(s*CommentService)GetReplies(ctxcontext.Context,parentIDuuid.UUID,page,limitint)([]models.TrackComment,int64,error){// Updated parentID to uuid.UUID