frontend
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type newsItem struct {
|
||||
ID int `json:"id"`
|
||||
Ticker string `json:"ticker"`
|
||||
Headline string `json:"headline"`
|
||||
Source string `json:"source"`
|
||||
URL string `json:"url"`
|
||||
Sentiment string `json:"sentiment"`
|
||||
PublishedAt string `json:"published_at"`
|
||||
}
|
||||
|
||||
func (s *Server) handleGetNews(w http.ResponseWriter, r *http.Request) {
|
||||
ticker := r.URL.Query().Get("ticker")
|
||||
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
||||
if ticker != "" {
|
||||
rows, err = s.db.Query(`
|
||||
SELECT id, COALESCE(ticker,''), headline, COALESCE(source,''), COALESCE(url,''), COALESCE(sentiment,''), COALESCE(published_at,'')
|
||||
FROM news WHERE ticker = ? ORDER BY published_at DESC LIMIT 100`, ticker)
|
||||
} else {
|
||||
rows, err = s.db.Query(`
|
||||
SELECT id, COALESCE(ticker,''), headline, COALESCE(source,''), COALESCE(url,''), COALESCE(sentiment,''), COALESCE(published_at,'')
|
||||
FROM news ORDER BY published_at DESC LIMIT 100`)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items := []newsItem{}
|
||||
for rows.Next() {
|
||||
var it newsItem
|
||||
if err := rows.Scan(&it.ID, &it.Ticker, &it.Headline, &it.Source, &it.URL, &it.Sentiment, &it.PublishedAt); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
items = append(items, it)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(items)
|
||||
}
|
||||
Reference in New Issue
Block a user