2025-01-06 19:58:01 +01:00
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crowsnest/internal/model"
|
|
|
|
|
"database/sql"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ArticleModel struct {
|
2025-01-07 09:32:57 +01:00
|
|
|
DB *sql.DB
|
2025-01-06 19:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-07 09:32:57 +01:00
|
|
|
// Gets all the article objects from the database. This may throw an error if
|
|
|
|
|
// the connection to the database fails.
|
2025-01-06 19:58:01 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 09:32:57 +01:00
|
|
|
// Will use the full-text search features of the underlying database to search
|
|
|
|
|
// articles for a given search query. This may fail if the connection to the
|
|
|
|
|
// database fails.
|
2025-01-06 19:58:01 +01:00
|
|
|
func (m *ArticleModel) Search(query string) ([]model.Article, error) {
|
|
|
|
|
stmt := `
|
2025-01-07 09:32:57 +01:00
|
|
|
SELECT id, title, sourceurl, author, content, publishdate, fetchDate
|
|
|
|
|
FROM articles
|
|
|
|
|
WHERE fts_vector @@ to_tsquery('german', $1)
|
|
|
|
|
ORDER BY ts_rank(fts_vector, to_tsquery('german', $1)) DESC
|
|
|
|
|
LIMIT 10
|
|
|
|
|
`
|
|
|
|
|
|
2025-01-06 19:58:01 +01:00
|
|
|
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 {
|
|
|
|
|
// insert article
|
|
|
|
|
stmt := `INSERT INTO articles (title, sourceUrl, author, content, publishDate, fetchDate)
|
2025-01-07 09:32:57 +01:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
2025-01-06 19:58:01 +01:00
|
|
|
`
|
2025-01-07 09:32:57 +01:00
|
|
|
_, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Author, a.Content, a.PublishDate, a.FetchDate)
|
2025-01-06 19:58:01 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO docstring
|
|
|
|
|
func (m *ArticleModel) Update(a *model.Article) error {
|
|
|
|
|
stmt := `UPDATE articles
|
2025-01-07 09:32:57 +01:00
|
|
|
SET title = $1, sourceUrl = $2, author = $3, content = $4, publishDate = $5, fetchDate = $6
|
|
|
|
|
WHERE id = $7
|
2025-01-06 19:58:01 +01:00
|
|
|
`
|
2025-01-07 09:32:57 +01:00
|
|
|
_, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Author, a.Content, a.PublishDate, a.FetchDate, a.Identifier)
|
2025-01-06 19:58:01 +01:00
|
|
|
return err
|
|
|
|
|
}
|