personal-website/apps/personalprops/personalprops.go

146 lines
3.3 KiB
Go
Raw Normal View History

2023-03-15 12:32:41 +00:00
package personalprops
2023-03-17 01:16:51 +00:00
import (
2023-05-03 21:50:08 +00:00
"net/http"
"personalwebsite/apps/appCtx"
2023-04-29 19:37:08 +00:00
"personalwebsite/webfilesystem"
2023-03-17 01:16:51 +00:00
"github.com/gin-gonic/gin"
2023-04-29 19:37:08 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-03-17 01:16:51 +00:00
)
//TODO Rename to AboutMe
2023-03-15 12:32:41 +00:00
type PersonalPropertiesApp struct {
fs *webfilesystem.WebFileSystem
appID string
2023-03-15 12:32:41 +00:00
}
func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) *PersonalPropertiesApp {
2023-03-17 01:16:51 +00:00
newApp := PersonalPropertiesApp{
fs: webFs,
appID: "AboutMe",
2023-03-17 01:16:51 +00:00
}
return &newApp
2023-03-15 12:32:41 +00:00
}
func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) {
route.POST("render", func(ctx *gin.Context) {
2023-05-07 01:00:22 +00:00
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)
2023-05-03 21:50:08 +00:00
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
2023-05-05 22:50:57 +00:00
return
2023-05-03 21:50:08 +00:00
}
if appCtx.IsMobile {
2023-05-03 21:50:08 +00:00
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)
})
2023-03-17 01:16:51 +00:00
}
func (p *PersonalPropertiesApp) GetAppID() string {
return p.appID
2023-03-17 01:16:51 +00:00
}
2023-05-07 01:00:22 +00:00
func (p *PersonalPropertiesApp) WriteMock() error {
fileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "aboutme.props",
Type: "personal-properties",
Icon: "",
2023-04-29 19:37:08 +00:00
}
2023-05-07 01:00:22 +00:00
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"},
},
},
},
},
}
2023-05-07 01:00:22 +00:00
_, _, err := p.fs.Write("/home/user/aboutme.props", &fileHeader, fileData)
return err
}
2023-05-05 22:50:57 +00:00
func (p *PersonalPropertiesApp) Render(appCtx appCtx.AppContext, filePath string) (gin.H, error) {
2023-05-07 01:00:22 +00:00
propsData := PropertiesFileData{}
filePath = p.fs.RelativeToAbsolute(appCtx, filePath)
2023-05-07 01:00:22 +00:00
_, err := p.fs.Read(filePath, &propsData)
if err != nil {
2023-05-05 22:50:57 +00:00
return nil, err
}
propsData.Header.IconPath = p.fs.RelativeToAbsolute(appCtx, propsData.Header.IconPath)
2023-05-05 22:50:57 +00:00
return gin.H{
2023-05-07 01:00:22 +00:00
"headerProps": propsData.Header,
"allprops": propsData.Props,
2023-05-05 22:50:57 +00:00
}, nil
}
type HeaderIsland struct {
2023-05-05 22:50:57 +00:00
Name string `bson:"name"`
IconPath string `bson:"iconpath"`
Info1 string `bson:"info1"`
Info2 string `bson:"info2"`
2023-03-15 12:32:41 +00:00
}
2023-03-17 01:16:51 +00:00
2023-03-17 16:13:41 +00:00
type PropElement struct {
2023-03-18 02:16:32 +00:00
Key string
KeyComments []string
Values []string
2023-03-17 01:16:51 +00:00
}
2023-03-18 00:34:56 +00:00
type PropIsland struct {
Header string
Props []PropElement
}
2023-05-05 22:50:57 +00:00
type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}