Add a way to get info about clicks
This commit is contained in:
parent
523bdb24cd
commit
544dddc893
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user