You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

4 years ago
package api
import (
"net/http"
"github.com/gin-gonic/gin"
4 years ago
"github.com/imdotdev/im.dev/server/internal/user"
4 years ago
"github.com/imdotdev/im.dev/server/pkg/common"
4 years ago
"github.com/imdotdev/im.dev/server/pkg/e"
"github.com/imdotdev/im.dev/server/pkg/models"
4 years ago
)
func GetUsers(c *gin.Context) {
query := c.Query("query")
4 years ago
users, err := user.GetUsers(query)
4 years ago
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(users))
}
4 years ago
func GetUserSelf(c *gin.Context) {
u := user.CurrentUser(c)
userDetail, err := user.GetUserDetail(u.ID, "")
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(userDetail))
}
func GetUser(c *gin.Context) {
username := c.Param("username")
userDetail, err := user.GetUserDetail(0, username)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(userDetail))
}
func UpdateUser(c *gin.Context) {
u := &models.User{}
c.Bind(&u)
currentUser := user.CurrentUser(c)
if currentUser.ID != u.ID {
c.JSON(http.StatusForbidden, common.RespError(e.NoPermission))
return
}
err := user.UpdateUser(u)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(nil))
}
func GetSession(c *gin.Context) {
sess := user.GetSession(c)
c.JSON(http.StatusOK, common.RespSuccess(sess))
}