refactor: profile controller;
experiment: figured out a way to add auth middleware to individual methods in controllers, bypassing route group middleware if needed
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Module = fx.Module("controllers",
|
var Module = fx.Module("controllers",
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
NewServiceController,
|
NewServiceController,
|
||||||
NewAuthController,
|
NewAuthController,
|
||||||
|
NewProfileController,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user