website/routes/news.go

70 lines
1.4 KiB
Go

package routes
import (
"sort"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2"
)
func GET_News(c *fiber.Ctx) error {
contents, err := lib.ListContent("news")
if err != nil {
log.Error("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.Error("Bad date while sorting: %s", err.Error())
return false
}
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
return time1.After(time2)
})
return c.Render("news", fiber.Map{
"news": contents,
})
}
func GET_New(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.Error("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.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
return lib.RenderError(c, 404)
}