From e44397dcbb9454f89b8263738d5ccf1e20a12074 Mon Sep 17 00:00:00 2001 From: Vikas Kushwaha Date: Tue, 11 Feb 2025 17:50:21 +0530 Subject: Substituted 'article' with 'project' in filenames --- cmd/web/controller/htmx/article-action.go | 105 ------------------------------ cmd/web/controller/htmx/article.go | 64 ------------------ cmd/web/controller/htmx/project-action.go | 105 ++++++++++++++++++++++++++++++ cmd/web/controller/htmx/project.go | 64 ++++++++++++++++++ 4 files changed, 169 insertions(+), 169 deletions(-) delete mode 100644 cmd/web/controller/htmx/article-action.go delete mode 100644 cmd/web/controller/htmx/article.go create mode 100644 cmd/web/controller/htmx/project-action.go create mode 100644 cmd/web/controller/htmx/project.go (limited to 'cmd/web/controller/htmx') diff --git a/cmd/web/controller/htmx/article-action.go b/cmd/web/controller/htmx/article-action.go deleted file mode 100644 index a7b1f23..0000000 --- a/cmd/web/controller/htmx/article-action.go +++ /dev/null @@ -1,105 +0,0 @@ -package HTMXController - -import ( - "errors" - "projecty/cmd/web/model" - "projecty/internal/authentication" - "projecty/internal/database" - "projecty/internal/helper" - - "github.com/gofiber/fiber/v2" - "gorm.io/gorm" -) - -func ArticleFavoriteAction(c *fiber.Ctx) error { - - var article model.Article - var authenticatedUser model.User - - isArticleFavorited := false - - isAuthenticated, userID := authentication.AuthGet(c) - if !isAuthenticated { - return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) - - } - - db := database.Get() - - err := db.Model(&article). - Where("slug = ?", c.Params("slug")). - Preload("Favorites"). - Find(&article).Error - - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) - } - } - - authenticatedUser.ID = userID - - if article.FavoritedBy(userID) { - db.Model(&article).Association("Favorites").Delete(&authenticatedUser) - } else { - db.Model(&article).Association("Favorites").Append(&authenticatedUser) - isArticleFavorited = true - } - - return c.Render("articles/partials/favorite-button", fiber.Map{ - "Article": article, - "Slug": article.Slug, - "IsArticleFavorited": isArticleFavorited, - "IsOob": true, - }, "layouts/app-htmx") -} - -func ArticleFollowAction(c *fiber.Ctx) error { - - var article model.Article - var authenticatedUser model.User - - isFollowed := false - - isAuthenticated, userID := authentication.AuthGet(c) - if !isAuthenticated { - return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) - } - - db := database.Get() - - err := db.Model(&article). - Where("slug = ?", c.Params("slug")). - Preload("Favorites"). - Preload("User.Followers"). - Find(&article).Error - - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) - } - } - - authenticatedUser.ID = userID - - if article.User.FollowedBy(userID) { - - f := model.Follow{ - FollowerID: article.UserID, - FollowingID: userID, - } - - db.Model(&article.User).Association("Followers").Find(&f) - db.Delete(&f) - - } else { - db.Model(&article.User).Association("Followers").Append(&model.Follow{FollowerID: article.UserID, FollowingID: userID}) - isFollowed = true - } - - return c.Render("articles/partials/follow-button", fiber.Map{ - "Article": article, - "IsFollowed": isFollowed, - "IsOob": true, - }, "layouts/app-htmx") -} diff --git a/cmd/web/controller/htmx/article.go b/cmd/web/controller/htmx/article.go deleted file mode 100644 index c8dd8f4..0000000 --- a/cmd/web/controller/htmx/article.go +++ /dev/null @@ -1,64 +0,0 @@ -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") -} diff --git a/cmd/web/controller/htmx/project-action.go b/cmd/web/controller/htmx/project-action.go new file mode 100644 index 0000000..a7b1f23 --- /dev/null +++ b/cmd/web/controller/htmx/project-action.go @@ -0,0 +1,105 @@ +package HTMXController + +import ( + "errors" + "projecty/cmd/web/model" + "projecty/internal/authentication" + "projecty/internal/database" + "projecty/internal/helper" + + "github.com/gofiber/fiber/v2" + "gorm.io/gorm" +) + +func ArticleFavoriteAction(c *fiber.Ctx) error { + + var article model.Article + var authenticatedUser model.User + + isArticleFavorited := false + + isAuthenticated, userID := authentication.AuthGet(c) + if !isAuthenticated { + return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) + + } + + db := database.Get() + + err := db.Model(&article). + Where("slug = ?", c.Params("slug")). + Preload("Favorites"). + Find(&article).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) + } + } + + authenticatedUser.ID = userID + + if article.FavoritedBy(userID) { + db.Model(&article).Association("Favorites").Delete(&authenticatedUser) + } else { + db.Model(&article).Association("Favorites").Append(&authenticatedUser) + isArticleFavorited = true + } + + return c.Render("articles/partials/favorite-button", fiber.Map{ + "Article": article, + "Slug": article.Slug, + "IsArticleFavorited": isArticleFavorited, + "IsOob": true, + }, "layouts/app-htmx") +} + +func ArticleFollowAction(c *fiber.Ctx) error { + + var article model.Article + var authenticatedUser model.User + + isFollowed := false + + isAuthenticated, userID := authentication.AuthGet(c) + if !isAuthenticated { + return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) + } + + db := database.Get() + + err := db.Model(&article). + Where("slug = ?", c.Params("slug")). + Preload("Favorites"). + Preload("User.Followers"). + Find(&article).Error + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return helper.HTMXRedirectTo("/sign-in", "/htmx/sign-in", c) + } + } + + authenticatedUser.ID = userID + + if article.User.FollowedBy(userID) { + + f := model.Follow{ + FollowerID: article.UserID, + FollowingID: userID, + } + + db.Model(&article.User).Association("Followers").Find(&f) + db.Delete(&f) + + } else { + db.Model(&article.User).Association("Followers").Append(&model.Follow{FollowerID: article.UserID, FollowingID: userID}) + isFollowed = true + } + + return c.Render("articles/partials/follow-button", fiber.Map{ + "Article": article, + "IsFollowed": isFollowed, + "IsOob": true, + }, "layouts/app-htmx") +} 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") +} -- cgit v1.2.3