add single article page

This commit is contained in:
2025-01-12 19:57:54 +01:00
parent de32cef530
commit 9104bc7716
7 changed files with 132 additions and 9 deletions

View File

@@ -14,7 +14,9 @@
{{end}} {{end}}
</p> </p>
<p class="card-text">{{ .Summary }}</p> <p class="card-text">{{ .Summary }}</p>
<a href="{{ .SourceUrl }}" class="link">Link</a> <div class="flex flex-row-reverse">
<a href="/article/{{ .Id }}" class="btn btn-active btn-sm btn-primary">Details</a>
</div>
</div> </div>
</div> </div>
{{ end }} {{ end }}

View File

@@ -0,0 +1,27 @@
{{ define "content" }}
<div class="content max-w-screen-lg flex flex-col mx-auto">
<div tabindex="0" class="card bg-base-200 shadow mb-4">
<div class="card-body">
<div class="flex flex-row pb-4">
<div class="divider divider-horizontal divider-primary"></div>
<div class="card-title font-medium">{{ .ArticlePageVM.Title }}</div>
</div>
<div class="px-5 pb-4">
<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">Inhalt</span>{{ .ArticlePageVM.Content }}</p>
<div class="card-actions justify-end">
<a href="{{ .ArticlePageVM.SourceUrl }}">
<button class="btn btn-primary btn-sm">Seite besuchen</button>
</a>
</div>
</div>
</div>
</div>
</div>
{{ end }}

View File

@@ -20,7 +20,7 @@
<div class="flex-1"> <div class="flex-1">
<a href="/" tabindex="0" class="btn btn-ghost text-xl" up-follow up-target=".body">crowsnest</a> <a href="/" tabindex="0" class="btn btn-ghost text-xl" up-follow up-target=".body">crowsnest</a>
<ul class="menu menu-horizontal hidden sm:flex"> <ul class="menu menu-horizontal hidden sm:flex">
<li><a tabindex="0" {{ if .SelectedNavItemArticle }} class="active" {{ end }}>Artikel</a></li> <li><a href="/" tabindex="0" {{ if .SelectedNavItemArticle }} class="active" {{ end }}>Artikel</a></li>
<li><a tabindex="0">Themen</a></li> <li><a tabindex="0">Themen</a></li>
</ul> </ul>
</div> </div>
@@ -47,7 +47,7 @@
</svg> </svg>
</div> </div>
<ul class="menu dropdown-content bg-base-100 rounded-box z-[1] mt-3 w-52 p-2 shadow"> <ul class="menu dropdown-content bg-base-100 rounded-box z-[1] mt-3 w-52 p-2 shadow">
<li><a tabindex="0" class="active">Artikel</a></li> <li><a href="/" tabindex="0" class="active">Artikel</a></li>
<li><a tabindex="0">Themen</a></li> <li><a tabindex="0">Themen</a></li>
</ul> </ul>
</div> </div>

View File

@@ -0,0 +1,42 @@
package app
import (
"html/template"
"net/http"
"strconv"
)
// Enpoint that returns a list of articles given search terms in the post
// request of a search form. Uses the content template.
func (app *App) Article(w http.ResponseWriter, req *http.Request) {
// get id
id, err := strconv.ParseUint(req.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, req)
return
}
// get articles
article, err := app.articles.ById(int(id))
if err != nil {
// treat as no result
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// render template
t := template.Must(template.ParseFiles(
"assets/templates/articlePage.html",
"assets/templates/layout.html",
))
data := map[string]interface{}{
"SelectedNavItemArticle": false,
"ArticlePageVM": article.PageViewModel(),
}
err = t.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
return
}
}

View File

@@ -24,6 +24,8 @@ func (app *App) Routes() http.Handler {
mux.Handle("GET /page/{id}", http.HandlerFunc(app.Index)) mux.Handle("GET /page/{id}", http.HandlerFunc(app.Index))
mux.Handle("POST /up/search", http.HandlerFunc(app.UpSearch)) mux.Handle("POST /up/search", http.HandlerFunc(app.UpSearch))
mux.Handle("GET /article/{id}", http.HandlerFunc(app.Article))
// serve files from the "static" directory // serve files from the "static" directory
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static")))) mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static"))))

View File

@@ -7,7 +7,7 @@ import (
// TODO docstring // TODO docstring
type Article struct { type Article struct {
Identifier int Id int
SourceUrl string SourceUrl string
PublishDate time.Time PublishDate time.Time
FetchDate time.Time FetchDate time.Time
@@ -18,14 +18,23 @@ type Article struct {
// TODO docstring // TODO docstring
type ArticleViewModel struct { type ArticleViewModel struct {
Id int
Title string Title string
PublishDate string PublishDate string
SourceUrl string
ShortSource string ShortSource string
Summary string Summary string
AiSummarized bool AiSummarized bool
} }
type ArticlePageViewModel struct {
SourceUrl string
ShortSource string
PublishDate string
Title string
Content string
AiSummary string
}
// TODO docstring // TODO docstring
func (a *Article) ViewModel() *ArticleViewModel { func (a *Article) ViewModel() *ArticleViewModel {
summary := a.AiSummary summary := a.AiSummary
@@ -44,11 +53,33 @@ func (a *Article) ViewModel() *ArticleViewModel {
} }
return &ArticleViewModel{ return &ArticleViewModel{
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"),
SourceUrl: a.SourceUrl,
ShortSource: short_url, ShortSource: short_url,
Summary: summary, Summary: summary,
AiSummarized: a.AiSummary != "", AiSummarized: a.AiSummary != "",
} }
} }
func (a *Article) PageViewModel() *ArticlePageViewModel {
summary := a.AiSummary
if summary == "" {
summary = "N/A"
}
short_url := ""
parsedURL, err := url.Parse(a.SourceUrl)
if err == nil {
short_url = parsedURL.Hostname()
}
return &ArticlePageViewModel{
SourceUrl: a.SourceUrl,
ShortSource: short_url,
Title: a.Title,
PublishDate: a.PublishDate.Local().Format("02.01.2006 15:04"),
Content: a.Content,
AiSummary: summary,
}
}

View File

@@ -27,7 +27,7 @@ func (m *ArticleModel) All(limit int, offset int) ([]model.Article, error) {
articles := []model.Article{} articles := []model.Article{}
for rows.Next() { for rows.Next() {
a := model.Article{} a := model.Article{}
err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate, &a.AiSummary) err := rows.Scan(&a.Id, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate, &a.AiSummary)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -78,7 +78,7 @@ func (m *ArticleModel) Search(query string) ([]model.Article, error) {
articles := []model.Article{} articles := []model.Article{}
for rows.Next() { for rows.Next() {
a := model.Article{} a := model.Article{}
err := rows.Scan(&a.Identifier, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate) err := rows.Scan(&a.Id, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -92,6 +92,25 @@ func (m *ArticleModel) Search(query string) ([]model.Article, error) {
return articles, nil return articles, nil
} }
// Will return an article given an id. This may fail if the connection to the
// database fails or there is no aritcle with the given id.
func (m *ArticleModel) ById(id int) (*model.Article, error) {
stmt := `
SELECT a.id, a.title, a.sourceurl, a.content, a.publishdate, a.fetchDate
FROM articles a
WHERE a.id = $1
`
rows := m.DB.QueryRow(stmt, id)
a := &model.Article{}
if err := rows.Scan(&a.Id, &a.Title, &a.SourceUrl, &a.Content, &a.PublishDate, &a.FetchDate); err != nil {
return nil, err
}
return a, nil
}
// Inserts a new article into the database. The id attribute of the given // 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 // article will be ignored. May throw an error if the execution of the database
// query fails. // query fails.
@@ -110,6 +129,6 @@ func (m *ArticleModel) Update(a *model.Article) error {
SET title = $1, sourceUrl = $2, content = $4, publishDate = $5, fetchDate = $6, aisummary = $7 SET title = $1, sourceUrl = $2, content = $4, publishDate = $5, fetchDate = $6, aisummary = $7
WHERE id = $8 WHERE id = $8
` `
_, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Content, a.PublishDate, a.FetchDate, a.AiSummary, a.Identifier) _, err := m.DB.Exec(stmt, a.Title, a.SourceUrl, a.Content, a.PublishDate, a.FetchDate, a.AiSummary, a.Id)
return err return err
} }