Files
crowsnest/src/internal/model/article.go
2025-01-22 09:48:25 +01:00

87 lines
1.6 KiB
Go

package model
import (
"net/url"
"time"
)
type Article struct {
Id int
SourceUrl string
PublishDate time.Time
FetchDate time.Time
Title string
Content string
}
func (a *Article) Clone() *Article {
return &Article{
Id: a.Id,
SourceUrl: a.SourceUrl,
PublishDate: a.PublishDate,
FetchDate: a.FetchDate,
Title: a.Title,
Content: a.Content,
}
}
type ArticleViewModel struct {
Id int
Title string
PublishDate string
ShortSource string
Summary string
}
type ArticlePageViewModel struct {
SourceUrl string
ShortSource string
PublishDate string
Title string
Content 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) PageViewModel() *ArticlePageViewModel {
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,
Summary: summary,
}
}