aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/controller/user.go
diff options
context:
space:
mode:
authorVikas Kushwaha <dev@vikas.rocks>2025-02-11 16:31:08 +0530
committerVikas Kushwaha <dev@vikas.rocks>2025-02-11 16:31:08 +0530
commit57eb8f6712361a3bf75983ce153fac4846dc0273 (patch)
tree269a168d59c917c4e313c819e2b4c3ff8175f912 /cmd/web/controller/user.go
Initial commit
Diffstat (limited to 'cmd/web/controller/user.go')
-rw-r--r--cmd/web/controller/user.go110
1 files changed, 110 insertions, 0 deletions
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")
+}