diff --git a/cmd/backend/articles.go b/cmd/backend/articles.go index fc47ec7..fa00771 100644 --- a/cmd/backend/articles.go +++ b/cmd/backend/articles.go @@ -9,18 +9,20 @@ import ( ) type Article struct { - Title string - Created time.Time - Description string - Link string - EncURL string - EncLength int - EncType string - Published bool - Rejected bool - ID int64 - AuthorID int64 - IssueID int64 + Title string + Created time.Time + Description string + Link string + EncURL string + EncLength int + EncType string + Published bool + Rejected bool + ID int64 + AuthorID int64 + IssueID int64 + IsInIssue bool + AutoGenerated bool } func (db *DB) AddArticle(a *Article) (int64, error) { @@ -29,8 +31,8 @@ func (db *DB) AddArticle(a *Article) (int64, error) { selectQuery := "SELECT id FROM issues WHERE published = false" insertQuery := ` INSERT INTO articles - (title, description, link, enc_url, enc_length, enc_type, published, rejected, author_id, issue_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + (title, description, link, enc_url, enc_length, enc_type, published, rejected, author_id, issue_id, is_in_issue, auto_generated) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ` for i := 0; i < TxMaxRetries; i++ { @@ -47,8 +49,10 @@ func (db *DB) AddArticle(a *Article) (int64, error) { return 0, fmt.Errorf("error getting issue ID when adding article to DB: %v", err) } + fmt.Println(a) result, err := tx.Exec(insertQuery, a.Title, a.Description, a.Link, - a.EncURL, a.EncLength, a.EncType, a.Published, a.Rejected, a.AuthorID, id) + a.EncURL, a.EncLength, a.EncType, a.Published, a.Rejected, a.AuthorID, id, + a.IsInIssue, a.AutoGenerated) if err != nil { if rollbackErr := tx.Rollback(); rollbackErr != nil { log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr) @@ -82,7 +86,7 @@ func (db *DB) AddArticle(a *Article) (int64, error) { func (db *DB) GetArticle(id int64) (*Article, error) { query := ` - SELECT title, created, description, link, enc_url, enc_length, enc_type, published, author_id + SELECT title, created, description, link, enc_url, enc_length, enc_type, published, author_id, issue_id, is_in_issue, auto_generated FROM articles WHERE id = ? ` @@ -94,7 +98,8 @@ func (db *DB) GetArticle(id int64) (*Article, error) { if err := row.Scan(&article.Title, &created, &article.Description, &article.Link, &article.EncURL, &article.EncLength, &article.EncType, - &article.Published, &article.AuthorID); err != nil { + &article.Published, &article.AuthorID, &article.IssueID, + &article.IsInIssue, &article.AutoGenerated); err != nil { return nil, fmt.Errorf("error scanning article row: %v", err) } @@ -109,7 +114,7 @@ func (db *DB) GetArticle(id int64) (*Article, error) { func (db *DB) GetCertainArticles(published, rejected bool) ([]*Article, error) { query := ` - SELECT id, title, created, description, link, enc_url, enc_length, enc_type, author_id, issue_id + SELECT id, title, created, description, link, enc_url, enc_length, enc_type, author_id, issue_id, is_in_issue, auto_generated FROM articles WHERE published = ? AND rejected = ? @@ -126,7 +131,8 @@ func (db *DB) GetCertainArticles(published, rejected bool) ([]*Article, error) { if err = rows.Scan(&article.ID, &article.Title, &created, &article.Description, &article.Link, &article.EncURL, &article.EncLength, - &article.EncType, &article.AuthorID, &article.IssueID); err != nil { + &article.EncType, &article.AuthorID, &article.IssueID, + &article.IsInIssue, &article.AutoGenerated); err != nil { return nil, fmt.Errorf("error scanning article row: %v", err) } @@ -147,9 +153,9 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) { txOptions := &sql.TxOptions{Isolation: sql.LevelSerializable} issueQuery := "SELECT id FROM issues WHERE published = false" articlesQuery := ` - SELECT id, title, created, description, link, enc_url, enc_length, enc_type, author_id + SELECT id, title, created, description, link, enc_url, enc_length, enc_type, author_id, auto_generated FROM articles - WHERE issue_id = ? AND published = true + WHERE issue_id = ? AND published = true AND is_in_issue = true ` for i := 0; i < TxMaxRetries; i++ { @@ -182,7 +188,7 @@ func (db *DB) GetCurrentIssueArticles() ([]*Article, error) { if err = rows.Scan(&article.ID, &article.Title, &created, &article.Description, &article.Link, &article.EncURL, &article.EncLength, - &article.EncType, &article.AuthorID); err != nil { + &article.EncType, &article.AuthorID, &article.AutoGenerated); err != nil { if rollbackErr := tx.Rollback(); rollbackErr != nil { log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr) } diff --git a/cmd/backend/rss.go b/cmd/backend/rss.go index 35927d2..e79a73c 100644 --- a/cmd/backend/rss.go +++ b/cmd/backend/rss.go @@ -31,7 +31,10 @@ func GenerateRSS(c *Config, db *DB) (*string, error) { for _, tag := range tags { tagNames = append(tagNames, tag.Name) } - tagNames = append(tagNames, fmt.Sprint("Orient Express ", article.IssueID)) + + if article.IsInIssue { + tagNames = append(tagNames, fmt.Sprint("Orient Express ", article.IssueID)) + } user, err := db.GetUser(article.AuthorID) if err != nil { @@ -57,9 +60,8 @@ func GenerateRSS(c *Config, db *DB) (*string, error) { PubDate: article.Created.Format(time.RFC1123Z), Title: articleTitle, } - fmt.Println(article.Link, ": ", len(article.Link)) - if article.Title == "Autogenerated cpolis Issue Article" { + if article.AutoGenerated { item.Enclosure = &rss.Enclosure{ Url: article.EncURL, Lenght: article.EncLength, diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 0b926c4..d788cd3 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -73,11 +73,13 @@ func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { } article := &b.Article{ - Title: r.PostFormValue("article-title"), - Description: r.PostFormValue("article-description"), - Published: false, - Rejected: false, - AuthorID: session.Values["id"].(int64), + Title: r.PostFormValue("article-title"), + Description: r.PostFormValue("article-description"), + Published: false, + Rejected: false, + AuthorID: session.Values["id"].(int64), + IsInIssue: r.PostFormValue("issue") == "on", + AutoGenerated: false, } article.ID, err = db.AddArticle(article) @@ -153,6 +155,7 @@ func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { &b.Attribute{Table: "articles", ID: id, AttName: "title", Value: title}, &b.Attribute{Table: "articles", ID: id, AttName: "description", Value: description}, &b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false}, + &b.Attribute{Table: "articles", ID: id, AttName: "is_in_issue", Value: r.PostFormValue("issue") == "on"}, ); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -240,46 +243,27 @@ func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Hand return } - type htmlData struct { - Title string - Description string - Content template.HTML - Tags []*b.Tag - ID int64 - } + data := new(struct { + Article *b.Article + Content template.HTML + Tags []*b.Tag + }) - var err error - data := new(htmlData) - - data.ID, err = strconv.ParseInt(r.PathValue("id"), 10, 64) + 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) + data.Article, err = db.GetArticle(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 - } - - articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md") + articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.ID, ".md") contentBytes, err := os.ReadFile(articleAbsName) if err != nil { log.Println(err) @@ -295,7 +279,7 @@ func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Hand } data.Content = template.HTML(content) - data.Tags, err = db.GetArticleTags(data.ID) + data.Tags, err = db.GetArticleTags(data.Article.ID) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -314,13 +298,12 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler return } - type htmlData struct { + data := new(struct { Selected map[int64]bool Article *b.Article Content string Tags []*b.Tag - } - data := new(htmlData) + }) id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) if err != nil { @@ -378,13 +361,20 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { } id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + article, err := db.GetArticle(id) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } - if err = db.AddArticleToCurrentIssue(id); err != nil { + if err = db.AddArticleToCurrentIssue(article.ID); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -523,7 +513,7 @@ func ShowPublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler filteredArticles := make([]*b.Article, 0) for _, article := range publishedArticles { - if article.Title != "Autogenerated cpolis Issue Article" { + if !article.AutoGenerated { filteredArticles = append(filteredArticles, article) } } diff --git a/cmd/frontend/issues.go b/cmd/frontend/issues.go index d641d41..47e32ae 100644 --- a/cmd/frontend/issues.go +++ b/cmd/frontend/issues.go @@ -59,14 +59,15 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFun mimeType := mime.TypeByExtension(filepath.Ext(imgAbsName)) article := &b.Article{ - Title: "Autogenerated cpolis Issue Article", - EncURL: fmt.Sprint(c.Domain, "/image/serve/", imgFileName), - EncLength: int(imgSize), - EncType: mimeType, - Published: true, - Rejected: false, - Created: time.Now(), - AuthorID: session.Values["id"].(int64), + Title: r.PostFormValue("issue-title"), + EncURL: fmt.Sprint(c.Domain, "/image/serve/", imgFileName), + EncLength: int(imgSize), + EncType: mimeType, + Published: true, + Rejected: false, + Created: time.Now(), + AuthorID: session.Values["id"].(int64), + AutoGenerated: true, } fmt.Println(article.Link) diff --git a/create_db.sql b/create_db.sql index 23eb491..5cde280 100644 --- a/create_db.sql +++ b/create_db.sql @@ -21,18 +21,20 @@ CREATE TABLE issues ( ); CREATE TABLE articles ( - id INT AUTO_INCREMENT, - title VARCHAR(255) NOT NULL, - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - description TEXT NOT NULL, - link VARCHAR(255), - enc_url VARCHAR(255), - enc_length INT, - enc_type VARCHAR(255), - published BOOL NOT NULL, - rejected BOOL NOT NULL, - author_id INT NOT NULL, - issue_id INT NOT NULL, + id INT AUTO_INCREMENT, + title VARCHAR(255) NOT NULL, + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + description TEXT NOT NULL, + link VARCHAR(255), + enc_url VARCHAR(255), + enc_length INT, + enc_type VARCHAR(255), + published BOOL NOT NULL, + rejected BOOL NOT NULL, + author_id INT NOT NULL, + issue_id INT NOT NULL, + is_in_issue BOOL NOT NULL, + auto_generated BOOL NOT NULL, PRIMARY KEY (id), FOREIGN KEY (author_id) REFERENCES users (id), FOREIGN KEY (issue_id) REFERENCES issues (id) diff --git a/web/templates/current-articles.html b/web/templates/current-articles.html index fc58ba5..7719c00 100644 --- a/web/templates/current-articles.html +++ b/web/templates/current-articles.html @@ -17,7 +17,18 @@

Cover

- +
+ + +
+
+ +
+

Titel

+
+ +
diff --git a/web/templates/editor.html b/web/templates/editor.html index ebbabbf..1db7de4 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -21,6 +21,11 @@
Tags
+
+ + +
+ {{range .Tags}}
diff --git a/web/templates/rework-article.html b/web/templates/rework-article.html index 5029144..29cded9 100644 --- a/web/templates/rework-article.html +++ b/web/templates/rework-article.html @@ -21,6 +21,11 @@
Tags
+
+ + +
+ {{range .Tags}}
Titel
- {{.Title}} + {{.Article.Title}}
Beschreibung
- {{.Description}} + {{.Article.Description}}
Artikel @@ -21,16 +21,21 @@ Tags
+ {{if .Article.IsInIssue}} + Orient Express +
+ {{end}} {{range .Tags}} - {{.Name}} + {{.Name}}
{{end}}
- + -