2023-12-09 16:25:38 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2024-03-19 20:16:26 +00:00
|
|
|
"log"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewsRoute(c *fiber.Ctx) error {
|
2024-01-06 20:20:58 +00:00
|
|
|
contents, err := lib.ListContent("news")
|
2023-12-09 16:25:38 +00:00
|
|
|
if err != nil {
|
2024-03-19 20:16:26 +00:00
|
|
|
log.Printf("ListContent failed: %s", err.Error())
|
2024-01-06 20:20:58 +00:00
|
|
|
return lib.RenderError(c, 500)
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
sort.Slice(contents, func(i, j int) bool {
|
|
|
|
time1, err := lib.TimeFromString(contents[i].Date)
|
|
|
|
if err != nil {
|
2024-03-19 20:16:26 +00:00
|
|
|
log.Printf("Bad date while sorting: %s", err.Error())
|
2024-01-06 20:20:58 +00:00
|
|
|
return false
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
2023-12-22 21:22:23 +00:00
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
time2, err := lib.TimeFromString(contents[j].Date)
|
|
|
|
if err != nil {
|
2024-03-19 20:16:26 +00:00
|
|
|
log.Printf("Bad date while sorting: %s", err.Error())
|
2024-01-06 20:20:58 +00:00
|
|
|
return false
|
2023-12-22 21:22:23 +00:00
|
|
|
}
|
2024-01-06 20:20:58 +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{
|
2024-01-06 20:20:58 +00:00
|
|
|
"news": contents,
|
2023-12-09 16:25:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostRoute(c *fiber.Ctx) error {
|
|
|
|
postid := c.Params("id")
|
|
|
|
if len(postid) == 0 {
|
2024-01-06 20:20:58 +00:00
|
|
|
return lib.RenderError(c, 404)
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
contents, err := lib.ListContent("news")
|
2023-12-09 16:25:38 +00:00
|
|
|
if err != nil {
|
2024-03-19 20:16:26 +00:00
|
|
|
log.Printf("ListContent failed: %s", err.Error())
|
2024-01-06 20:20:58 +00:00
|
|
|
return lib.RenderError(c, 500)
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
for _, con := range contents {
|
|
|
|
if(con.ID != postid) {
|
2023-12-09 16:25:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
con, err = lib.GetContent(con.Dir, con.Name)
|
2023-12-09 16:25:38 +00:00
|
|
|
if err != nil {
|
2024-03-19 20:16:26 +00:00
|
|
|
log.Printf("ListContent failed: %s", err.Error())
|
2024-01-06 20:20:58 +00:00
|
|
|
return lib.RenderError(c, 500)
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
return c.Render("post", fiber.Map{
|
|
|
|
"title": "News",
|
|
|
|
"post": con,
|
|
|
|
})
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:20:58 +00:00
|
|
|
return lib.RenderError(c, 404)
|
2023-12-09 16:25:38 +00:00
|
|
|
}
|