From 544dddc8933a365c41905cbf3b886adf58e68c6c Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 27 Dec 2024 11:09:36 +0100 Subject: [PATCH] Add a way to get info about clicks --- cmd/calls/articles.go | 49 +++++++++++++++++++++++++++++++++++++++++-- cmd/main.go | 1 + 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cmd/calls/articles.go b/cmd/calls/articles.go index 449365c..4b5d518 100644 --- a/cmd/calls/articles.go +++ b/cmd/calls/articles.go @@ -10,6 +10,27 @@ import ( b "streifling.com/jason/cpolis/cmd/backend" ) +func incrementClicks(db *b.DB, a *b.Article) error { + a.Clicks++ + if err := db.UpdateAttributes(&b.Attribute{Table: "articles", ID: a.ID, AttName: "clicks", Value: a.Clicks}); err != nil { + return fmt.Errorf("error updating click attribute of article %v: %v", a.ID, err) + } + + if a.IsInIssue { + issue, err := db.GetArticle(a.IssueID) + if err != nil { + return fmt.Errorf("error getting issue %v: %v", a.IssueID, err) + } + + issue.Clicks++ + if err := db.UpdateAttributes(&b.Attribute{Table: "articles", ID: issue.ID, AttName: "clicks", Value: issue.Clicks}); err != nil { + return fmt.Errorf("error updating click attribute of issue %v: %v", issue.ID, err) + } + } + + return nil +} + func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !tokenIsVerified(w, r, c) { @@ -50,8 +71,7 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { return } - article.Clicks++ - if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: article.ID, AttName: "clicks", Value: article.Clicks}); err != nil { + if err = incrementClicks(db, article); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -64,3 +84,28 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc { } } } + +func ServeClicks(db *b.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + idString := r.PathValue("id") + id, err := strconv.ParseInt(idString, 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + article, err := db.GetArticle(id) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if _, err = fmt.Fprint(w, article.Clicks); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } +} diff --git a/cmd/main.go b/cmd/main.go index 8a43477..c10ae2f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -65,6 +65,7 @@ func main() { mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, store)) mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewArticle(config, db, store, "publish", "Artikel veröffentlichen", "Veröffentlichen")) mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db)) + mux.HandleFunc("GET /article/serve/{id}/clicks", c.ServeClicks(db)) mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, store)) mux.HandleFunc("GET /atom/serve", c.ServeAtomFeed(config)) mux.HandleFunc("GET /hub", f.ShowHub(config, db, store))