From 376a1264f5c8923d623bd7ce11c5670c61e799a3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 27 Dec 2024 10:52:57 +0100 Subject: [PATCH] Add further support for clicks counter --- cmd/calls/articles.go | 13 ++++++++++++- create_db.sql | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/calls/articles.go b/cmd/calls/articles.go index 1568cdd..449365c 100644 --- a/cmd/calls/articles.go +++ b/cmd/calls/articles.go @@ -50,6 +50,17 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { return } - fmt.Fprint(w, content) + article.Clicks++ + if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: article.ID, AttName: "clicks", Value: article.Clicks}); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if _, err = fmt.Fprint(w, content); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } } diff --git a/create_db.sql b/create_db.sql index 6f136ed..4ad9067 100644 --- a/create_db.sql +++ b/create_db.sql @@ -35,6 +35,7 @@ CREATE TABLE articles ( creator_id INT NOT NULL, issue_id INT NOT NULL, edited_id INT, + clicks INT NOT NULL, is_in_issue BOOL NOT NULL, auto_generated BOOL NOT NULL, PRIMARY KEY (id),