diff options
Diffstat (limited to 'cmd/web/controller/editor.go')
-rw-r--r-- | cmd/web/controller/editor.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/cmd/web/controller/editor.go b/cmd/web/controller/editor.go new file mode 100644 index 0000000..725ac5f --- /dev/null +++ b/cmd/web/controller/editor.go @@ -0,0 +1,51 @@ +package controller + +import ( + "projecty/cmd/web/model" + "projecty/internal/authentication" + "projecty/internal/database" + + "github.com/gofiber/fiber/v2" + "gorm.io/gorm" +) + +func EditorPage(c *fiber.Ctx) error { + + var authenticatedUser model.User + var article model.Article + hasArticle := false + + isAuthenticated, userID := authentication.AuthGet(c) + if !isAuthenticated { + return c.Redirect("/") + } + + db := database.Get() + + db.Model(&authenticatedUser). + Where("id = ?", userID). + First(&authenticatedUser) + + if c.Params("slug") != "" { + + err := db.Model(&article). + Where("slug = ?", c.Params("slug")). + Preload("Tags", func(db *gorm.DB) *gorm.DB { + return db.Order("tags.name asc") + }). + Find(&article).Error + + if err == nil { + hasArticle = true + } + } + + return c.Render("editor/form", fiber.Map{ + "PageTitle": "Editor — Projecty", + "FiberCtx": c, + "NavBarActive": "editor", + "AuthenticatedUser": authenticatedUser, + "HasArticle": hasArticle, + "Article": article, + }, "layouts/app") +} |