update article page #13
This commit is contained in:
@@ -9,10 +9,10 @@
|
|||||||
<div class="card-title font-medium">{{ .ArticlePageVM.Title }}</div>
|
<div class="card-title font-medium">{{ .ArticlePageVM.Title }}</div>
|
||||||
</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">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">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>
|
<p><span class="badge badge-neutral me-4 w-20">Inhalt</span>{{ .ArticlePageVM.Content }}</p>
|
||||||
<div class="card-actions justify-end">
|
<div class="card-actions justify-end">
|
||||||
<a href="{{ .ArticlePageVM.SourceUrl }}">
|
<a href="{{ .ArticlePageVM.SourceUrl }}">
|
||||||
|
|||||||
@@ -7,16 +7,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
articles *database.ArticleRepository
|
articles *database.ArticleRepository
|
||||||
articleVMs *database.ArticleViewModelRepository
|
articleVMs *database.ArticleViewModelRepository
|
||||||
rssItems *database.RSSItemRepository
|
articlePageVMs *database.ArticlePageViewModelRepository
|
||||||
|
rssItems *database.RSSItemRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(db *sql.DB) *App {
|
func NewApp(db *sql.DB) *App {
|
||||||
return &App{
|
return &App{
|
||||||
articles: &database.ArticleRepository{DB: db},
|
articles: &database.ArticleRepository{DB: db},
|
||||||
articleVMs: &database.ArticleViewModelRepository{DB: db},
|
articleVMs: &database.ArticleViewModelRepository{DB: db},
|
||||||
rssItems: &database.RSSItemRepository{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
|
// get articles
|
||||||
article, err := app.articles.ById(int64(id))
|
articlePageVM, err := app.articlePageVMs.ById(int64(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// treat as no result
|
// treat as no result
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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{}{
|
data := map[string]interface{}{
|
||||||
"SelectedNavItemArticle": false,
|
"SelectedNavItemArticle": false,
|
||||||
"ArticlePageVM": article.PageViewModel(),
|
"ArticlePageVM": articlePageVM,
|
||||||
}
|
}
|
||||||
err = t.ExecuteTemplate(w, "base", data)
|
err = t.ExecuteTemplate(w, "base", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -39,32 +39,32 @@ type ArticlePageViewModel struct {
|
|||||||
PublishDate string
|
PublishDate string
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
AiSummary string
|
Summary string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO docstring
|
// TODO docstring
|
||||||
func (a *Article) ViewModel() *ArticleViewModel {
|
//func (a *Article) ViewModel() *ArticleViewModel {
|
||||||
var summary string
|
// var summary string
|
||||||
if len(a.Content) > 200 {
|
// if len(a.Content) > 200 {
|
||||||
summary = a.Content[:200]
|
// summary = a.Content[:200]
|
||||||
} else {
|
// } else {
|
||||||
summary = a.Content
|
// summary = a.Content
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
short_url := ""
|
// short_url := ""
|
||||||
parsedURL, err := url.Parse(a.SourceUrl)
|
// parsedURL, err := url.Parse(a.SourceUrl)
|
||||||
if err == nil {
|
// if err == nil {
|
||||||
short_url = parsedURL.Hostname()
|
// short_url = parsedURL.Hostname()
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return &ArticleViewModel{
|
// return &ArticleViewModel{
|
||||||
Id: a.Id,
|
// Id: a.Id,
|
||||||
Title: a.Title,
|
// Title: a.Title,
|
||||||
PublishDate: a.PublishDate.Local().Format("02.01.2006"),
|
// PublishDate: a.PublishDate.Local().Format("02.01.2006"),
|
||||||
ShortSource: short_url,
|
// ShortSource: short_url,
|
||||||
Summary: summary,
|
// Summary: summary,
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
func (a *Article) PageViewModel() *ArticlePageViewModel {
|
func (a *Article) PageViewModel() *ArticlePageViewModel {
|
||||||
summary := "N/A"
|
summary := "N/A"
|
||||||
@@ -81,6 +81,6 @@ func (a *Article) PageViewModel() *ArticlePageViewModel {
|
|||||||
Title: a.Title,
|
Title: a.Title,
|
||||||
PublishDate: a.PublishDate.Local().Format("02.01.2006 15:04"),
|
PublishDate: a.PublishDate.Local().Format("02.01.2006 15:04"),
|
||||||
Content: a.Content,
|
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