From 57eb8f6712361a3bf75983ce153fac4846dc0273 Mon Sep 17 00:00:00 2001 From: Vikas Kushwaha Date: Tue, 11 Feb 2025 16:31:08 +0530 Subject: Initial commit --- cmd/web/controller/user.go | 110 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 cmd/web/controller/user.go (limited to 'cmd/web/controller/user.go') diff --git a/cmd/web/controller/user.go b/cmd/web/controller/user.go new file mode 100644 index 0000000..3b3bd07 --- /dev/null +++ b/cmd/web/controller/user.go @@ -0,0 +1,110 @@ +package controller + +import ( + "errors" + "projecty/cmd/web/model" + "projecty/internal/authentication" + "projecty/internal/database" + + "github.com/gofiber/fiber/v2" + "gorm.io/gorm" +) + +func UserDetailPage(c *fiber.Ctx) error { + + var user model.User + var authenticatedUser model.User + isSelf := false + isFollowed := false + navbarActive := "none" + + isAuthenticated, userID := authentication.AuthGet(c) + + db := database.Get() + + err := db.Model(&user). + Where("username = ?", c.Params("username")). + Preload("Followers"). + Find(&user).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 && user.ID == userID { + isSelf = true + navbarActive = "profile" + } + + if isAuthenticated && !isSelf && user.FollowedBy(userID) { + isFollowed = true + } + + return c.Render("users/show", fiber.Map{ + "PageTitle": user.Name + " — Projecty", + "FiberCtx": c, + "IsSelf": isSelf, + "IsFollowed": isFollowed, + "User": user, + "AuthenticatedUser": authenticatedUser, + "NavBarActive": navbarActive, + }, "layouts/app") +} + +func UserDetailFavoritePage(c *fiber.Ctx) error { + + var user model.User + var authenticatedUser model.User + isSelf := false + isFollowed := false + navbarActive := "none" + + isAuthenticated, userID := authentication.AuthGet(c) + + db := database.Get() + + err := db.Model(&user). + Where("username = ?", c.Params("username")). + Preload("Followers"). + Find(&user).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 && user.ID == userID { + isSelf = true + navbarActive = "profile" + } + + if isAuthenticated && !isSelf && user.FollowedBy(userID) { + isFollowed = true + } + + return c.Render("users/show", fiber.Map{ + "PageTitle": user.Name + " — Projecty", + "FiberCtx": c, + "IsSelf": isSelf, + "IsFollowed": isFollowed, + "User": user, + "AuthenticatedUser": authenticatedUser, + "NavBarActive": navbarActive, + "IsLoadFavorites": true, + }, "layouts/app") +} -- cgit v1.2.3