diff --git a/cmd/backend/articles.go b/cmd/backend/articles.go index 41a32d3..392c882 100644 --- a/cmd/backend/articles.go +++ b/cmd/backend/articles.go @@ -256,3 +256,21 @@ func (db *DB) AddArticleToCurrentIssue(id int64) error { return fmt.Errorf("error: %v unsuccessful retries for DB operation, aborting", TxMaxRetries) } + +func (db *DB) DeleteArticle(id int64) error { + articlesTagsQuery := "DELETE FROM articles_tags WHERE article_id = ?" + + _, err := db.Exec(articlesTagsQuery, id) + if err != nil { + return fmt.Errorf("error deleting article %v from DB: %v", id, err) + } + + articlesQuery := "DELETE FROM articles WHERE id = ?" + + _, err = db.Exec(articlesQuery, id) + if err != nil { + return fmt.Errorf("error deleting article %v from DB: %v", id, err) + } + + return nil +} diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 9393897..b5833fa 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -476,3 +476,114 @@ func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc { json.NewEncoder(w).Encode(url) } } + +func ShowPublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if _, err := getSession(w, r, c, s); err != nil { + return + } + + publishedArticles, err := db.GetCertainArticles(true, false) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl, err := template.ParseFiles(c.WebDir + "/templates/published-articles.html") + tmpl = template.Must(tmpl, err) + tmpl.ExecuteTemplate(w, "page-content", publishedArticles) + } +} + +func ReviewArticleForDeletion(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if _, err := getSession(w, r, c, s); err != nil { + return + } + + type htmlData struct { + Title string + Description string + Content template.HTML + Tags []*b.Tag + ID int64 + } + + var err error + data := new(htmlData) + + data.ID, err = strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + article, err := db.GetArticle(data.ID) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + data.Title, err = b.ConvertToPlain(article.Title) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + data.Description, err = b.ConvertToPlain(article.Description) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + content, err := b.ConvertToHTML(article.Content) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + data.Content = template.HTML(content) + + data.Tags, err = db.GetArticleTags(data.ID) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl, err := template.ParseFiles(c.WebDir + "/templates/to-be-deleted.html") + tmpl = template.Must(tmpl, err) + tmpl.ExecuteTemplate(w, "page-content", data) + } +} + +func DeleteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + session, err := getSession(w, r, c, s) + if err != nil { + return + } + + id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if err = db.DeleteArticle(id); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl = template.Must(tmpl, err) + tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)) + } +} diff --git a/cmd/main.go b/cmd/main.go index c2ba0db..7bb9a61 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,9 +51,10 @@ func main() { mux.HandleFunc("GET /create-tag", f.CreateTag(config, store)) mux.HandleFunc("GET /create-user", f.CreateUser(config, store)) + mux.HandleFunc("GET /delete-article/{id}", f.DeleteArticle(config, db, store)) + mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store)) mux.HandleFunc("GET /edit-self", f.EditSelf(config, db, store)) mux.HandleFunc("GET /edit-user/{id}", f.EditUser(config, db, store)) - mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store)) mux.HandleFunc("GET /hub", f.ShowHub(config, db, store)) mux.HandleFunc("GET /logout", f.Logout(config, store)) mux.HandleFunc("GET /pdf/get-list", c.ServePDFList(config)) @@ -61,8 +62,10 @@ func main() { mux.HandleFunc("GET /pics/{pic}", f.ServeImage(config, store)) mux.HandleFunc("GET /publish-article/{id}", f.PublishArticle(config, db, store)) mux.HandleFunc("GET /publish-issue", f.PublishLatestIssue(config, db, store)) + mux.HandleFunc("GET /published-articles", f.ShowPublishedArticles(config, db, store)) mux.HandleFunc("GET /reject-article/{id}", f.RejectArticle(config, db, store)) mux.HandleFunc("GET /rejected-articles", f.ShowRejectedArticles(config, db, store)) + mux.HandleFunc("GET /review-article-for-deletion/{id}", f.ReviewArticleForDeletion(config, db, store)) mux.HandleFunc("GET /review-rejected-article/{id}", f.ReviewRejectedArticle(config, db, store)) mux.HandleFunc("GET /review-unpublished-article/{id}", f.ReviewUnpublishedArticle(config, db, store)) mux.HandleFunc("GET /rss", c.ServeRSS(config)) diff --git a/web/templates/hub.html b/web/templates/hub.html index c696366..5b29fa7 100644 --- a/web/templates/hub.html +++ b/web/templates/hub.html @@ -31,6 +31,7 @@