Added news sorting and image support

This commit is contained in:
ngn
2023-12-23 00:22:23 +03:00
parent 6b503d8555
commit 6477240dd4
4 changed files with 34 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path"
"sort"
"strings"
"github.com/gofiber/fiber/v2"
@ -28,18 +29,27 @@ func NewsRoute(c *fiber.Ctx) error {
var con Content
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
if err != nil {
log.Errorf("Cannot news JSON: %s", err)
log.Errorf("Cannot news JSON for %s: %s", e.Name(), err)
return RenderError(c, 500)
}
err = json.Unmarshal(jsonc, &con)
if err != nil {
log.Errorf("Cannot parse news JSON: %s", err)
log.Errorf("Cannot parse news JSON for %s: %s", e.Name(), err)
return RenderError(c, 500)
}
con.Time, err = TimeFromString(con.Date)
if err != nil {
log.Errorf("Cannot parse time for '%s': %s", con.Name, err)
}
res = append(res, con)
}
sort.Slice(res, func(i, j int) bool {
return res[i].Time.After(res[j].Time)
})
return c.Render("news", fiber.Map{
"news": res,
})