Files
InsightReply/server/internal/handler/tweet_handler.go
zs d82d59cbe4
All checks were successful
Extension Build & Release / build (push) Successful in 1m32s
Backend Deploy (Go + Docker) / deploy (push) Successful in 1m51s
feat: 扩展弹框配置重构
2026-03-03 15:32:33 +08:00

56 lines
1.8 KiB
Go

package handler
import (
"encoding/json"
"log"
"net/http"
"github.com/zs/InsightReply/internal/repository"
)
type TweetHandler struct {
repo *repository.TweetRepository
}
func NewTweetHandler(repo *repository.TweetRepository) *TweetHandler {
return &TweetHandler{repo: repo}
}
// GetHotTweets returns the top heating tweets spanning across all tracking targets
func (h *TweetHandler) GetHotTweets(w http.ResponseWriter, r *http.Request) {
log.Printf("[TweetHandler] GetHotTweets called from %s", r.RemoteAddr)
tweets, err := h.repo.GetTopHeatingTweets(50)
if err != nil {
log.Printf("[TweetHandler] ERROR GetTopHeatingTweets: %v", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{"error": "failed to retrieve hot tweets"})
return
}
log.Printf("[TweetHandler] GetHotTweets returning %d tweets", len(tweets))
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(tweets)
}
// SearchTweets provides the multi-rule filtering API for Epic 5
func (h *TweetHandler) GetSearchTweets(w http.ResponseWriter, r *http.Request) {
keyword := r.URL.Query().Get("keyword")
handle := r.URL.Query().Get("handle")
log.Printf("[TweetHandler] SearchTweets called: keyword=%q handle=%q", keyword, handle)
tweets, err := h.repo.SearchTweets(keyword, handle, 50)
if err != nil {
log.Printf("[TweetHandler] ERROR SearchTweets: %v", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{"error": "failed to search tweets"})
return
}
log.Printf("[TweetHandler] SearchTweets returning %d tweets", len(tweets))
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(tweets)
}