personal-website/websiteapp/blogviewer/blogviewer.go

91 lines
1.9 KiB
Go
Raw Normal View History

2023-04-14 15:53:23 +00:00
package blogviewer
import (
2023-04-29 13:58:39 +00:00
"personalwebsite/webfilesystem"
2023-04-14 15:53:23 +00:00
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin"
2023-04-29 13:58:39 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-04-14 15:53:23 +00:00
)
type BlogViewerApplication struct {
2023-04-29 13:58:39 +00:00
fs *webfilesystem.WebFileSystem
2023-04-14 15:53:23 +00:00
manifest websiteapp.ApplicationManifest
}
2023-04-29 13:58:39 +00:00
func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication {
2023-04-14 15:53:23 +00:00
return &BlogViewerApplication{
2023-04-29 13:58:39 +00:00
fs: webFs,
2023-04-14 15:53:23 +00:00
manifest: websiteapp.ApplicationManifest{
AppId: "blog-viewer",
WindowName: "",
},
}
}
func (b *BlogViewerApplication) GetManifest() websiteapp.ApplicationManifest {
return b.manifest
}
2023-04-29 13:58:39 +00:00
2023-04-14 15:53:23 +00:00
func (b *BlogViewerApplication) GetId() string {
return b.manifest.AppId
}
2023-04-29 13:58:39 +00:00
func (b *BlogViewerApplication) WriteMock(path string) {
blogFile := webfilesystem.WebFSFile{
MongoId: primitive.NewObjectID(),
Name: "test-1.blog",
Type: "blog-page",
Data: BlogFileData{
Header: "OMG THIS IS BLOG",
Blocks: []Block{
{
Type: "plain-text",
Data: []string{
"Apoqiwepoqiwepo",
".,mas;dakls;d",
"q[poqwieqpipoi]",
},
},
},
2023-04-14 15:53:23 +00:00
},
}
2023-04-29 13:58:39 +00:00
err := b.fs.CreateFile(&blogFile, path)
if err != nil {
println(err.Error())
}
}
func (b *BlogViewerApplication) Render(path string, isMobile bool) (gin.H, error) {
file, err := b.fs.Read(path)
if err != nil {
println(err.Error())
return nil, err
}
blogDataRaw := file.Data.(primitive.D).Map()
header := blogDataRaw["header"]
blocks := []Block{}
for _, rawBlock := range blogDataRaw["blocks"].(primitive.A) {
lel := rawBlock.(primitive.D).Map()
block := Block{
Type: lel["type"].(string),
Data: []string{},
}
for _, v := range lel["data"].(primitive.A) {
block.Data = append(block.Data, v.(string))
}
blocks = append(blocks, block)
2023-04-14 15:53:23 +00:00
}
2023-04-29 13:58:39 +00:00
return gin.H{
"header": header,
"blocks": blocks,
}, nil
2023-04-14 15:53:23 +00:00
}
type Block struct {
2023-04-29 13:58:39 +00:00
Type string `bson:"type"`
Data []string `bson:"data"`
2023-04-14 15:53:23 +00:00
}