move from file storage to sqlite3
This commit is contained in:
@@ -2,13 +2,12 @@ package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
|
||||
// TODO docstring
|
||||
type Article struct {
|
||||
Identifier int
|
||||
SourceUrl string
|
||||
PublishDate time.Time
|
||||
FetchDate time.Time
|
||||
@@ -42,13 +41,3 @@ func (a *Article) ViewModel() *ArticleViewModel {
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --- implement IIdentifiable interface ---
|
||||
|
||||
// Generates a hash based on the source url of the article. Can be used to
|
||||
// identify the article.
|
||||
func (article *Article) Id() string {
|
||||
hash := sha256.Sum256([]byte(article.SourceUrl))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
87
internal/model/sqlite/articles.go
Normal file
87
internal/model/sqlite/articles.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"crowsnest/internal/model"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// TODO docstring
|
||||
type ArticleModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
func (m *ArticleModel) All() ([]model.Article, error) {
|
||||
stmt := `
|
||||
SELECT id, title, sourceUrl, author, content, publishDate, fetchDate
|
||||
FROM articles
|
||||
ORDER BY publishDate DESC
|
||||
`
|
||||
rows, err := m.DB.Query(stmt)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
articles := []model.Article{}
|
||||
for rows.Next() {
|
||||
a := model.Article{}
|
||||
err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Author, &a.Content, &a.PublishDate, &a.FetchDate)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
articles = append(articles, a)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil { return nil, err }
|
||||
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
func (m *ArticleModel) Search(query string) ([]model.Article, error) {
|
||||
stmt := `
|
||||
SELECT id, title, sourceUrl, author, content, publishDate, fetchDate
|
||||
FROM articles JOIN (
|
||||
SELECT id as id2, rank FROM fts_articles WHERE content MATCH ?
|
||||
) ON id = id2
|
||||
ORDER BY rank ASC, publishDate DESC
|
||||
LIMIT 10
|
||||
`
|
||||
rows, err := m.DB.Query(stmt, query)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
articles := []model.Article{}
|
||||
for rows.Next() {
|
||||
a := model.Article{}
|
||||
err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Author, &a.Content, &a.PublishDate, &a.FetchDate)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
articles = append(articles, a)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil { return nil, err }
|
||||
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// Inserts a new article into the database. The id attribute of the given
|
||||
// article will be ignored. May throw an error if the execution of the database
|
||||
// query fails.
|
||||
func (m *ArticleModel) Insert(a *model.Article) error {
|
||||
// begin transaction
|
||||
_, err := m.DB.Begin()
|
||||
if err != nil { return err }
|
||||
|
||||
// insert article
|
||||
stmt := `INSERT INTO articles (title, sourceUrl, author, content, publishDate, fetchDate)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
result, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Author, a.Content, a.PublishDate, a.FetchDate)
|
||||
if err != nil { return err }
|
||||
lastId, err := result.LastInsertId()
|
||||
if err != nil { return err }
|
||||
|
||||
// insert into fts_articles for full-text search
|
||||
stmt = `INSERT INTO fts_articles (id, content)
|
||||
VALUES (?, ? || '\n' || ? || '\n' || ?)
|
||||
`
|
||||
_, err = m.DB.Exec(stmt, lastId, a.Title, a.Author, a.Content)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user