feat: add profile api
This commit is contained in:
parent
544a9fd071
commit
92dc04041e
@ -12,7 +12,8 @@ var _ Handler = (*handler)(nil)
|
||||
type Handler interface {
|
||||
Create(c *gin.Context)
|
||||
Login(c *gin.Context)
|
||||
// List(c *gin.Context)
|
||||
Logout(c *gin.Context)
|
||||
Profile(c *gin.Context)
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
@ -31,5 +32,5 @@ func RouteRegister(g *global.Global, group *gin.RouterGroup) {
|
||||
group.POST("/login", app.Login)
|
||||
group.POST("/create", app.Create)
|
||||
group.POST("/logout", app.jwtService.Handler(), app.Logout)
|
||||
// group.GET("/", app.List)
|
||||
group.POST("/profile", app.jwtService.Handler(), app.Profile)
|
||||
}
|
||||
|
@ -1 +1,48 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"github.com/WHUPRJ/woj-server/internal/e"
|
||||
"github.com/WHUPRJ/woj-server/internal/global"
|
||||
"github.com/WHUPRJ/woj-server/internal/repo/model"
|
||||
"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
|
||||
// @Param uid formData string false "user id"
|
||||
// @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
|
||||
req := new(profileRequest)
|
||||
if err := c.ShouldBind(req); err == nil {
|
||||
if req.UID != 0 && req.UID != uid {
|
||||
if role >= model.RoleAdmin {
|
||||
uid = req.UID
|
||||
} else {
|
||||
e.Pong(c, e.UserUnauthorized, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
user, status := h.userService.Profile(uid)
|
||||
e.Pong(c, status, user)
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ const (
|
||||
UserWrongPassword Status = 301
|
||||
UserDuplicated Status = 302
|
||||
UserUnauthenticated Status = 303
|
||||
UserUnauthorized Status = 304
|
||||
|
||||
RedisError Status = 400
|
||||
)
|
||||
@ -46,6 +47,7 @@ var msgText = map[Status]string{
|
||||
UserWrongPassword: "User Wrong Password",
|
||||
UserDuplicated: "User Duplicated",
|
||||
UserUnauthenticated: "User Unauthenticated",
|
||||
UserUnauthorized: "User Unauthorized",
|
||||
|
||||
RedisError: "Redis Error",
|
||||
}
|
||||
|
22
internal/service/user/profile.go
Normal file
22
internal/service/user/profile.go
Normal file
@ -0,0 +1,22 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/WHUPRJ/woj-server/internal/e"
|
||||
"github.com/WHUPRJ/woj-server/internal/repo/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (s *service) Profile(id uint) (*model.User, e.Status) {
|
||||
user := new(model.User)
|
||||
|
||||
err := s.db.First(&user, id).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return user, e.UserNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return user, e.DatabaseError
|
||||
}
|
||||
|
||||
return user, e.Success
|
||||
}
|
@ -15,6 +15,7 @@ type Service interface {
|
||||
Create(data *CreateData) (*model.User, e.Status)
|
||||
Login(data *model.User) (*model.User, e.Status)
|
||||
IncrVersion(id uint) (int64, e.Status)
|
||||
Profile(id uint) (*model.User, e.Status)
|
||||
}
|
||||
|
||||
type service struct {
|
||||
|
Loading…
Reference in New Issue
Block a user