Add blog editing api
This commit is contained in:
parent
55bb4c17fc
commit
5aaea9c1be
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user