Use utils.HttpResponse/Abort to replace c.JSON/Abort
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -10,26 +11,29 @@ import (
|
||||
func Full(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
c.JSON(403, gin.H{"status": "userid error"})
|
||||
utils.HttpResponse(c, 403, "", "userid error")
|
||||
return
|
||||
}
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to parse uuid"})
|
||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
||||
return
|
||||
}
|
||||
|
||||
userData, err := new(data.User).GetByUserId(userId)
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"status": "user not found"})
|
||||
utils.HttpResponse(c, 404, "", "user not found")
|
||||
return
|
||||
}
|
||||
|
||||
data, err := userData.GetFullTable()
|
||||
users, err := userData.GetFullTable()
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "database error"})
|
||||
utils.HttpResponse(c, 500, "", "database error")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"user_table": data})
|
||||
userFullResp := struct {
|
||||
UserTable *[]data.User `json:"user_table"`
|
||||
}{users}
|
||||
utils.HttpResponse(c, 200, "", "success", userFullResp)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -11,29 +12,32 @@ func Info(c *gin.Context) {
|
||||
userData := new(data.User)
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
c.JSON(403, gin.H{"status": "userid error"})
|
||||
utils.HttpResponse(c, 403, "", "userid error")
|
||||
return
|
||||
}
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to parse uuid"})
|
||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
||||
return
|
||||
}
|
||||
|
||||
// Get user from database
|
||||
user, err := userData.GetByUserId(userId)
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"status": "user not found"})
|
||||
utils.HttpResponse(c, 404, "", "user not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"user_id": user.UserId,
|
||||
"email": user.Email,
|
||||
"nickname": user.Nickname,
|
||||
"subtitle": user.Subtitle,
|
||||
"avatar": user.Avatar,
|
||||
"bio": user.Bio,
|
||||
"permission_level": user.PermissionLevel,
|
||||
})
|
||||
userInfoResp := struct {
|
||||
UserId uuid.UUID `json:"user_id"`
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Subtitle string `json:"subtitle"`
|
||||
Avatar string `json:"avatar"`
|
||||
Bio string `json:"bio"`
|
||||
PermissionLevel uint `json:"permission_level"`
|
||||
}{user.UserId, user.Email, user.Username, user.Nickname, user.Subtitle, user.Avatar, user.Bio, user.PermissionLevel}
|
||||
|
||||
utils.HttpResponse(c, 200, "", "success", userInfoResp)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/utils"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -15,26 +16,30 @@ func List(c *gin.Context) {
|
||||
}
|
||||
offset, ok := c.GetQuery("offset")
|
||||
if !ok {
|
||||
c.JSON(400, gin.H{"status": "offset not found"})
|
||||
utils.HttpResponse(c, 400, "", "offset not found")
|
||||
return
|
||||
}
|
||||
|
||||
// Parse string to int64
|
||||
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"status": "parse string to int error"})
|
||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
||||
return
|
||||
}
|
||||
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"status": "parse string to int error"})
|
||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
||||
return
|
||||
}
|
||||
|
||||
// Get user list from search engine
|
||||
list, err := new(data.User).FastListUsers(limitNum, offsetNum)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed list users from meilisearch"})
|
||||
utils.HttpResponse(c, 500, "", "failed list users from meilisearch")
|
||||
}
|
||||
c.JSON(200, list)
|
||||
|
||||
userListResp := struct {
|
||||
List *[]data.UserSearchDoc `json:"list"`
|
||||
}{list}
|
||||
utils.HttpResponse(c, 200, "", "success", userListResp)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package user
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/cryptography"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -12,12 +13,12 @@ func Update(c *gin.Context) {
|
||||
// New user model
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
c.JSON(403, gin.H{"status": "userid error"})
|
||||
utils.HttpResponse(c, 403, "", "userid error")
|
||||
return
|
||||
}
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to parse uuid"})
|
||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -27,18 +28,29 @@ func Update(c *gin.Context) {
|
||||
// Get user info
|
||||
userData, err := new(data.User).GetByUserId(userId)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to find user"})
|
||||
utils.HttpResponse(c, 500, "", "failed to find user")
|
||||
return
|
||||
}
|
||||
|
||||
userData.Avatar = ReqInfo.Avatar
|
||||
if len(ReqInfo.Email) < 5 || len(ReqInfo.Email) >= 255 {
|
||||
utils.HttpResponse(c, 400, "", "invilad email")
|
||||
return
|
||||
}
|
||||
userData.Email = ReqInfo.Email
|
||||
|
||||
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
||||
utils.HttpResponse(c, 400, "", "invilad user name")
|
||||
return
|
||||
}
|
||||
userData.Username = ReqInfo.Username
|
||||
|
||||
userData.Nickname = ReqInfo.Nickname
|
||||
userData.Subtitle = ReqInfo.Subtitle
|
||||
userData.Avatar = ReqInfo.Avatar
|
||||
|
||||
if ReqInfo.Bio != "" {
|
||||
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
||||
c.JSON(400, gin.H{"status": "invalid base64"})
|
||||
utils.HttpResponse(c, 400, "", "invalid base64")
|
||||
}
|
||||
}
|
||||
userData.Bio = ReqInfo.Bio
|
||||
@@ -46,5 +58,5 @@ func Update(c *gin.Context) {
|
||||
// Update user info
|
||||
userData.UpdateByUserID(userId)
|
||||
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
utils.HttpResponse(c, 200, "", "success")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user