2024-08-30 15:17:14 +02:00
|
|
|
package calls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-08-30 23:43:01 +02:00
|
|
|
"os"
|
2024-08-30 15:17:14 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
)
|
|
|
|
|
2024-12-27 11:09:36 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-30 15:17:14 +02:00
|
|
|
func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-30 21:20:29 +02:00
|
|
|
if !tokenIsVerified(w, r, c) {
|
|
|
|
return
|
|
|
|
}
|
2024-08-30 15:17:14 +02:00
|
|
|
|
2024-08-30 21:20:29 +02:00
|
|
|
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
|
|
|
|
}
|
2024-08-30 15:17:14 +02:00
|
|
|
|
2024-08-30 21:20:29 +02:00
|
|
|
article, err := db.GetArticle(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-08-30 15:17:14 +02:00
|
|
|
}
|
2024-08-30 21:20:29 +02:00
|
|
|
|
2024-08-30 23:43:01 +02:00
|
|
|
if !article.Published {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-31 01:00:39 +02:00
|
|
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
|
|
|
contentBytes, err := os.ReadFile(articleAbsName)
|
2024-08-30 23:43:01 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := b.ConvertToHTML(string(contentBytes))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-27 11:09:36 +01:00
|
|
|
if err = incrementClicks(db, article); err != nil {
|
2024-12-27 10:52:57 +01:00
|
|
|
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
|
|
|
|
}
|
2024-08-30 15:17:14 +02:00
|
|
|
}
|
|
|
|
}
|
2024-12-27 11:09:36 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|