Articles and tags are now inserted into DB correctly

This commit is contained in:
2024-03-07 15:31:00 +01:00
parent 582f25bec7
commit 4d65be195b
8 changed files with 79 additions and 43 deletions

View File

@ -65,9 +65,6 @@ func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
return
}
r.ParseForm()
article.Tags = append(article.Tags, r.Form["tags"]...)
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
@ -76,7 +73,29 @@ func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
}
article.AuthorID = session.Values["id"].(int64)
db.AddArticle(article)
article.ID, err = db.AddArticle(article)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
r.ParseForm()
tags := make([]int64, 0)
for _, tag := range r.Form["tags"] {
tagID, err := strconv.ParseInt(tag, 10, 64)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tags = append(tags, tagID)
}
if err = db.WriteArticleTags(article.ID, tags); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles("web/templates/hub.html")
tmpl = template.Must(tmpl, err)
@ -135,7 +154,7 @@ func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.Hand
return
}
if err = db.UpdateArticle(id, "published", true); err != nil {
if err = db.UpdateAttribute("articles", id, "published", true); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -160,7 +179,7 @@ func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.Hand
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc,
Content: &rss.Content{Value: article.Content},
Categories: article.Tags,
// Categories: article.Tags,
})
c.Save("tmp/rss.gob")