Weather and Category
This commit is contained in:
@@ -4,31 +4,65 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"gitlab.com/kbr4/svevijesti/internal/database"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
)
|
||||
|
||||
func categoryHandler(wr http.ResponseWriter, r *http.Request) {
|
||||
var CategoryMenu = []string{
|
||||
"Politika",
|
||||
"Biznis",
|
||||
"Sport",
|
||||
"Magazin",
|
||||
"Nauka i tehnologija",
|
||||
"Ostalo",
|
||||
}
|
||||
|
||||
func handleCategory(wr http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
category := vars["category"]
|
||||
|
||||
store, err := database.Connect()
|
||||
|
||||
if err != nil {
|
||||
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
currentDate, err := time.Parse("2006-01-02", category)
|
||||
if err != nil {
|
||||
currentDate = time.Now()
|
||||
}
|
||||
|
||||
articles, err := database.ArticleByCategory(store, time.Now())
|
||||
articles, err := database.ArticlesForDay(store, currentDate)
|
||||
if err != nil {
|
||||
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
articlesByCategory := make(map[string][]model.DisplayArticle)
|
||||
|
||||
for _, article := range articles {
|
||||
articlesByCategory[article.Category] = append(articlesByCategory[article.Category], article)
|
||||
}
|
||||
|
||||
for category, articles := range articlesByCategory {
|
||||
data := map[string]interface{}{"Category": category, "Articles": articles}
|
||||
err := templates.ExecuteTemplate(wr, "categoryHTML", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var categories []string
|
||||
for cat := range articlesByCategory {
|
||||
categories = append(categories, cat)
|
||||
}
|
||||
|
||||
prevDay := currentDate.AddDate(0, 0, -1)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"title": category,
|
||||
"currentCategory": category,
|
||||
"articles": articlesByCategory[category],
|
||||
"categories": CategoryMenu,
|
||||
"previous": prevDay.Format("2006-01-02"),
|
||||
"next": "/",
|
||||
}
|
||||
err = templates.ExecuteTemplate(wr, "categoryHTML", data)
|
||||
if err != nil {
|
||||
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user