update article page #13
This commit is contained in:
@@ -9,10 +9,10 @@
|
||||
<div class="card-title font-medium">{{ .ArticlePageVM.Title }}</div>
|
||||
</div>
|
||||
|
||||
<div class="px-5 pb-4">
|
||||
<div class="px-5 pb-4 grid gap-y-4 grid-cols-1">
|
||||
<p><span class="badge badge-neutral me-4 w-20">Datum</span>{{ .ArticlePageVM.PublishDate }}</p>
|
||||
<p><span class="badge badge-neutral me-4 w-20">Quelle</span>{{ .ArticlePageVM.ShortSource }}</p>
|
||||
<p><span class="badge badge-neutral me-4 w-20">TLDR</span>{{ .ArticlePageVM.AiSummary }}</p>
|
||||
<p><span class="badge badge-neutral me-4 w-20">TLDR</span>{{ .ArticlePageVM.Summary }}</p>
|
||||
<p><span class="badge badge-neutral me-4 w-20">Inhalt</span>{{ .ArticlePageVM.Content }}</p>
|
||||
<div class="card-actions justify-end">
|
||||
<a href="{{ .ArticlePageVM.SourceUrl }}">
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
type App struct {
|
||||
articles *database.ArticleRepository
|
||||
articleVMs *database.ArticleViewModelRepository
|
||||
articlePageVMs *database.ArticlePageViewModelRepository
|
||||
rssItems *database.RSSItemRepository
|
||||
}
|
||||
|
||||
@@ -16,6 +17,7 @@ func NewApp(db *sql.DB) *App {
|
||||
return &App{
|
||||
articles: &database.ArticleRepository{DB: db},
|
||||
articleVMs: &database.ArticleViewModelRepository{DB: db},
|
||||
articlePageVMs: &database.ArticlePageViewModelRepository{DB: db},
|
||||
rssItems: &database.RSSItemRepository{DB: db},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func (app *App) Article(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
// get articles
|
||||
article, err := app.articles.ById(int64(id))
|
||||
articlePageVM, err := app.articlePageVMs.ById(int64(id))
|
||||
if err != nil {
|
||||
// treat as no result
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -32,7 +32,7 @@ func (app *App) Article(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
data := map[string]interface{}{
|
||||
"SelectedNavItemArticle": false,
|
||||
"ArticlePageVM": article.PageViewModel(),
|
||||
"ArticlePageVM": articlePageVM,
|
||||
}
|
||||
err = t.ExecuteTemplate(w, "base", data)
|
||||
if err != nil {
|
||||
|
||||
@@ -39,32 +39,32 @@ type ArticlePageViewModel struct {
|
||||
PublishDate string
|
||||
Title string
|
||||
Content string
|
||||
AiSummary string
|
||||
Summary string
|
||||
}
|
||||
|
||||
// TODO docstring
|
||||
func (a *Article) ViewModel() *ArticleViewModel {
|
||||
var summary string
|
||||
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{
|
||||
Id: a.Id,
|
||||
Title: a.Title,
|
||||
PublishDate: a.PublishDate.Local().Format("02.01.2006"),
|
||||
ShortSource: short_url,
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
//func (a *Article) ViewModel() *ArticleViewModel {
|
||||
// var summary string
|
||||
// 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{
|
||||
// Id: a.Id,
|
||||
// Title: a.Title,
|
||||
// PublishDate: a.PublishDate.Local().Format("02.01.2006"),
|
||||
// ShortSource: short_url,
|
||||
// Summary: summary,
|
||||
// }
|
||||
//}
|
||||
|
||||
func (a *Article) PageViewModel() *ArticlePageViewModel {
|
||||
summary := "N/A"
|
||||
@@ -81,6 +81,6 @@ func (a *Article) PageViewModel() *ArticlePageViewModel {
|
||||
Title: a.Title,
|
||||
PublishDate: a.PublishDate.Local().Format("02.01.2006 15:04"),
|
||||
Content: a.Content,
|
||||
AiSummary: summary,
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"crowsnest/internal/model"
|
||||
"database/sql"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ArticlePageViewModelRepository struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Gets all the view model for the article pages given a article id. This may
|
||||
// throw an error if the connection to the database fails.
|
||||
func (m *ArticlePageViewModelRepository) ById(id int64) (*model.ArticlePageViewModel, error) {
|
||||
stmt := `
|
||||
SELECT a.sourceUrl, a.publishDate, a.title, a.content, d.summary
|
||||
FROM articles a JOIN documents d ON a.document_id = d.id
|
||||
WHERE a.id = $1
|
||||
`
|
||||
|
||||
rows := m.DB.QueryRow(stmt, id)
|
||||
|
||||
a := &model.ArticlePageViewModel{}
|
||||
if err := rows.Scan(&a.SourceUrl, &a.PublishDate, &a.Title, &a.Content, &a.Summary); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// short url
|
||||
parsedURL, err := url.Parse(a.SourceUrl)
|
||||
if err == nil {
|
||||
a.ShortSource = parsedURL.Hostname()
|
||||
} else {
|
||||
a.ShortSource = ""
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
Reference in New Issue
Block a user