diff options
Diffstat (limited to 'cmd/web/controller/project.go')
-rw-r--r-- | cmd/web/controller/project.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/cmd/web/controller/project.go b/cmd/web/controller/project.go new file mode 100644 index 0000000..94735f1 --- /dev/null +++ b/cmd/web/controller/project.go @@ -0,0 +1,63 @@ +package controller + +import ( + "errors" + "projecty/cmd/web/model" + "projecty/internal/authentication" + "projecty/internal/database" + + "github.com/gofiber/fiber/v2" + "gorm.io/gorm" +) + +func ArticleDetailPage(c *fiber.Ctx) error { + + var article model.Article + var authenticatedUser model.User + isSelf := false + isFollowed := false + + isAuthenticated, userID := authentication.AuthGet(c) + + db := database.Get() + + err := db.Model(&article). + Where("slug = ?", c.Params("slug")). + Preload("Favorites"). + Preload("Tags", func(db *gorm.DB) *gorm.DB { + return db.Order("tags.name asc") + }). + Preload("User.Followers"). + Find(&article).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return c.Redirect("/") + } + } + + if isAuthenticated { + db.Model(&authenticatedUser). + Where("id = ?", userID). + First(&authenticatedUser) + } + + if isAuthenticated && article.User.FollowedBy(userID) { + isFollowed = true + } + + if isAuthenticated && article.User.ID == userID { + isSelf = true + } + + return c.Render("articles/show", fiber.Map{ + "PageTitle": article.Title + " — Projecty", + "Article": article, + "FiberCtx": c, + "IsOob": false, + "IsSelf": isSelf, + "IsFollowed": isFollowed, + "IsArticleFavorited": article.FavoritedBy(userID), + "AuthenticatedUser": authenticatedUser, + }, "layouts/app") +} |