From b72645852bbda3b3bd637a0e8ef6c122ed368508 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Fri, 20 Jun 2025 17:53:11 +0300 Subject: [PATCH] refactor: profile controller; experiment: figured out a way to add auth middleware to individual methods in controllers, bypassing route group middleware if needed --- backend/internal/controllers/profile.go | 64 ++++++++++++++++--------- backend/internal/controllers/setup.go | 3 +- backend/internal/routes/router.go | 5 ++ 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/backend/internal/controllers/profile.go b/backend/internal/controllers/profile.go index 5cd8321..08aeeaf 100644 --- a/backend/internal/controllers/profile.go +++ b/backend/internal/controllers/profile.go @@ -1,11 +1,28 @@ package controllers import ( + "easywish/internal/middleware" "net/http" "github.com/gin-gonic/gin" ) +type ProfileController interface { + GetProfile(c *gin.Context) + GetOwnProfile(c *gin.Context) + UpdateProfile(c *gin.Context) + GetPrivacySettings(c *gin.Context) + UpdatePrivacySettings(c *gin.Context) + Router +} + +type profileControllerImpl struct { +} + +func NewProfileController() ProfileController { + return &profileControllerImpl{} +} + // @Summary Get someone's profile details // @Tags Profile // @Accept json @@ -13,20 +30,8 @@ import ( // @Param username path string true "Username" // @Security JWT // @Router /profile/{username} [get] -func GetProfile(c *gin.Context) { - - username := c.Param("username") - - if username == "" { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "Username cannot be empty", - }) - return - } - - c.JSON(http.StatusNotImplemented, gin.H{ - "username": username, - }) +func (p *profileControllerImpl) GetProfile(c *gin.Context) { + c.Status(http.StatusNotImplemented) } // @Summary Get own profile when authorized @@ -35,13 +40,18 @@ func GetProfile(c *gin.Context) { // @Produce json // @Security JWT // @Router /profile/me [get] -func GetOwnProfile(c *gin.Context) { +func (p *profileControllerImpl) GetOwnProfile(c *gin.Context) { + c.Status(http.StatusNotImplemented) +} - username := "Gregory House" - - c.JSON(http.StatusNotImplemented, gin.H{ - "username": username, - }) +// @Summary Update profile +// @Tags Profile +// @Accept json +// @Produce json +// @Security JWT +// @Router /profile [patch] +func (p *profileControllerImpl) UpdateProfile(c *gin.Context) { + c.Status(http.StatusNotImplemented) } // @Summary Get profile privacy settings @@ -50,7 +60,7 @@ func GetOwnProfile(c *gin.Context) { // @Produce json // @Security JWT // @Router /profile/privacy [get] -func GetPrivacySettings(c *gin.Context) { +func (p *profileControllerImpl) GetPrivacySettings(c *gin.Context) { c.Status(http.StatusNotImplemented) } @@ -60,6 +70,16 @@ func GetPrivacySettings(c *gin.Context) { // @Produce json // @Security JWT // @Router /profile/privacy [patch] -func UpdatePrivacySettings(c *gin.Context) { +func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) { c.Status(http.StatusNotImplemented) } + +func (p *profileControllerImpl) RegisterRoutes(group *gin.RouterGroup) { + protected := group.Group("") + protected.Use(middleware.JWTAuthMiddleware()) + { + protected.GET("/me", p.GetOwnProfile) + protected.GET("/:username", p.GetProfile) + protected.GET("/privacy", p.GetPrivacySettings) + } +} diff --git a/backend/internal/controllers/setup.go b/backend/internal/controllers/setup.go index a4bf298..5b64227 100644 --- a/backend/internal/controllers/setup.go +++ b/backend/internal/controllers/setup.go @@ -1,12 +1,13 @@ package controllers import ( - "go.uber.org/fx" + "go.uber.org/fx" ) var Module = fx.Module("controllers", fx.Provide( NewServiceController, NewAuthController, + NewProfileController, ), ) diff --git a/backend/internal/routes/router.go b/backend/internal/routes/router.go index ad1a538..abda7f2 100644 --- a/backend/internal/routes/router.go +++ b/backend/internal/routes/router.go @@ -25,6 +25,7 @@ type RouteGroup struct { func NewRouteGroups( authController controllers.AuthController, serviceController controllers.ServiceController, + profileController controllers.ProfileController, ) []RouteGroup { return []RouteGroup{ { @@ -35,5 +36,9 @@ func NewRouteGroups( BasePath: "/service", Router: serviceController, }, + { + BasePath: "/profile", + Router: profileController, + }, } }