experiment-service_controller_pattern #1

Merged
weirdcat merged 8 commits from experiment-service_controller_pattern into main 2025-06-20 17:57:12 +03:00
3 changed files with 49 additions and 23 deletions
Showing only changes of commit b72645852b - Show all commits

View File

@@ -1,11 +1,28 @@
package controllers package controllers
import ( import (
"easywish/internal/middleware"
"net/http" "net/http"
"github.com/gin-gonic/gin" "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 // @Summary Get someone's profile details
// @Tags Profile // @Tags Profile
// @Accept json // @Accept json
@@ -13,20 +30,8 @@ import (
// @Param username path string true "Username" // @Param username path string true "Username"
// @Security JWT // @Security JWT
// @Router /profile/{username} [get] // @Router /profile/{username} [get]
func GetProfile(c *gin.Context) { func (p *profileControllerImpl) GetProfile(c *gin.Context) {
c.Status(http.StatusNotImplemented)
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,
})
} }
// @Summary Get own profile when authorized // @Summary Get own profile when authorized
@@ -35,13 +40,18 @@ func GetProfile(c *gin.Context) {
// @Produce json // @Produce json
// @Security JWT // @Security JWT
// @Router /profile/me [get] // @Router /profile/me [get]
func GetOwnProfile(c *gin.Context) { func (p *profileControllerImpl) GetOwnProfile(c *gin.Context) {
c.Status(http.StatusNotImplemented)
}
username := "Gregory House" // @Summary Update profile
// @Tags Profile
c.JSON(http.StatusNotImplemented, gin.H{ // @Accept json
"username": username, // @Produce json
}) // @Security JWT
// @Router /profile [patch]
func (p *profileControllerImpl) UpdateProfile(c *gin.Context) {
c.Status(http.StatusNotImplemented)
} }
// @Summary Get profile privacy settings // @Summary Get profile privacy settings
@@ -50,7 +60,7 @@ func GetOwnProfile(c *gin.Context) {
// @Produce json // @Produce json
// @Security JWT // @Security JWT
// @Router /profile/privacy [get] // @Router /profile/privacy [get]
func GetPrivacySettings(c *gin.Context) { func (p *profileControllerImpl) GetPrivacySettings(c *gin.Context) {
c.Status(http.StatusNotImplemented) c.Status(http.StatusNotImplemented)
} }
@@ -60,6 +70,16 @@ func GetPrivacySettings(c *gin.Context) {
// @Produce json // @Produce json
// @Security JWT // @Security JWT
// @Router /profile/privacy [patch] // @Router /profile/privacy [patch]
func UpdatePrivacySettings(c *gin.Context) { func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) {
c.Status(http.StatusNotImplemented) 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)
}
}

View File

@@ -8,5 +8,6 @@ var Module = fx.Module("controllers",
fx.Provide( fx.Provide(
NewServiceController, NewServiceController,
NewAuthController, NewAuthController,
NewProfileController,
), ),
) )

View File

@@ -25,6 +25,7 @@ type RouteGroup struct {
func NewRouteGroups( func NewRouteGroups(
authController controllers.AuthController, authController controllers.AuthController,
serviceController controllers.ServiceController, serviceController controllers.ServiceController,
profileController controllers.ProfileController,
) []RouteGroup { ) []RouteGroup {
return []RouteGroup{ return []RouteGroup{
{ {
@@ -35,5 +36,9 @@ func NewRouteGroups(
BasePath: "/service", BasePath: "/service",
Router: serviceController, Router: serviceController,
}, },
{
BasePath: "/profile",
Router: profileController,
},
} }
} }