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
}

View File

@@ -1,6 +1,9 @@
package model
import "time"
import (
"html/template"
"time"
)
type ScrapedArticle struct {
Title string
@@ -13,7 +16,7 @@ type ScrapedArticle struct {
type DisplayArticle struct {
ID int
Title string
Content string
Content template.HTML
Slug string
OriginalUrl string
SourceId int

View File

@@ -0,0 +1,61 @@
package server
import (
"github.com/gorilla/mux"
"gitlab.com/kbr4/svevijesti/internal/database"
"net/http"
"strconv"
"time"
)
func rootHandler(wr http.ResponseWriter, req *http.Request) {
title := "Pocetna"
store, err := database.Connect()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
articles, err := database.ArticlesForDay(store, time.Now())
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
data := map[string]interface{}{
"title": title,
"articles": articles,
}
err = templates.ExecuteTemplate(wr, "homeHTML", data)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
}
func articleHandler(wr http.ResponseWriter, req *http.Request) {
title := "Pocetna"
store, err := database.Connect()
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
vars := mux.Vars(req)
articleID, err := strconv.Atoi(vars["id"])
if err != nil {
articleID = -1
}
articleSlug := vars["slug"]
article, err := database.ArticleByID(store, articleID, articleSlug)
if err != nil {
http.Error(wr, err.Error(), http.StatusNotFound)
}
data := map[string]interface{}{
"title": title,
"article": article,
}
err = templates.ExecuteTemplate(wr, "articleHTML", data)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
}
}

View File

@@ -1 +1,45 @@
package server
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"io/ioutil"
"path/filepath"
"strings"
)
var tPath = "./web/tpl/"
var dPath = "./web/data/"
var templateDirs = []string{"./web/tpl", "./web/data"}
var templates *template.Template
func getTemplates() (templates *template.Template, err error) {
var allFiles []string
for _, dir := range templateDirs {
files2, _ := ioutil.ReadDir(dir)
for _, file := range files2 {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
filePath := filepath.Join(dir, filename)
fmt.Println("Template found: ", filePath)
allFiles = append(allFiles, filePath)
}
}
}
templates, err = template.New("").ParseFiles(allFiles...)
return
}
func init() {
templates, _ = getTemplates()
}
func CreateRoutes() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", rootHandler)
r.HandleFunc("/{id:[0-9]+}/{slug}", articleHandler)
return r
}