diff --git a/internal/api/user/handler.go b/internal/api/user/handler.go index 6f0e94b..c61f324 100644 --- a/internal/api/user/handler.go +++ b/internal/api/user/handler.go @@ -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) } diff --git a/internal/api/user/profile.go b/internal/api/user/profile.go index a00006b..45eef59 100644 --- a/internal/api/user/profile.go +++ b/internal/api/user/profile.go @@ -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) +} diff --git a/internal/e/code.go b/internal/e/code.go index 5a6fd2b..d77f552 100644 --- a/internal/e/code.go +++ b/internal/e/code.go @@ -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", } diff --git a/internal/service/user/profile.go b/internal/service/user/profile.go new file mode 100644 index 0000000..07709db --- /dev/null +++ b/internal/service/user/profile.go @@ -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 +} diff --git a/internal/service/user/service.go b/internal/service/user/service.go index e684c72..b430350 100644 --- a/internal/service/user/service.go +++ b/internal/service/user/service.go @@ -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 {