{{ .Title }}
{{ .ShortSource }} {{ .PublishDate }} + {{if .AiSummarized}} + ai summary + {{end}}
{{ .Summary }}
Link diff --git a/cmd/frontend/Index.go b/cmd/frontend/Index.go index 63c501b..633f6b5 100644 --- a/cmd/frontend/Index.go +++ b/cmd/frontend/Index.go @@ -9,7 +9,7 @@ import ( // List the latest articles using the base template. func (app *App) Index(w http.ResponseWriter, req *http.Request) { // get articles - articles, err := app.articles.All(10) + articles, err := app.articles.All(30) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/internal/model/article.go b/internal/model/article.go index b1f631c..2170468 100644 --- a/internal/model/article.go +++ b/internal/model/article.go @@ -13,22 +13,28 @@ type Article struct { FetchDate time.Time Title string Content string + AiSummary string } // TODO docstring type ArticleViewModel struct { - Title string - PublishDate string - SourceUrl string - ShortSource string - Summary string + Title string + PublishDate string + SourceUrl string + ShortSource string + Summary string + AiSummarized bool } // TODO docstring func (a *Article) ViewModel() *ArticleViewModel { - summary := a.Content - if len(a.Content) > 300 { - summary = summary[:300] + summary := a.AiSummary + if summary == "" { + if len(a.Content) > 200 { + summary = a.Content[:200] + } else { + summary = a.Content + } } short_url := "" @@ -38,10 +44,11 @@ func (a *Article) ViewModel() *ArticleViewModel { } return &ArticleViewModel{ - Title: a.Title, - PublishDate: a.PublishDate.Local().Format("02.01.2006"), - SourceUrl: a.SourceUrl, - ShortSource: short_url, - Summary: summary, + Title: a.Title, + PublishDate: a.PublishDate.Local().Format("02.01.2006"), + SourceUrl: a.SourceUrl, + ShortSource: short_url, + Summary: summary, + AiSummarized: a.AiSummary != "", } } diff --git a/internal/model/database/articles.go b/internal/model/database/articles.go index 2ce6f84..34d78d1 100644 --- a/internal/model/database/articles.go +++ b/internal/model/database/articles.go @@ -13,7 +13,7 @@ type ArticleModel struct { // the connection to the database fails. func (m *ArticleModel) All(limit int) ([]model.Article, error) { stmt := ` - SELECT id, title, sourceUrl, content, publishDate, fetchDate + SELECT id, title, sourceUrl, content, publishDate, fetchDate, aisummary FROM articles ORDER BY publishDate DESC LIMIT $1 @@ -26,7 +26,7 @@ func (m *ArticleModel) All(limit int) ([]model.Article, error) { articles := []model.Article{} for rows.Next() { a := model.Article{} - err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate) + err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate, &a.AiSummary) if err != nil { return nil, err } @@ -80,19 +80,19 @@ func (m *ArticleModel) Search(query string) ([]model.Article, error) { // query fails. func (m *ArticleModel) Insert(a *model.Article) error { // insert article - stmt := `INSERT INTO articles (title, sourceUrl, content, publishDate, fetchDate) - VALUES ($1, $2, $3, $4, $5) + 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) + _, 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 - WHERE id = $7 + 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.Identifier) + _, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Content, a.PublishDate, a.FetchDate, a.AiSummary, a.Identifier) return err }