2023-05-27 07:57:01 +00:00
|
|
|
package routes
|
|
|
|
|
2023-05-27 08:04:41 +00:00
|
|
|
import (
|
2023-05-28 18:21:48 +00:00
|
|
|
"fmt"
|
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 18:21:48 +00:00
|
|
|
paragraphs := post.Content.BodyModel.Paragraphs
|
|
|
|
|
|
|
|
p := converters.ConvertParagraphs(paragraphs)
|
|
|
|
|
|
|
|
desc := ""
|
|
|
|
if len(paragraphs) >= 0 {
|
2023-05-28 18:26:23 +00:00
|
|
|
for _, p := range paragraphs {
|
|
|
|
if p.Type == "H3" || p.Type == "P" {
|
|
|
|
desc = p.Text
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
imgUrl := ""
|
|
|
|
for _, p := range paragraphs {
|
|
|
|
if p.Type == "IMG" {
|
|
|
|
imgUrl = fmt.Sprintf("https://miro.medium.com/v2/resize:fit:1200/%s", p.Metadata.ID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
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-28 18:21:48 +00:00
|
|
|
"Description": desc,
|
|
|
|
"Path": c.Path(),
|
|
|
|
"Image": imgUrl,
|
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
|
|
|
}
|