227 lines
5.0 KiB
Go
227 lines
5.0 KiB
Go
package aboutme
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"path"
|
|
"personalwebsite/apps/appCtx"
|
|
"personalwebsite/errormessage"
|
|
"personalwebsite/webfilesystem"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
type AboutMeApp struct {
|
|
fs *webfilesystem.WebFileSystem
|
|
appID string
|
|
}
|
|
|
|
func NewAboutMeApp(webFs *webfilesystem.WebFileSystem) *AboutMeApp {
|
|
newApp := AboutMeApp{
|
|
fs: webFs,
|
|
appID: "AboutMe",
|
|
}
|
|
return &newApp
|
|
}
|
|
|
|
func (p *AboutMeApp) 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 *AboutMeApp) 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)
|
|
})
|
|
|
|
router.GET("read", func(ctx *gin.Context) {
|
|
filePath := ctx.Query("path")
|
|
if filePath == "" {
|
|
ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{
|
|
Message: "File path is empty",
|
|
})
|
|
return
|
|
}
|
|
propData, err := p.Read(filePath)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
|
|
Message: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, propData)
|
|
})
|
|
|
|
router.POST("edit", func(ctx *gin.Context) {
|
|
filePath := ctx.Query("path")
|
|
if filePath == "" {
|
|
ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{
|
|
Message: "File path is empty",
|
|
})
|
|
return
|
|
}
|
|
|
|
propsData := PropertiesFileData{}
|
|
err := ctx.BindJSON(&propsData)
|
|
if err != nil {
|
|
ctx.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
err = p.Edit(filePath, propsData)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{
|
|
Message: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
ctx.Status(http.StatusOK)
|
|
})
|
|
|
|
}
|
|
|
|
func (p *AboutMeApp) GetAppID() string {
|
|
return p.appID
|
|
}
|
|
|
|
func (p *AboutMeApp) 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 *AboutMeApp) Read(filePath string) (*PropertiesFileData, error) {
|
|
propData := PropertiesFileData{}
|
|
fileHeader, err := p.fs.Read(path.Join(filePath, "aboutme.props"), &propData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if fileHeader.Type != "personal-properties" {
|
|
return nil, errors.New("wrong file type")
|
|
}
|
|
return &propData, nil
|
|
}
|
|
|
|
func (p *AboutMeApp) Edit(filePath string, blogData PropertiesFileData) error {
|
|
propsPath := path.Join(filePath, "aboutme.props")
|
|
fileHeader, err := p.fs.Read(propsPath, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if fileHeader.Type != "personal-properties" {
|
|
return errors.New("wrong file type")
|
|
}
|
|
|
|
err = p.fs.Remove(propsPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fileHeader.MongoId = primitive.NewObjectID()
|
|
_, _, err = p.fs.Write(propsPath, fileHeader, blogData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *AboutMeApp) 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"`
|
|
}
|