+
handleFilterChange(e.target.value)}
+ />
+ {filteredUsers.length > 100 ? (
+
{
+ const user = filteredUsers[index];
+ return (
+
+

+ {user.name}
+
+ );
+ }}
+ />
+ ) : (
+ filteredUsers.map(user => (
+
+

+ {user.name}
+
+ ))
+ )}
+
+ );
+ }
+
+ - input: |
+ // Bad: Inefficient database query
+ func GetUserPosts(db *sql.DB, userID int) ([]Post, error) {
+ var posts []Post
+ rows, err := db.Query("SELECT * FROM posts WHERE user_id = ?", userID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var post Post
+ err := rows.Scan(&post.ID, &post.Title, &post.Content, &post.UserID, &post.CreatedAt)
+ if err != nil {
+ return nil, err
+ }
+
+ // N+1 query problem
+ commentRows, err := db.Query("SELECT * FROM comments WHERE post_id = ?", post.ID)
+ if err != nil {
+ return nil, err
+ }
+ defer commentRows.Close()
+
+ for commentRows.Next() {
+ var comment Comment
+ err := commentRows.Scan(&comment.ID, &comment.Content, &comment.PostID, &comment.UserID)
+ if err != nil {
+ return nil, err
+ }
+ post.Comments = append(post.Comments, comment)
+ }
+
+ posts = append(posts, post)
+ }
+
+ return posts, nil
+ }
+ output: |
+ // Good: Optimized database query
+ func GetUserPosts(db *sql.DB, userID int) ([]Post, error) {
+ // First, fetch all posts in one query
+ var posts []Post
+ postRows, err := db.Query(`
+ SELECT id, title, content, user_id, created_at
+ FROM posts
+ WHERE user_id = ?
+ ORDER BY created_at DESC`,
+ userID)
+ if err != nil {
+ return nil, fmt.Errorf("error querying posts: %w", err)
+ }
+ defer postRows.Close()
+
+ postIDs := []int{}
+ postMap := make(map[int]*Post)
+
+ for postRows.Next() {
+ var post Post
+ err := postRows.Scan(&post.ID, &post.Title, &post.Content, &post.UserID, &post.CreatedAt)
+ if err != nil {
+ return nil, fmt.Errorf("error scanning post: %w", err)
+ }
+ posts = append(posts, post)
+ postIDs = append(postIDs, post.ID)
+ postMap[post.ID] = &posts[len(posts)-1]
+ }
+
+ if len(postIDs) == 0 {
+ return posts, nil
+ }
+
+ // Use a single query with IN clause to fetch all comments for all posts
+ query, args, err := sqlx.In(`
+ SELECT id, content, post_id, user_id
+ FROM comments
+ WHERE post_id IN (?)
+ ORDER BY created_at ASC`,
+ postIDs)
+ if err != nil {
+ return nil, fmt.Errorf("error preparing IN query: %w", err)
+ }
+
+ query = db.Rebind(query)
+ commentRows, err := db.Query(query, args...)
+ if err != nil {
+ return nil, fmt.Errorf("error querying comments: %w", err)
+ }
+ defer commentRows.Close()
+
+ // Populate the comments for each post
+ for commentRows.Next() {
+ var comment Comment
+ var postID int
+ err := commentRows.Scan(&comment.ID, &comment.Content, &postID, &comment.UserID)
+ if err != nil {
+ return nil, fmt.Errorf("error scanning comment: %w", err)
+ }
+ if post, ok := postMap[postID]; ok {
+ post.Comments = append(post.Comments, comment)
+ }
+ }
+
+ return posts, nil
+ }
\ No newline at end of file
diff --git a/.cursor/rules/rule-versioning-strategy.mdc b/.cursor/rules/rule-versioning-strategy.mdc
new file mode 100644
index 0000000..1064290
--- /dev/null
+++ b/.cursor/rules/rule-versioning-strategy.mdc
@@ -0,0 +1,203 @@
+---
+description: Standards for versioning Cursor rules as they evolve over time
+globs: .cursor/rules/*.mdc
+---
+