2024-05-17 16:01:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-28 19:19:30 +00:00
|
|
|
"database/sql"
|
2024-05-29 14:55:49 +00:00
|
|
|
"errors"
|
2024-05-28 19:19:30 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-05-28 17:17:30 +00:00
|
|
|
"net/http"
|
2024-05-28 19:19:30 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-sql-driver/mysql"
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-28 17:17:30 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-05-17 16:01:33 +00:00
|
|
|
)
|
|
|
|
|
2024-05-28 19:19:30 +00:00
|
|
|
var db *sql.DB
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Emoji counts for each post are retrieved in this format
|
2024-05-29 07:25:59 +00:00
|
|
|
type EmojiCount struct {
|
|
|
|
Emoji string
|
|
|
|
TotalCount int
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Emojis' order should be like this
|
|
|
|
var emojis = [6]string{"👍", "👎", "😀", "😑", "🤢", "👀"}
|
|
|
|
|
|
|
|
// Get the emoji counts foe a given post id
|
|
|
|
func countEmojis(postId string) ([]EmojiCount, error) {
|
|
|
|
emojiList := []EmojiCount{}
|
|
|
|
// This will hold the total counts for each emoji
|
|
|
|
// for a given post
|
|
|
|
var emojiCounter = map[string]int{
|
|
|
|
"👍": 0,
|
|
|
|
"👎": 0,
|
|
|
|
"😀": 0,
|
|
|
|
"😑": 0,
|
|
|
|
"🤢": 0,
|
|
|
|
"👀": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the emoji counts for each emoji
|
|
|
|
rows, err := db.Query("SELECT emoji, COUNT(*) FROM emoji_clicks WHERE post_id = ? GROUP BY emoji;", postId)
|
|
|
|
if err != nil {
|
|
|
|
return emojiList, errors.New("couldn't get emoji counts")
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
// Read the rows and update the counter
|
|
|
|
for rows.Next() {
|
|
|
|
var ec EmojiCount
|
|
|
|
if err := rows.Scan(&ec.Emoji, &ec.TotalCount); err != nil {
|
|
|
|
return emojiList, errors.New("DB results couldn't read")
|
|
|
|
}
|
|
|
|
emojiCounter[ec.Emoji] = ec.TotalCount
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emojis are returned as an ordered list
|
|
|
|
// as maps are randomly iterated
|
|
|
|
for _, emoji := range emojis {
|
|
|
|
var count = EmojiCount{emoji, emojiCounter[emoji]}
|
|
|
|
emojiList = append(emojiList, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
return emojiList, nil
|
|
|
|
}
|
|
|
|
|
2024-05-17 16:01:33 +00:00
|
|
|
func main() {
|
2024-05-28 19:19:30 +00:00
|
|
|
cfg := mysql.Config{
|
|
|
|
User: os.Getenv("DBUSER"),
|
|
|
|
Passwd: os.Getenv("DBPASS"),
|
|
|
|
Net: "tcp",
|
|
|
|
Addr: "127.0.0.1:3306",
|
|
|
|
DBName: "emojis",
|
|
|
|
}
|
|
|
|
// Get a database handle.
|
|
|
|
var err error
|
|
|
|
db, err = sql.Open("mysql", cfg.FormatDSN())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Check db connection
|
2024-05-28 19:19:30 +00:00
|
|
|
pingErr := db.Ping()
|
|
|
|
if pingErr != nil {
|
|
|
|
log.Fatal(pingErr)
|
|
|
|
}
|
|
|
|
fmt.Println("Connected!")
|
|
|
|
|
2024-05-28 17:17:30 +00:00
|
|
|
r := gin.Default()
|
|
|
|
r.LoadHTMLGlob("templates/*")
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// CORS configuration
|
2024-05-28 17:17:30 +00:00
|
|
|
corsConfig := cors.DefaultConfig()
|
|
|
|
corsConfig.AllowOrigins = []string{"*"}
|
|
|
|
corsConfig.AllowHeaders = []string{"hx-current-url", "hx-request"}
|
|
|
|
r.Use(cors.New(corsConfig))
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Get the emoji form, you must provide postId query parameter
|
2024-05-28 17:17:30 +00:00
|
|
|
r.GET("/forms/emoji", func(c *gin.Context) {
|
2024-05-29 07:25:59 +00:00
|
|
|
postId := c.Query("postId")
|
2024-05-29 08:04:10 +00:00
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// get emoji counts for each emoji
|
|
|
|
emojiCounter, err := countEmojis(postId)
|
2024-05-29 07:25:59 +00:00
|
|
|
if err != nil {
|
2024-05-29 14:55:49 +00:00
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId})
|
2024-05-29 07:25:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Return the html template
|
2024-05-28 19:19:30 +00:00
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{
|
2024-05-29 07:25:59 +00:00
|
|
|
"postId": c.Query("postId"),
|
2024-05-29 14:55:49 +00:00
|
|
|
"results": emojiCounter,
|
2024-05-28 19:19:30 +00:00
|
|
|
})
|
2024-05-28 17:17:30 +00:00
|
|
|
})
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Update the emoji counter, this handler will
|
|
|
|
// add a new entry to the database with anonymized ip
|
|
|
|
// then sums the results to get the emoji counter
|
2024-05-28 17:17:30 +00:00
|
|
|
r.POST("/forms/emoji/post", func(c *gin.Context) {
|
2024-05-29 07:25:59 +00:00
|
|
|
postId := c.PostForm("postId")
|
2024-05-28 19:19:30 +00:00
|
|
|
emoji := c.PostForm("emojiInput")
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Check if parameters are missing
|
2024-05-29 07:25:59 +00:00
|
|
|
if postId == "" || emoji == "" {
|
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters", "postId": postId})
|
2024-05-28 19:19:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Add the new emoji entry to the database
|
2024-05-29 07:25:59 +00:00
|
|
|
_, err := db.Exec("INSERT INTO emoji_clicks (post_id, emoji) VALUES (?, ?)", postId, emoji)
|
2024-05-28 19:19:30 +00:00
|
|
|
if err != nil {
|
2024-05-29 07:25:59 +00:00
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database", "postId": postId})
|
2024-05-28 19:19:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// get emoji counts for each emoji
|
|
|
|
emojiCounter, err := countEmojis(postId)
|
2024-05-28 19:19:30 +00:00
|
|
|
if err != nil {
|
2024-05-29 14:55:49 +00:00
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts", "postId": postId})
|
2024-05-28 19:19:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-29 14:55:49 +00:00
|
|
|
// Return the html with the updated emoji counter
|
|
|
|
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"postId": postId, "results": emojiCounter})
|
2024-05-28 17:17:30 +00:00
|
|
|
})
|
2024-05-17 16:01:33 +00:00
|
|
|
|
2024-05-29 07:25:59 +00:00
|
|
|
r.Run(":3001")
|
2024-05-17 16:01:33 +00:00
|
|
|
}
|