Files
crowsnest/internal/model/article.go

48 lines
819 B
Go
Raw Normal View History

2024-12-27 03:25:44 +01:00
package model
import (
2025-01-07 11:59:10 +01:00
"net/url"
"time"
2024-12-27 03:25:44 +01:00
)
2024-12-28 01:40:30 +01:00
// TODO docstring
2024-12-27 03:25:44 +01:00
type Article struct {
2025-01-07 11:59:10 +01:00
Identifier int
SourceUrl string
PublishDate time.Time
FetchDate time.Time
Title string
Content string
2024-12-27 03:25:44 +01:00
}
2025-01-02 16:21:02 +01:00
// TODO docstring
type ArticleViewModel struct {
2025-01-07 11:59:10 +01:00
Title string
PublishDate string
SourceUrl string
ShortSource string
Summary string
2025-01-02 16:21:02 +01:00
}
2024-12-27 22:34:43 +01:00
2025-01-02 16:21:02 +01:00
// TODO docstring
func (a *Article) ViewModel() *ArticleViewModel {
2025-01-07 11:59:10 +01:00
summary := a.Content
if len(a.Content) > 300 {
summary = summary[:300]
}
2025-01-02 16:21:02 +01:00
2025-01-07 11:59:10 +01:00
short_url := ""
2025-01-06 18:45:44 +01:00
parsedURL, err := url.Parse(a.SourceUrl)
2025-01-07 11:59:10 +01:00
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,
}
2024-12-27 03:25:44 +01:00
}