55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
)
|
|
|
|
|
|
// TODO docstring
|
|
type Article struct {
|
|
SourceUrl string
|
|
PublishDate time.Time
|
|
FetchDate time.Time
|
|
Title string
|
|
Content string
|
|
Author string
|
|
}
|
|
|
|
// TODO docstring
|
|
type ArticleViewModel struct {
|
|
Title string
|
|
Author string
|
|
PublishDate string
|
|
SourceUrl string
|
|
Summary string
|
|
}
|
|
|
|
|
|
// TODO docstring
|
|
func (a *Article) ViewModel() *ArticleViewModel {
|
|
summary := a.Content
|
|
if len(a.Content) > 300 {
|
|
summary = summary[:300]
|
|
}
|
|
|
|
return &ArticleViewModel{
|
|
Title: a.Title,
|
|
Author: a.Author,
|
|
PublishDate: a.PublishDate.Local().Format("02.01.2006 03:04"),
|
|
SourceUrl: a.SourceUrl,
|
|
Summary: summary,
|
|
}
|
|
}
|
|
|
|
|
|
// --- implement IIdentifiable interface ---
|
|
|
|
// Generates a hash based on the source url of the article. Can be used to
|
|
// identify the article.
|
|
func (article *Article) Id() string {
|
|
hash := sha256.Sum256([]byte(article.SourceUrl))
|
|
return hex.EncodeToString(hash[:])
|
|
}
|