mirror of https://github.com/sunface/rust-course
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.
54 lines
1.2 KiB
54 lines
1.2 KiB
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/imdotdev/im.dev/server/internal/admin"
|
|
"github.com/imdotdev/im.dev/server/internal/user"
|
|
"github.com/imdotdev/im.dev/server/pkg/common"
|
|
"github.com/imdotdev/im.dev/server/pkg/e"
|
|
"github.com/imdotdev/im.dev/server/pkg/models"
|
|
)
|
|
|
|
func AdminSubmitUser(c *gin.Context) {
|
|
currentUser := user.CurrentUser(c)
|
|
if !currentUser.Role.IsAdmin() {
|
|
c.JSON(http.StatusForbidden, common.RespError(e.NoPermission))
|
|
return
|
|
}
|
|
|
|
u := &models.User{}
|
|
c.Bind(&u)
|
|
|
|
if u.Username == "" || u.Email == "" || !govalidator.IsEmail(u.Email) || !u.Role.IsValid() {
|
|
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
|
|
return
|
|
}
|
|
|
|
err := user.SubmitUser(u)
|
|
if err != nil {
|
|
c.JSON(err.Status, common.RespError(err.Message))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, common.RespSuccess(nil))
|
|
}
|
|
|
|
func AdminGetUsers(c *gin.Context) {
|
|
currentUser := user.CurrentUser(c)
|
|
if !currentUser.Role.IsAdmin() {
|
|
c.JSON(http.StatusForbidden, common.RespError(e.NoPermission))
|
|
return
|
|
}
|
|
|
|
users, err := admin.GetUsers()
|
|
if err != nil {
|
|
c.JSON(err.Status, common.RespError(err.Message))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, common.RespSuccess(users))
|
|
}
|