69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"gitlab.com/kbr4/svevijesti/internal/database"
|
|
"gitlab.com/kbr4/svevijesti/internal/model"
|
|
)
|
|
|
|
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.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)
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|