website/routes/news.go

70 lines
1.4 KiB
Go
Raw Normal View History

2023-12-09 16:25:38 +00:00
package routes
import (
2023-12-22 21:22:23 +00:00
"sort"
2023-12-09 16:25:38 +00:00
2024-01-18 17:01:30 +00:00
"git.matterlinux.xyz/Matter/website/lib"
2023-12-09 16:25:38 +00:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
func NewsRoute(c *fiber.Ctx) error {
contents, err := lib.ListContent("news")
2023-12-09 16:25:38 +00:00
if err != nil {
log.Errorf("ListContent -> %s", err.Error())
return lib.RenderError(c, 500)
2023-12-09 16:25:38 +00:00
}
sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date)
if err != nil {
log.Errorf("Bad date while sorting: %s", err.Error())
return false
2023-12-09 16:25:38 +00:00
}
2023-12-22 21:22:23 +00:00
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Errorf("Bad date while sorting: %s", err.Error())
return false
2023-12-22 21:22:23 +00:00
}
return time1.After(time2)
2023-12-22 21:22:23 +00:00
})
2023-12-09 16:25:38 +00:00
return c.Render("news", fiber.Map{
"news": contents,
2023-12-09 16:25:38 +00:00
})
}
func PostRoute(c *fiber.Ctx) error {
postid := c.Params("id")
if len(postid) == 0 {
return lib.RenderError(c, 404)
2023-12-09 16:25:38 +00:00
}
contents, err := lib.ListContent("news")
2023-12-09 16:25:38 +00:00
if err != nil {
log.Errorf("ListContent -> %s", err.Error())
return lib.RenderError(c, 500)
2023-12-09 16:25:38 +00:00
}
for _, con := range contents {
if(con.ID != postid) {
2023-12-09 16:25:38 +00:00
continue
}
con, err = lib.GetContent(con.Dir, con.Name)
2023-12-09 16:25:38 +00:00
if err != nil {
log.Errorf("GetContent -> %s", err.Error())
return lib.RenderError(c, 500)
2023-12-09 16:25:38 +00:00
}
return c.Render("post", fiber.Map{
"title": "News",
"post": con,
})
2023-12-09 16:25:38 +00:00
}
return lib.RenderError(c, 404)
2023-12-09 16:25:38 +00:00
}