personal-website/apps/personalprops/personalprops.go

146 lines
3.3 KiB
Go

package personalprops
import (
"net/http"
"personalwebsite/apps/appCtx"
"personalwebsite/webfilesystem"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
//TODO Rename to AboutMe
type PersonalPropertiesApp struct {
fs *webfilesystem.WebFileSystem
appID string
}
func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) *PersonalPropertiesApp {
newApp := PersonalPropertiesApp{
fs: webFs,
appID: "AboutMe",
}
return &newApp
}
func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) {
route.POST("render", func(ctx *gin.Context) {
filePath := ctx.Query("path")
if filePath == "" {
ctx.Status(http.StatusBadRequest)
return
}
appCtx := appCtx.AppContext{}
err := ctx.BindJSON(&appCtx)
if err != nil {
ctx.Status(http.StatusBadRequest)
return
}
ginH, err := p.Render(appCtx, filePath)
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
return
}
if appCtx.IsMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", ginH)
}
})
}
func (p *PersonalPropertiesApp) PrivateRoutes(router *gin.RouterGroup) {
p.PublicRoutes(router)
router.GET("writeMock", func(ctx *gin.Context) {
err := p.WriteMock()
if err != nil {
ctx.Status(http.StatusInternalServerError)
}
ctx.Status(http.StatusOK)
})
}
func (p *PersonalPropertiesApp) GetAppID() string {
return p.appID
}
func (p *PersonalPropertiesApp) WriteMock() error {
fileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "aboutme.props",
Type: "personal-properties",
Icon: "",
}
fileData := PropertiesFileData{
Header: HeaderIsland{
Name: "Test Name",
IconPath: "test icon path",
Info1: "Info1",
Info2: "Info2",
},
Props: []PropIsland{
{
Header: "Test Prop Header",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
{
Header: "Test Prop Header 2",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
},
}
_, _, err := p.fs.Write("/home/user/aboutme.props", &fileHeader, fileData)
return err
}
func (p *PersonalPropertiesApp) Render(appCtx appCtx.AppContext, filePath string) (gin.H, error) {
propsData := PropertiesFileData{}
filePath = p.fs.RelativeToAbsolute(appCtx, filePath)
_, err := p.fs.Read(filePath, &propsData)
if err != nil {
return nil, err
}
propsData.Header.IconPath = p.fs.RelativeToAbsolute(appCtx, propsData.Header.IconPath)
return gin.H{
"headerProps": propsData.Header,
"allprops": propsData.Props,
}, 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 PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}