From 5aaea9c1beba5c02f6a3de3c2fc8641ab0d7b84a Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 24 May 2023 02:09:16 +0300 Subject: [PATCH] Add blog editing api --- apps/blogviewer/blogviewer.go | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index 0537d5c..43e36af 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -1,9 +1,11 @@ package blogviewer import ( + "errors" "net/http" "path" "personalwebsite/apps/appCtx" + "personalwebsite/errormessage" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" @@ -27,6 +29,48 @@ func (b *BlogViewerApplication) GetAppID() string { } func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) { b.PublicRoutes(route) + + route.POST("edit", func(ctx *gin.Context) { + filePath := ctx.Query("path") + if filePath == "" { + ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{ + Message: "File path is empty", + }) + } + blogData := BlogFileData{} + err := ctx.BindJSON(&blogData) + if err != nil { + ctx.Status(http.StatusBadRequest) + return + } + + err = b.Edit(filePath, blogData) + if err != nil { + ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{ + Message: err.Error(), + }) + return + } + ctx.Status(http.StatusOK) + }) + + route.GET("read", func(ctx *gin.Context) { + filePath := ctx.Query("path") + if filePath == "" { + ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{ + Message: "File path is empty", + }) + } + + blogData, err := b.Read(filePath) + if err != nil { + ctx.JSON(http.StatusInternalServerError, errormessage.ErrorMessage{ + Message: err.Error(), + }) + return + } + ctx.JSON(http.StatusOK, blogData) + }) } func (b *BlogViewerApplication) PublicRoutes(route *gin.RouterGroup) { route.GET("writeMockBlog", func(ctx *gin.Context) { @@ -120,6 +164,42 @@ func (b *BlogViewerApplication) WriteMock(path string) error { return nil } +func (b *BlogViewerApplication) Read(filePath string) (*BlogFileData, error) { + fileData := BlogFileData{} + fileHeader, err := b.fs.Read(path.Join(filePath, ".content"), &fileData) + if err != nil { + return nil, err + } + + if fileHeader.Type != "blog-content" { + return nil, errors.New("wrong file type") + } + + return &fileData, nil +} + +func (b *BlogViewerApplication) Edit(filePath string, blogData BlogFileData) error { + contentPath := path.Join(filePath, ".content") + fileHeader, err := b.fs.Read(contentPath, nil) + if err != nil { + return err + } + if fileHeader.Type != "blog-content" { + return errors.New("wrong file type") + } + + err = b.fs.Remove(contentPath) + if err != nil { + return err + } + fileHeader.MongoId = primitive.NewObjectID() + _, _, err = b.fs.Write(contentPath, fileHeader, blogData) + if err != nil { + return err + } + return nil +} + func (b *BlogViewerApplication) Render(filePath string, appCtx appCtx.AppContext) (gin.H, error) { data := &BlogFileData{} _, err := b.fs.Read(path.Join(filePath, ".content"), &data)