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"
2024-08-13 21:32:48 +00:00
"git.matterlinux.xyz/Matter/website/log"
2023-12-09 16:25:38 +00:00
"github.com/gofiber/fiber/v2"
)
2024-08-13 21:32:48 +00:00
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)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
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
}
2023-12-22 21:22:23 +00:00
2024-08-13 21:32:48 +00:00
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
2023-12-22 21:22:23 +00:00
2024-08-13 21:32:48 +00:00
return time1.After(time2)
})
return c.Render("news", fiber.Map{
"news": contents,
})
2023-12-09 16:25:38 +00:00
}
2024-08-13 21:32:48 +00:00
func GET_New(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
2024-08-13 21:32:48 +00:00
contents, err := lib.ListContent("news")
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
for _, con := range contents {
if con.ID != postid {
continue
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
return lib.RenderError(c, 404)
2023-12-09 16:25:38 +00:00
}