Changing from js to golang

This commit is contained in:
2024-01-29 14:55:20 +01:00
parent 30d8ca73da
commit f4a2251178
40 changed files with 1174 additions and 438 deletions

View File

@@ -2,12 +2,13 @@ package database
import (
"fmt"
_ "github.com/lib/pq"
"gitlab.com/kbr4/svevijesti/internal/model"
"html/template"
"math"
"strings"
"time"
_ "github.com/lib/pq"
"gitlab.com/kbr4/svevijesti/internal/model"
)
func InsertArticle(store *Store, article model.ScrapedArticle) (err error) {
@@ -186,3 +187,48 @@ func PreviousAndNextArticleUrlByID(store *Store, ID int) (nextUrl string, previo
return nextResult, previousResult, nil
}
func ArticleByCategory(store *Store, day time.Time) (cateogry []model.DisplayArticle, err error) {
result := []model.DisplayArticle{}
query, err := store.Prepare(`select id,title,content,slug,created_at,source_id,category from articles where created_at > $1 and created_at < $2 and LENGTH(content) > 10 order by id desc;`)
if err != nil {
return result, err
}
defer query.Close()
tomorow := day.AddDate(0, 0, 1)
todayDate := day.Format("2024-01-26")
tomorrowDate := tomorow.Format("2024:01:26")
rows, err := query.Query(todayDate, tomorrowDate)
if err != nil {
return result, err
}
defer rows.Close()
for rows.Next() {
r := model.DisplayArticle{}
err = rows.Scan(&r.ID, &r.Title, &r.Content, &r.Slug, &r.CreatedAt, &r.OriginalUrl, &r.SourceId, &r.CreatedAt, &r.Category)
if err != nil {
return result, err
}
ago := time.Now().Sub(r.CreatedAt)
hours := ago.Hours()
if hours < 1 {
r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(ago.Minutes())))
} else if hours > 24 {
r.FormatedCreatedAt = r.CreatedAt.Format("28.01.2024. 01:03:05")
} else {
r.FormatedCreatedAt = fmt.Sprintf("Prije %d sati.", int(math.Floor(ago.Minutes())))
}
r.SourceName = model.SourceName(r.SourceId)
result = append(result, r)
}
return result, nil
}