From a921646f4c6b57a0f6e98fa83783e75529149946 Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Thu, 25 May 2023 01:51:37 +0300 Subject: [PATCH] Rename persprops to AboutMe --- .../personalprops.go | 101 ++++++++++++++++-- main.go | 4 +- 2 files changed, 93 insertions(+), 12 deletions(-) rename apps/{personalprops => aboutme}/personalprops.go (54%) diff --git a/apps/personalprops/personalprops.go b/apps/aboutme/personalprops.go similarity index 54% rename from apps/personalprops/personalprops.go rename to apps/aboutme/personalprops.go index 387d5ef..a6e1680 100644 --- a/apps/personalprops/personalprops.go +++ b/apps/aboutme/personalprops.go @@ -1,29 +1,31 @@ -package personalprops +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" ) -//TODO Rename to AboutMe -type PersonalPropertiesApp struct { +type AboutMeApp struct { fs *webfilesystem.WebFileSystem appID string } -func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) *PersonalPropertiesApp { - newApp := PersonalPropertiesApp{ +func NewAboutMeApp(webFs *webfilesystem.WebFileSystem) *AboutMeApp { + newApp := AboutMeApp{ fs: webFs, appID: "AboutMe", } return &newApp } -func (p *PersonalPropertiesApp) PublicRoutes(route *gin.RouterGroup) { +func (p *AboutMeApp) PublicRoutes(route *gin.RouterGroup) { route.POST("render", func(ctx *gin.Context) { filePath := ctx.Query("path") 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) router.GET("writeMock", func(ctx *gin.Context) { err := p.WriteMock() @@ -60,13 +62,58 @@ func (p *PersonalPropertiesApp) PrivateRoutes(router *gin.RouterGroup) { 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 } -func (p *PersonalPropertiesApp) WriteMock() error { +func (p *AboutMeApp) WriteMock() error { fileHeader := webfilesystem.FileHeader{ MongoId: primitive.NewObjectID(), Name: "aboutme.props", @@ -107,7 +154,41 @@ func (p *PersonalPropertiesApp) WriteMock() error { 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{} filePath = p.fs.RelativeToAbsolute(appCtx, filePath) _, err := p.fs.Read(filePath, &propsData) diff --git a/main.go b/main.go index c7b63e1..f1030b6 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,10 @@ import ( "personalwebsite/apps" blogwriter "personalwebsite/apps/BlogWriter" + "personalwebsite/apps/aboutme" "personalwebsite/apps/blogviewer" "personalwebsite/apps/finder" imgviewer "personalwebsite/apps/img-viewer" - "personalwebsite/apps/personalprops" "personalwebsite/routes" "personalwebsite/wde" "personalwebsite/webfilesystem" @@ -67,7 +67,7 @@ func main() { webde := wde.NewWDE(webfs) //TODO Split to different apps init for private and public? - persPropsApp := personalprops.NewPersPropsApp(webfs) + persPropsApp := aboutme.NewAboutMeApp(webfs) finderApp := finder.NewFinderApplication(webfs) imgViewerApp := imgviewer.NewImgViewerApp(webfs) blogViewerApp := blogviewer.NewBlogViewerApp(webfs)