Files
crowsnest/internal/model/article.go

59 lines
1.1 KiB
Go
Raw Normal View History

2024-12-27 03:25:44 +01:00
package model
import (
"time"
2025-01-06 18:45:44 +01:00
//"strings"
"net/url"
2024-12-27 03:25:44 +01:00
)
2025-01-02 16:21:02 +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-03 01:00:06 +01:00
Identifier int
2024-12-27 22:34:43 +01:00
SourceUrl string
PublishDate time.Time
FetchDate time.Time
Title string
Content string
Author string
2024-12-27 03:25:44 +01:00
}
2025-01-02 16:21:02 +01:00
// TODO docstring
type ArticleViewModel struct {
Title string
Author string
PublishDate string
SourceUrl string
2025-01-06 18:45:44 +01:00
ShortSource string
2025-01-02 16:21:02 +01:00
Summary string
}
2024-12-27 22:34:43 +01:00
2025-01-02 16:21:02 +01:00
// TODO docstring
func (a *Article) ViewModel() *ArticleViewModel {
summary := a.Content
if len(a.Content) > 300 {
summary = summary[:300]
}
2025-01-06 18:45:44 +01:00
short_url := ""
parsedURL, err := url.Parse(a.SourceUrl)
if err == nil {
short_url = parsedURL.Hostname()
//hostParts := strings.Split(short_url, ".")
//if len(hostParts) >= 2 {
// short_url = strings.Join(hostParts[len(hostParts)-2:], ".")
//}
}
2025-01-02 16:21:02 +01:00
return &ArticleViewModel{
Title: a.Title,
Author: a.Author,
2025-01-06 18:45:44 +01:00
PublishDate: a.PublishDate.Local().Format("02.01.2006"),
2025-01-02 16:21:02 +01:00
SourceUrl: a.SourceUrl,
2025-01-06 18:45:44 +01:00
ShortSource: short_url,
2025-01-02 16:21:02 +01:00
Summary: summary,
}
2024-12-27 03:25:44 +01:00
}