woj-server/internal/api/user/profile.go

53 lines
1.2 KiB
Go
Raw Normal View History

2022-09-20 14:15:21 +08:00
package user
2022-09-20 15:11:37 +08:00
import (
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/internal/model"
"git.0x7f.app/WOJ/woj-server/pkg/utils"
2022-09-20 15:11:37 +08:00
"github.com/gin-gonic/gin"
)
type profileRequest struct {
UID uint `form:"uid" json:"uid"`
2022-09-20 15:11:37 +08:00
}
// Profile
// @Summary profile
// @Description fetch user profile
2023-12-18 21:21:15 +08:00
// @Tags user
2022-09-20 15:11:37 +08:00
// @Accept application/x-www-form-urlencoded
// @Produce json
2022-09-26 16:13:31 +08:00
// @Param uid formData int false "user id"
// @Response 200 {object} e.Response[model.User] "user info"
2022-09-20 15:11:37 +08:00
// @Security Authentication
// @Router /v1/user/profile [post]
func (h *handler) Profile(c *gin.Context) {
claim, exist := c.Get("claim")
if !exist {
e.Pong[any](c, e.UserUnauthenticated, nil)
2022-09-20 15:11:37 +08:00
return
}
2023-07-15 16:19:49 +08:00
uid := claim.(*model.Claim).UID
role := claim.(*model.Claim).Role
2022-09-20 15:11:37 +08:00
req := new(profileRequest)
if err := c.ShouldBind(req); err != nil {
e.Pong(c, e.InvalidParameter, err.Error())
return
}
user, status := h.userService.Profile(utils.If(req.UID == 0, uid, req.UID))
if status != e.Success {
e.Pong[any](c, status, nil)
return
2022-09-20 15:11:37 +08:00
}
if role < model.RoleAdmin && user.ID != uid {
e.Pong[any](c, e.UserUnauthorized, nil)
return
}
2022-09-20 15:11:37 +08:00
e.Pong(c, status, user)
}