personal-website/websiteapp/blogviewer/blogviewer.go

91 lines
1.9 KiB
Go

package blogviewer
import (
"personalwebsite/webfilesystem"
"personalwebsite/websiteapp"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type BlogViewerApplication struct {
fs *webfilesystem.WebFileSystem
manifest websiteapp.ApplicationManifest
}
func NewBlogViewerApp(webFs *webfilesystem.WebFileSystem) *BlogViewerApplication {
return &BlogViewerApplication{
fs: webFs,
manifest: websiteapp.ApplicationManifest{
AppId: "blog-viewer",
WindowName: "",
},
}
}
func (b *BlogViewerApplication) GetManifest() websiteapp.ApplicationManifest {
return b.manifest
}
func (b *BlogViewerApplication) GetId() string {
return b.manifest.AppId
}
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]",
},
},
},
},
}
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)
}
return gin.H{
"header": header,
"blocks": blocks,
}, nil
}
type Block struct {
Type string `bson:"type"`
Data []string `bson:"data"`
}