66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package userhandler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
userlogic "spark-server/internal/module/user/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
// Handler is the users HTTP controller.
|
|
type Handler struct {
|
|
users *userlogic.Service
|
|
}
|
|
|
|
func New(users *userlogic.Service) *Handler {
|
|
return &Handler{users: users}
|
|
}
|
|
|
|
func (h *Handler) Me(c *gin.Context) {
|
|
rc := reqctx.From(c.Request.Context())
|
|
if rc == nil || rc.UserID <= 0 {
|
|
respond.JSONError(c, errs.ErrUnauthorized)
|
|
return
|
|
}
|
|
u, err := h.users.Get(c.Request.Context(), rc.UserID)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, userlogic.PublicProfile(u))
|
|
}
|
|
|
|
func (h *Handler) PatchMe(c *gin.Context) {
|
|
rc := reqctx.From(c.Request.Context())
|
|
if rc == nil || rc.UserID <= 0 {
|
|
respond.JSONError(c, errs.ErrUnauthorized)
|
|
return
|
|
}
|
|
var in userlogic.UpdateProfileInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
u, err := h.users.UpdateProfile(c.Request.Context(), rc.UserID, in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, userlogic.PublicProfile(u))
|
|
}
|
|
|
|
func (h *Handler) DeleteMe(c *gin.Context) {
|
|
rc := reqctx.From(c.Request.Context())
|
|
if rc == nil || rc.UserID <= 0 {
|
|
respond.JSONError(c, errs.ErrUnauthorized)
|
|
return
|
|
}
|
|
if err := h.users.DeleteAccount(c.Request.Context(), rc.UserID); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|