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
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user