diff options
author | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 17:50:21 +0530 |
---|---|---|
committer | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 17:50:21 +0530 |
commit | e44397dcbb9454f89b8263738d5ccf1e20a12074 (patch) | |
tree | 1c2b5816c597ddd10c7afb68ed2af7cc2c26d56d /cmd/web/controller/htmx/project.go | |
parent | c720f1f85e5ec7c93604462b7a70e1f84a1ce244 (diff) |
Diffstat (limited to 'cmd/web/controller/htmx/project.go')
-rw-r--r-- | cmd/web/controller/htmx/project.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/cmd/web/controller/htmx/project.go b/cmd/web/controller/htmx/project.go new file mode 100644 index 0000000..c8dd8f4 --- /dev/null +++ b/cmd/web/controller/htmx/project.go @@ -0,0 +1,64 @@ +package HTMXController + +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 + isSelf := false + isFollowed := false + var authenticatedUser model.User + + isAuthenticated, userID := authentication.AuthGet(c) + + db := database.Get() + + if isAuthenticated { + db.Model(&authenticatedUser). + Where("id = ?", userID). + First(&authenticatedUser) + } + + 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"). + Find(&article).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return c.Redirect("/") + } + } + + if isAuthenticated && article.User.FollowedBy(userID) { + isFollowed = true + } + + if isAuthenticated && article.User.ID == userID { + isSelf = true + } + + return c.Render("articles/htmx-article-page", fiber.Map{ + "PageTitle": article.Title, + "NavBarActive": "none", + "Article": article, + "IsOob": false, + "IsSelf": isSelf, + "IsFollowed": isFollowed, + "IsArticleFavorited": article.FavoritedBy(userID), + "AuthenticatedUser": authenticatedUser, + "FiberCtx": c, + }, "layouts/app-htmx") +} |