Files
cms-server/service/user/list.go
Noa Virellia e4329dfc2b refactor: standardize error handling with exception.Builder
- Replace hardcoded error messages with structured error codes using exception.Builder.
- Introduce new common error constants in exception/common.go (CommonErrorInvalidInput, CommonErrorUserNotFound, etc.).
- Update exception/specific.go with domain-specific errors and remove redundant ones.
- Apply consistent error handling across auth, event, user services and middleware.

Co-authored-by: Gemini <gemini@google.com>
Signed-off-by: Noa Virellia <noa@requiem.garden>
2026-01-21 12:47:49 +08:00

75 lines
1.9 KiB
Go

package user
import (
"nixcn-cms/data"
"nixcn-cms/exception"
"nixcn-cms/utils"
"strconv"
"github.com/gin-gonic/gin"
)
func List(c *gin.Context) {
// Get limit and offset from query
limit, ok := c.GetQuery("limit")
if !ok {
limit = "0"
}
offset, ok := c.GetQuery("offset")
if !ok {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.UserService).
SetEndpoint(exception.UserListEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
// Parse string to int64
limitNum, err := strconv.ParseInt(limit, 10, 64)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.UserService).
SetEndpoint(exception.UserListEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
offsetNum, err := strconv.ParseInt(offset, 10, 64)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusClient).
SetService(exception.UserService).
SetEndpoint(exception.UserListEndpoint).
SetType(exception.ErrorTypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
Build()
utils.HttpResponse(c, 400, errorCode)
return
}
// Get user list from search engine
list, err := new(data.User).FastListUsers(limitNum, offsetNum)
if err != nil {
errorCode := new(exception.Builder).
SetStatus(exception.ErrorStatusServer).
SetService(exception.UserService).
SetEndpoint(exception.UserListEndpoint).
SetType(exception.ErrorTypeSpecific).
SetOriginal(exception.UserListMeilisearchFailed).
Build()
utils.HttpResponse(c, 500, errorCode)
}
userListResp := struct {
List *[]data.UserSearchDoc `json:"list"`
}{list}
utils.HttpResponse(c, 200, "", "success", userListResp)
}