website/routes/news.go

70 lines
1.4 KiB
Go

package routes
import (
"log"
"sort"
"git.matterlinux.xyz/Matter/website/lib"
"github.com/gofiber/fiber/v2"
)
func NewsRoute(c *fiber.Ctx) error {
contents, err := lib.ListContent("news")
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date)
if err != nil {
log.Printf("Bad date while sorting: %s", err.Error())
return false
}
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Printf("Bad date while sorting: %s", err.Error())
return false
}
return time1.After(time2)
})
return c.Render("news", fiber.Map{
"news": contents,
})
}
func PostRoute(c *fiber.Ctx) error {
postid := c.Params("id")
if len(postid) == 0 {
return lib.RenderError(c, 404)
}
contents, err := lib.ListContent("news")
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
for _, con := range contents {
if(con.ID != postid) {
continue
}
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("post", fiber.Map{
"title": "News",
"post": con,
})
}
return lib.RenderError(c, 404)
}