move ai python server into seperate repo
This commit is contained in:
54
src/internal/model/article.go
Normal file
54
src/internal/model/article.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TODO docstring
|
||||
type Article struct {
|
||||
Identifier int
|
||||
SourceUrl string
|
||||
PublishDate time.Time
|
||||
FetchDate time.Time
|
||||
Title string
|
||||
Content string
|
||||
AiSummary string
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
type ArticleViewModel struct {
|
||||
Title string
|
||||
PublishDate string
|
||||
SourceUrl string
|
||||
ShortSource string
|
||||
Summary string
|
||||
AiSummarized bool
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
func (a *Article) ViewModel() *ArticleViewModel {
|
||||
summary := a.AiSummary
|
||||
if summary == "" {
|
||||
if len(a.Content) > 200 {
|
||||
summary = a.Content[:200]
|
||||
} else {
|
||||
summary = a.Content
|
||||
}
|
||||
}
|
||||
|
||||
short_url := ""
|
||||
parsedURL, err := url.Parse(a.SourceUrl)
|
||||
if err == nil {
|
||||
short_url = parsedURL.Hostname()
|
||||
}
|
||||
|
||||
return &ArticleViewModel{
|
||||
Title: a.Title,
|
||||
PublishDate: a.PublishDate.Local().Format("02.01.2006"),
|
||||
SourceUrl: a.SourceUrl,
|
||||
ShortSource: short_url,
|
||||
Summary: summary,
|
||||
AiSummarized: a.AiSummary != "",
|
||||
}
|
||||
}
|
||||
98
src/internal/model/database/articles.go
Normal file
98
src/internal/model/database/articles.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"crowsnest/internal/model"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type ArticleModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Gets all the article objects from the database. This may throw an error if
|
||||
// the connection to the database fails.
|
||||
func (m *ArticleModel) All(limit int) ([]model.Article, error) {
|
||||
stmt := `
|
||||
SELECT id, title, sourceUrl, content, publishDate, fetchDate, aisummary
|
||||
FROM articles
|
||||
ORDER BY publishDate DESC
|
||||
LIMIT $1
|
||||
`
|
||||
rows, err := m.DB.Query(stmt, limit)
|
||||
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.Content, &a.PublishDate, &a.FetchDate, &a.AiSummary)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
articles = append(articles, a)
|
||||
}
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (m *ArticleModel) Search(query string) ([]model.Article, error) {
|
||||
stmt := `
|
||||
SELECT id, title, sourceurl, 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
|
||||
`
|
||||
|
||||
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.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, content, publishDate, fetchDate, aisummary)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`
|
||||
_, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Content, a.PublishDate, a.FetchDate, a.AiSummary)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
func (m *ArticleModel) Update(a *model.Article) error {
|
||||
stmt := `UPDATE articles
|
||||
SET title = $1, sourceUrl = $2, content = $4, publishDate = $5, fetchDate = $6, aisummary = $7
|
||||
WHERE id = $8
|
||||
`
|
||||
_, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Content, a.PublishDate, a.FetchDate, a.AiSummary, a.Identifier)
|
||||
return err
|
||||
}
|
||||
45
src/internal/model/database/connect.go
Normal file
45
src/internal/model/database/connect.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Will try to connect to the database defined by the env variables. If the
|
||||
// connection fails a error will be returned. Ensure that the returned sql.DB
|
||||
// object is closed after use.
|
||||
func DbConnection() (*sql.DB, error) {
|
||||
// collect environement variables
|
||||
dbPass := os.Getenv("DB_PASS")
|
||||
if dbPass == "" {
|
||||
return nil, errors.New("empty env. variable DB_PASS")
|
||||
}
|
||||
dbHost := os.Getenv("DB_HOST")
|
||||
if dbHost == "" {
|
||||
return nil, errors.New("empty env. variable DB_HOST")
|
||||
}
|
||||
dbPort := os.Getenv("DB_PORT")
|
||||
if dbPort == "" {
|
||||
dbPort = "5432"
|
||||
}
|
||||
dbUser := os.Getenv("DB_USER")
|
||||
if dbUser == "" {
|
||||
dbUser = "postgres"
|
||||
}
|
||||
dbName := os.Getenv("DB_NAME")
|
||||
|
||||
// connect to database
|
||||
databaseURL := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable",
|
||||
dbUser, dbPass, dbName, dbHost, dbPort)
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = db.Ping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user