2022-09-20 14:15:21 +08:00
|
|
|
package user
|
2022-09-20 15:11:37 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/WHUPRJ/woj-server/internal/e"
|
2022-10-20 16:50:19 +08:00
|
|
|
"github.com/WHUPRJ/woj-server/internal/global"
|
|
|
|
"github.com/WHUPRJ/woj-server/internal/model"
|
2022-09-20 15:11:37 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type profileRequest struct {
|
|
|
|
UID uint `form:"uid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile
|
|
|
|
// @Summary profile
|
|
|
|
// @Description fetch user profile
|
|
|
|
// @Accept application/x-www-form-urlencoded
|
|
|
|
// @Produce json
|
2022-09-26 16:13:31 +08:00
|
|
|
// @Param uid formData int false "user id"
|
2022-09-20 15:11:37 +08:00
|
|
|
// @Response 200 {object} e.Response "user info"
|
|
|
|
// @Security Authentication
|
|
|
|
// @Router /v1/user/profile [post]
|
|
|
|
func (h *handler) Profile(c *gin.Context) {
|
|
|
|
// TODO: create a new struct for profile (user info & solve info)
|
|
|
|
|
|
|
|
claim, exist := c.Get("claim")
|
|
|
|
if !exist {
|
|
|
|
e.Pong(c, e.UserUnauthenticated, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := claim.(*global.Claim).UID
|
|
|
|
role := claim.(*global.Claim).Role
|
2022-10-22 17:38:39 +08:00
|
|
|
|
2022-09-20 15:11:37 +08:00
|
|
|
req := new(profileRequest)
|
2022-10-22 17:38:39 +08:00
|
|
|
|
|
|
|
if err := c.ShouldBind(req); err != nil {
|
|
|
|
e.Pong(c, e.InvalidParameter, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.UID == 0 {
|
|
|
|
req.UID = uid
|
|
|
|
} else if req.UID != uid && role < model.RoleGeneral {
|
|
|
|
e.Pong(c, e.UserUnauthorized, nil)
|
|
|
|
return
|
2022-09-20 15:11:37 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 17:38:39 +08:00
|
|
|
user, status := h.userService.Profile(req.UID)
|
2022-10-23 17:29:35 +08:00
|
|
|
|
|
|
|
// TODO: >= admin can see is_enable
|
|
|
|
|
2022-09-20 15:11:37 +08:00
|
|
|
e.Pong(c, status, user)
|
|
|
|
}
|