forked from jason/cpolis
Allow articles to be edited
This commit is contained in:
@ -18,6 +18,7 @@ type Article struct {
|
||||
ID int64
|
||||
AuthorID int64
|
||||
IssueID int64
|
||||
EditedID int64
|
||||
EncLength int
|
||||
Published bool
|
||||
Rejected bool
|
||||
@ -31,8 +32,9 @@ 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, is_in_issue, auto_generated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(title, description, link, enc_url, enc_length, enc_type, published,
|
||||
rejected, author_id, issue_id, edited_id, is_in_issue, auto_generated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
for i := 0; i < TxMaxRetries; i++ {
|
||||
@ -51,7 +53,7 @@ func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||
|
||||
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.IsInIssue, a.AutoGenerated)
|
||||
a.EditedID, a.IsInIssue, a.AutoGenerated)
|
||||
if err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
@ -85,7 +87,9 @@ 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, issue_id, is_in_issue, auto_generated
|
||||
SELECT
|
||||
title, created, description, link, enc_url, enc_length, enc_type,
|
||||
published, author_id, issue_id, edited_id, is_in_issue, auto_generated
|
||||
FROM articles
|
||||
WHERE id = ?
|
||||
`
|
||||
@ -97,7 +101,7 @@ 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, &article.IssueID,
|
||||
&article.Published, &article.AuthorID, &article.IssueID, &article.EditedID,
|
||||
&article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
@ -111,14 +115,15 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
return article, nil
|
||||
}
|
||||
|
||||
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, is_in_issue, auto_generated
|
||||
func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, error) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
id, title, created, description, link, enc_url, enc_length, enc_type,
|
||||
author_id, issue_id, published, rejected, is_in_issue, auto_generated
|
||||
FROM articles
|
||||
WHERE published = ?
|
||||
AND rejected = ?
|
||||
`
|
||||
rows, err := db.Query(query, published, rejected)
|
||||
WHERE %s = ?
|
||||
`, attribute)
|
||||
rows, err := db.Query(query, value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error querying articles: %v", err)
|
||||
}
|
||||
@ -130,12 +135,11 @@ 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,
|
||||
&article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
&article.EncType, &article.AuthorID, &article.IssueID, &article.Published,
|
||||
&article.Rejected, &article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
|
||||
article.Published = false
|
||||
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||
@ -152,7 +156,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, auto_generated
|
||||
SELECT
|
||||
id, title, created, description, link, enc_url, enc_length, enc_type,
|
||||
author_id, auto_generated
|
||||
FROM articles
|
||||
WHERE issue_id = ? AND published = true AND is_in_issue = true
|
||||
`
|
||||
|
@ -17,7 +17,7 @@ func GenerateRSS(c *Config, db *DB) (*string, error) {
|
||||
Items: make([]*rss.Item, 0),
|
||||
}
|
||||
|
||||
articles, err := db.GetCertainArticles(true, false)
|
||||
articles, err := db.GetCertainArticles("published", true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting published articles for RSS feed: %v", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user