Skip to content

Commit

Permalink
add lang to likes
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienGasser committed Dec 8, 2024
1 parent ff4f4ce commit 97c924f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS likes(\
id SERIAL PRIMARY KEY, \
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, \
article_id INT NOT NULL, \
lang VARCHAR(255), \
ip VARCHAR(255), \
username VARCHAR(255), \
random_number INT NOT NULL\
Expand Down
3 changes: 2 additions & 1 deletion server/handleLikes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func handleLikeArticle(w http.ResponseWriter, r *http.Request) {
}
vars := mux.Vars(r)
idStr := vars["id"]
lang := getLang(r)
id, err := strconv.Atoi(idStr)
if err != nil {
http.NotFound(w, r)
Expand All @@ -33,7 +34,7 @@ func handleLikeArticle(w http.ResponseWriter, r *http.Request) {
if ip == "" {
ip = r.RemoteAddr
}
likeID, randomNumber, err := likes.Save(id, ip)
likeID, randomNumber, err := likes.Save(id, lang, ip)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
5 changes: 3 additions & 2 deletions server/likes/likes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/jackc/pgx/v5"
)

func Save(articleID int, ip string) (int, int32, error) {
func Save(articleID int, lang string, ip string) (int, int32, error) {
conn, err := db.GetDbConnection()
if err != nil {
fmt.Printf("error while connecting to the DB: %v\n", err)
Expand All @@ -20,10 +20,11 @@ func Save(articleID int, ip string) (int, int32, error) {
randomNumber := rand.Int32()
args := pgx.NamedArgs{
"articleID": articleID,
"lang": lang,
"ip": ip,
"randomNumber": randomNumber,
}
rows, err := conn.Query(context.Background(), "INSERT INTO likes (article_id, ip, random_number) values (@articleID, @ip, @randomNumber) RETURNING id", args)
rows, err := conn.Query(context.Background(), "INSERT INTO likes (article_id, lang, ip, random_number) values (@articleID, @lang, @ip, @randomNumber) RETURNING id", args)
if err != nil {
fmt.Printf("error while inserting like: %v\n", err)
return -1, -1, fmt.Errorf("error while inserting like")
Expand Down

0 comments on commit 97c924f

Please sign in to comment.