Detalji clanaka

This commit is contained in:
Senad Uka
2022-02-14 11:03:56 +01:00
parent f6e90deebd
commit 9f57520080
11 changed files with 207 additions and 62 deletions

View File

@@ -4,7 +4,9 @@ import (
"fmt"
_ "github.com/lib/pq"
"gitlab.com/kbr4/svevijesti/internal/model"
"html/template"
"math"
"strings"
"time"
)
@@ -86,3 +88,45 @@ func ArticlesForDay(store *Store, day time.Time) (articles []model.DisplayArticl
return result, nil
}
func ArticleByID(store *Store, ID int, slug string) (article model.DisplayArticle, err error) {
result := model.DisplayArticle{}
query, err := store.Prepare(`
select id,title, content, slug, original_url, source_id, created_at from articles where id = $1 and slug = $2;
`)
if err != nil {
return result, err
}
row := query.QueryRow(ID, slug)
if err != nil {
return result, err
}
r := model.DisplayArticle{}
content := ""
err = row.Scan(&r.ID, &r.Title, &content, &r.Slug, &r.OriginalUrl, &r.SourceId, &r.CreatedAt)
if err != nil {
return result, err
}
ago := time.Now().Sub(r.CreatedAt)
hours := ago.Hours()
r.Content = template.HTML(strings.Replace(content, "\n", "<br>\n", -1))
if hours < 1 {
r.FormatedCreatedAt = fmt.Sprintf("Prije %d minuta.", int(math.Floor(ago.Minutes())))
} else if hours > 24 {
r.FormatedCreatedAt = r.CreatedAt.Format("01.02.2006. 15:04:05")
} else {
r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(hours)))
}
r.SourceName = model.SourceName(r.SourceId)
result = r
return result, nil
}