personal-website/apps/personalprops/personalprops.go
2023-05-06 01:50:57 +03:00

131 lines
2.8 KiB
Go

package personalprops
import (
"errors"
"net/http"
websiteapp "personalwebsite/apps"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type PersonalPropertiesApp struct {
fs *webfilesystem.WebFileSystem
manifest websiteapp.ApplicationManifest
}
func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp {
newApp := PersonalPropertiesApp{
fs: webFs,
manifest: websiteapp.ApplicationManifest{
AppId: "personal-properties",
WindowName: "About me",
},
}
return newApp
}
func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) {
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
ginH, err := p.Render()
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
return
}
if isMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", ginH)
}
})
}
func (p *PersonalPropertiesApp) GetManifest() websiteapp.ApplicationManifest {
return p.manifest
}
func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId
}
func (p *PersonalPropertiesApp) Render() (gin.H, error) {
propsFile, err := p.fs.NewRead("/home/user/About Me.props")
if err != nil {
println(err.Error())
return nil, err
}
if propsFile.Type != "pers-props" {
return nil, errors.New("file is not a directory")
}
fileData, err := castToPersPropsData(propsFile.Data)
if err != nil {
println(err.Error())
return nil, err
}
_ = fileData
// file := castToFile(fileRaw)
// return file, nil
// if propsFile.Data == nil || propsFile.Type != "pers-props" {
// return nil, errors.New("bad file")
// }
// props, err := castToPersPropsData(propsFileRaw)
// _ = props
// if err != nil {
// return nil, err
// }
return gin.H{
// "headerProps": props.Header,
// "allprops": props.Props,
}, nil
}
func castToPersPropsData(fileData interface{}) (*PropertiesFileData, error) {
propsData := PropertiesFileData{
Header.Name: fileData.(primitive.D).Map()["name"].(string),
}
err := mapstructure.Decode(fileData.(primitive.D).Map(), &propsData)
if err != nil {
return nil, err
}
return &propsData, nil
}
type HeaderIsland struct {
Name string `bson:"name"`
IconPath string `bson:"iconpath"`
Info1 string `bson:"info1"`
Info2 string `bson:"info2"`
}
type PropElement struct {
Key string
KeyComments []string
Values []string
}
type PropIsland struct {
Header string
Props []PropElement
}
type PropertiesFile struct {
Props []PropIsland `bson:"props"`
}
type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}