Rename persprops to AboutMe

This commit is contained in:
cyber-dream 2023-05-25 01:51:37 +03:00
parent c1ba99bb77
commit a921646f4c
2 changed files with 93 additions and 12 deletions

View File

@ -1,29 +1,31 @@
package personalprops package aboutme
import ( import (
"errors"
"net/http" "net/http"
"path"
"personalwebsite/apps/appCtx" "personalwebsite/apps/appCtx"
"personalwebsite/errormessage"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
//TODO Rename to AboutMe type AboutMeApp struct {
type PersonalPropertiesApp struct {
fs *webfilesystem.WebFileSystem fs *webfilesystem.WebFileSystem
appID string appID string
} }
func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) *PersonalPropertiesApp { func NewAboutMeApp(webFs *webfilesystem.WebFileSystem) *AboutMeApp {
newApp := PersonalPropertiesApp{ newApp := AboutMeApp{
fs: webFs, fs: webFs,
appID: "AboutMe", appID: "AboutMe",
} }
return &newApp return &newApp
} }
func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) { func (p *AboutMeApp) PublicRoutes(route *gin.RouterGroup) {
route.POST("render", func(ctx *gin.Context) { route.POST("render", func(ctx *gin.Context) {
filePath := ctx.Query("path") filePath := ctx.Query("path")
if filePath == "" { if filePath == "" {
@ -50,7 +52,7 @@ func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) {
}) })
} }
func (p *PersonalPropertiesApp) PrivateRoutes(router *gin.RouterGroup) { func (p *AboutMeApp) PrivateRoutes(router *gin.RouterGroup) {
p.PublicRoutes(router) p.PublicRoutes(router)
router.GET("writeMock", func(ctx *gin.Context) { router.GET("writeMock", func(ctx *gin.Context) {
err := p.WriteMock() err := p.WriteMock()
@ -60,13 +62,58 @@ func (p *PersonalPropertiesApp) PrivateRoutes(router *gin.RouterGroup) {
ctx.Status(http.StatusOK) 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 *PersonalPropertiesApp) GetAppID() string { func (p *AboutMeApp) GetAppID() string {
return p.appID return p.appID
} }
func (p *PersonalPropertiesApp) WriteMock() error { func (p *AboutMeApp) WriteMock() error {
fileHeader := webfilesystem.FileHeader{ fileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(), MongoId: primitive.NewObjectID(),
Name: "aboutme.props", Name: "aboutme.props",
@ -107,7 +154,41 @@ func (p *PersonalPropertiesApp) WriteMock() error {
return err return err
} }
func (p *PersonalPropertiesApp) Render(appCtx appCtx.AppContext, filePath string) (gin.H, error) { 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{} propsData := PropertiesFileData{}
filePath = p.fs.RelativeToAbsolute(appCtx, filePath) filePath = p.fs.RelativeToAbsolute(appCtx, filePath)
_, err := p.fs.Read(filePath, &propsData) _, err := p.fs.Read(filePath, &propsData)

View File

@ -8,10 +8,10 @@ import (
"personalwebsite/apps" "personalwebsite/apps"
blogwriter "personalwebsite/apps/BlogWriter" blogwriter "personalwebsite/apps/BlogWriter"
"personalwebsite/apps/aboutme"
"personalwebsite/apps/blogviewer" "personalwebsite/apps/blogviewer"
"personalwebsite/apps/finder" "personalwebsite/apps/finder"
imgviewer "personalwebsite/apps/img-viewer" imgviewer "personalwebsite/apps/img-viewer"
"personalwebsite/apps/personalprops"
"personalwebsite/routes" "personalwebsite/routes"
"personalwebsite/wde" "personalwebsite/wde"
"personalwebsite/webfilesystem" "personalwebsite/webfilesystem"
@ -67,7 +67,7 @@ func main() {
webde := wde.NewWDE(webfs) webde := wde.NewWDE(webfs)
//TODO Split to different apps init for private and public? //TODO Split to different apps init for private and public?
persPropsApp := personalprops.NewPersPropsApp(webfs) persPropsApp := aboutme.NewAboutMeApp(webfs)
finderApp := finder.NewFinderApplication(webfs) finderApp := finder.NewFinderApplication(webfs)
imgViewerApp := imgviewer.NewImgViewerApp(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs)
blogViewerApp := blogviewer.NewBlogViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs)