diff --git a/emoji_counter.db b/emoji_counter.db new file mode 100644 index 0000000..cfe9de9 Binary files /dev/null and b/emoji_counter.db differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9ab931a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module log101-blog-services + +go 1.22.3 + +require github.com/mattn/go-sqlite3 v1.14.22 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e8d092a --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go new file mode 100644 index 0000000..98cae30 --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +// main.go +package main + +import ( + "database/sql" + "encoding/json" + "log" + "net/http" + + _ "github.com/mattn/go-sqlite3" +) + +type EmojiClick struct { + PostID string `json:"post_id"` + Emoji string `json:"emoji"` +} + +func main() { + // Connect to the SQLite database + db, err := sql.Open("sqlite3", "./emoji_counter.db") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + // Execute schema SQL to create tables + schema := `CREATE TABLE IF NOT EXISTS emoji_clicks ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + post_id TEXT NOT NULL, + emoji TEXT NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + );` + _, err = db.Exec(schema) + if err != nil { + log.Fatal(err) + } + + // Define the HTTP handler for emoji clicks + http.HandleFunc("/emoji-click", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) + return + } + + var emojiClick EmojiClick + if err := json.NewDecoder(r.Body).Decode(&emojiClick); err != nil { + http.Error(w, "Bad request", http.StatusBadRequest) + return + } + + _, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", emojiClick.PostID, emojiClick.Emoji) + if err != nil { + http.Error(w, "Failed to save emoji click", http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) + }) + + // Define the HTTP handler for getting emoji counts + http.HandleFunc("/emoji-counts", func(w http.ResponseWriter, r *http.Request) { + postID := r.URL.Query().Get("post_id") + if postID == "" { + http.Error(w, "post_id is required", http.StatusBadRequest) + return + } + + rows, err := db.Query("SELECT emoji, COUNT(*) as count FROM emoji_clicks WHERE post_id = ? GROUP BY emoji", postID) + if err != nil { + http.Error(w, "Failed to get emoji counts", http.StatusInternalServerError) + return + } + defer rows.Close() + + emojiCounts := make(map[string]int) + for rows.Next() { + var emoji string + var count int + if err := rows.Scan(&emoji, &count); err != nil { + http.Error(w, "Failed to parse emoji counts", http.StatusInternalServerError) + return + } + emojiCounts[emoji] = count + } + + if err := json.NewEncoder(w).Encode(emojiCounts); err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + return + } + }) + + // Start the HTTP server + log.Println("Starting server on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +} diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..e151ae9 --- /dev/null +++ b/schema.sql @@ -0,0 +1,7 @@ +-- schema.sql +CREATE TABLE IF NOT EXISTS emoji_clicks ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + post_id TEXT NOT NULL, + emoji TEXT NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP +);