Added ability to reject and rework article
This commit is contained in:
@ -8,12 +8,13 @@ import (
|
||||
func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||
query := `
|
||||
INSERT INTO articles
|
||||
(title, description, content, published, author_id)
|
||||
(title, description, content, published, rejected, author_id)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?)
|
||||
(?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
result, err := db.Exec(query, a.Title, a.Desc, a.Content, a.Published, a.AuthorID)
|
||||
result, err := db.Exec(query, a.Title, a.Description, a.Content, a.Published,
|
||||
a.Rejected, a.AuthorID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error inserting article into DB: %v", err)
|
||||
}
|
||||
@ -37,7 +38,7 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
var created []byte
|
||||
var err error
|
||||
|
||||
if err := row.Scan(&article.Title, &created, &article.Desc,
|
||||
if err := row.Scan(&article.Title, &created, &article.Description,
|
||||
&article.Content, &article.Published, &article.AuthorID); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
@ -51,13 +52,14 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
return article, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetCertainArticles(published bool) ([]*Article, error) {
|
||||
func (db *DB) GetCertainArticles(published, rejected bool) ([]*Article, error) {
|
||||
query := `
|
||||
SELECT id, title, created, description, content, author_id
|
||||
FROM articles
|
||||
WHERE published = ?
|
||||
AND rejected = ?
|
||||
`
|
||||
rows, err := db.Query(query, published)
|
||||
rows, err := db.Query(query, published, rejected)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error querying articles: %v", err)
|
||||
}
|
||||
@ -67,7 +69,7 @@ func (db *DB) GetCertainArticles(published bool) ([]*Article, error) {
|
||||
article := new(Article)
|
||||
var created []byte
|
||||
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.Desc,
|
||||
if err = rows.Scan(&article.ID, &article.Title, &created, &article.Description,
|
||||
&article.Content, &article.AuthorID); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -79,14 +80,28 @@ func OpenDB(dbName string) (*DB, error) {
|
||||
return &db, nil
|
||||
}
|
||||
|
||||
func (db *DB) UpdateAttribute(table string, id int64, attribute string, val interface{}) error {
|
||||
query := fmt.Sprintf(`
|
||||
func (db *DB) UpdateAttributes(a ...*Attribute) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error starting transaction: %v", err)
|
||||
}
|
||||
|
||||
for _, attribute := range a {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE %s
|
||||
SET %s = ?
|
||||
WHERE id = ?
|
||||
`, table, attribute)
|
||||
if _, err := db.Exec(query, val, id); err != nil {
|
||||
return fmt.Errorf("error updating article in DB: %v", err)
|
||||
`, attribute.Table, attribute.AttName)
|
||||
if _, err := tx.Exec(query, attribute.Value, attribute.ID); err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
}
|
||||
return fmt.Errorf("error updating article in DB: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return fmt.Errorf("error committing transaction: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -11,12 +11,11 @@ const (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
UserName string
|
||||
FirstName string
|
||||
LastName string
|
||||
RejectedArticles []*Article
|
||||
ID int64
|
||||
Role int
|
||||
UserName string
|
||||
FirstName string
|
||||
LastName string
|
||||
ID int64
|
||||
Role int
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
@ -25,11 +24,19 @@ type Tag struct {
|
||||
}
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
Created time.Time
|
||||
Desc string
|
||||
Content string
|
||||
Published bool
|
||||
ID int64
|
||||
AuthorID int64
|
||||
Title string
|
||||
Created time.Time
|
||||
Description string
|
||||
Content string
|
||||
Published bool
|
||||
Rejected bool
|
||||
ID int64
|
||||
AuthorID int64
|
||||
}
|
||||
|
||||
type Attribute struct {
|
||||
Value interface{}
|
||||
Table string
|
||||
AttName string
|
||||
ID int64
|
||||
}
|
||||
|
Reference in New Issue
Block a user