2023-05-27 07:57:01 +00:00
|
|
|
package routes
|
|
|
|
|
2023-05-27 08:04:41 +00:00
|
|
|
import (
|
2023-05-28 10:43:44 +00:00
|
|
|
"html/template"
|
2023-05-27 17:27:56 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-27 08:04:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/medium.rip/pkg/client"
|
2023-05-28 10:43:44 +00:00
|
|
|
"github.com/medium.rip/pkg/converters"
|
2023-05-27 08:04:41 +00:00
|
|
|
)
|
2023-05-27 07:57:01 +00:00
|
|
|
|
|
|
|
func show(c *fiber.Ctx) error {
|
2023-05-28 11:28:11 +00:00
|
|
|
postId := converters.ConvertId(c)
|
2023-05-27 08:04:41 +00:00
|
|
|
if postId == "" {
|
2023-05-28 11:28:11 +00:00
|
|
|
return c.Status(422).SendString("Invalid post ID")
|
2023-05-27 08:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e, err := client.PostData(postId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-27 17:27:56 +00:00
|
|
|
post := e.Data.Post
|
|
|
|
publishDate := time.UnixMilli(e.Data.Post.CreatedAt)
|
|
|
|
|
2023-05-28 10:43:44 +00:00
|
|
|
p := converters.ConvertParagraphs(post.Content.BodyModel.Paragraphs)
|
2023-05-27 17:27:56 +00:00
|
|
|
|
|
|
|
return c.Render("show", fiber.Map {
|
|
|
|
"Title": post.Title,
|
|
|
|
"UserId": post.Creator.ID,
|
|
|
|
"Author": post.Creator.Name,
|
|
|
|
"PublishDate": publishDate.Format(time.DateOnly),
|
2023-05-28 10:43:44 +00:00
|
|
|
"Paragraphs": template.HTML(p),
|
2023-05-27 17:27:56 +00:00
|
|
|
})
|
2023-05-27 07:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func index(c *fiber.Ctx) error {
|
|
|
|
return c.Render("index", fiber.Map {
|
|
|
|
"Title": "medium.rip",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterRoutes(app *fiber.App) {
|
|
|
|
app.Get("/", index)
|
2023-05-28 11:28:11 +00:00
|
|
|
app.Get("/*", show)
|
2023-05-27 07:57:01 +00:00
|
|
|
}
|