2024-03-09 11:06:03 +01:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-03-11 21:08:27 +01:00
|
|
|
type Article struct {
|
|
|
|
Title string
|
|
|
|
Created time.Time
|
|
|
|
Description string
|
|
|
|
Content string
|
|
|
|
Published bool
|
|
|
|
Rejected bool
|
|
|
|
ID int64
|
|
|
|
AuthorID int64
|
|
|
|
}
|
|
|
|
|
2024-03-09 11:06:03 +01:00
|
|
|
func (db *DB) AddArticle(a *Article) (int64, error) {
|
|
|
|
query := `
|
|
|
|
INSERT INTO articles
|
2024-03-10 15:03:46 +01:00
|
|
|
(title, description, content, published, rejected, author_id)
|
2024-03-15 15:18:02 +01:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2024-03-09 11:06:03 +01:00
|
|
|
`
|
|
|
|
|
2024-03-17 09:41:09 +01:00
|
|
|
result, err := db.Exec(query, a.Title, a.Description, a.Content,
|
|
|
|
a.Published, a.Rejected, a.AuthorID)
|
2024-03-09 11:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("error inserting article into DB: %v", err)
|
|
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("error retrieving last ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) GetArticle(id int64) (*Article, error) {
|
|
|
|
query := `
|
|
|
|
SELECT title, created, description, content, published, author_id
|
|
|
|
FROM articles
|
|
|
|
WHERE id = ?
|
|
|
|
`
|
|
|
|
row := db.QueryRow(query, id)
|
|
|
|
|
|
|
|
article := new(Article)
|
|
|
|
var created []byte
|
|
|
|
var err error
|
|
|
|
|
2024-03-10 15:03:46 +01:00
|
|
|
if err := row.Scan(&article.Title, &created, &article.Description,
|
2024-03-09 11:06:03 +01:00
|
|
|
&article.Content, &article.Published, &article.AuthorID); err != nil {
|
|
|
|
return nil, fmt.Errorf("error scanning article row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
article.ID = id
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return article, nil
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:03:46 +01:00
|
|
|
func (db *DB) GetCertainArticles(published, rejected bool) ([]*Article, error) {
|
2024-03-09 11:06:03 +01:00
|
|
|
query := `
|
|
|
|
SELECT id, title, created, description, content, author_id
|
|
|
|
FROM articles
|
|
|
|
WHERE published = ?
|
2024-03-10 15:03:46 +01:00
|
|
|
AND rejected = ?
|
2024-03-09 11:06:03 +01:00
|
|
|
`
|
2024-03-10 15:03:46 +01:00
|
|
|
rows, err := db.Query(query, published, rejected)
|
2024-03-09 11:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error querying articles: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
articleList := make([]*Article, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
article := new(Article)
|
|
|
|
var created []byte
|
|
|
|
|
2024-03-10 15:03:46 +01:00
|
|
|
if err = rows.Scan(&article.ID, &article.Title, &created, &article.Description,
|
2024-03-09 11:06:03 +01:00
|
|
|
&article.Content, &article.AuthorID); 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
articleList = append(articleList, article)
|
|
|
|
}
|
|
|
|
|
|
|
|
return articleList, nil
|
|
|
|
}
|