mirror of
https://github.com/go-nunu/nunu-layout-advanced.git
synced 2025-11-02 21:04:01 +08:00
88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-nunu/nunu-layout-advanced/internal/service"
|
|
"github.com/go-nunu/nunu-layout-advanced/pkg/helper/resp"
|
|
"github.com/pkg/errors"
|
|
"net/http"
|
|
)
|
|
|
|
type UserHandler struct {
|
|
*Handler
|
|
userService *service.UserService
|
|
}
|
|
|
|
func NewUserHandler(handler *Handler, userService *service.UserService) *UserHandler {
|
|
return &UserHandler{
|
|
Handler: handler,
|
|
userService: userService,
|
|
}
|
|
}
|
|
|
|
func (h *UserHandler) Register(ctx *gin.Context) {
|
|
req := new(service.RegisterRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, errors.Wrap(err, "invalid request").Error(), nil)
|
|
return
|
|
}
|
|
|
|
if err := h.userService.Register(req); err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, errors.Wrap(err, "invalid request").Error(), nil)
|
|
return
|
|
}
|
|
|
|
resp.HandleSuccess(ctx, nil)
|
|
}
|
|
|
|
func (h *UserHandler) Login(ctx *gin.Context) {
|
|
var req service.LoginRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, errors.Wrap(err, "invalid request").Error(), nil)
|
|
return
|
|
}
|
|
|
|
token, err := h.userService.Login(&req)
|
|
if err != nil {
|
|
resp.HandleError(ctx, http.StatusUnauthorized, 1, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
resp.HandleSuccess(ctx, gin.H{
|
|
"accessToken": token,
|
|
})
|
|
}
|
|
|
|
func (h *UserHandler) GetProfile(ctx *gin.Context) {
|
|
userId := GetUserIdFromCtx(ctx)
|
|
if userId == "" {
|
|
resp.HandleError(ctx, http.StatusUnauthorized, 1, "unauthorized", nil)
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.GetProfile(userId)
|
|
if err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
resp.HandleSuccess(ctx, user)
|
|
}
|
|
|
|
func (h *UserHandler) UpdateProfile(ctx *gin.Context) {
|
|
userId := GetUserIdFromCtx(ctx)
|
|
|
|
var req service.UpdateProfileRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, errors.Wrap(err, "invalid request").Error(), nil)
|
|
return
|
|
}
|
|
|
|
if err := h.userService.UpdateProfile(userId, &req); err != nil {
|
|
resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
|
|
return
|
|
}
|
|
|
|
resp.HandleSuccess(ctx, nil)
|
|
}
|