Changing from js to golang
This commit is contained in:
34
internal/server/category.go
Normal file
34
internal/server/category.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitlab.com/kbr4/svevijesti/internal/database"
|
||||
"gitlab.com/kbr4/svevijesti/internal/model"
|
||||
)
|
||||
|
||||
func categoryHandler(wr http.ResponseWriter, r *http.Request) {
|
||||
|
||||
store, err := database.Connect()
|
||||
|
||||
if err != nil {
|
||||
http.Error(wr, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
articles, err := database.ArticleByCategory(store, time.Now())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user