restructuring
This commit is contained in:
54
internal/model/article.go
Normal file
54
internal/model/article.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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[:])
|
||||
}
|
||||
Reference in New Issue
Block a user