parent
144ed379dc
commit
ec687d3427
@ -1,19 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package apiresp
|
|
||||||
|
|
||||||
type ApiFormat interface {
|
|
||||||
ApiFormat()
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package apiresp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GinError(c *gin.Context, err error) {
|
|
||||||
c.JSON(http.StatusOK, ParseError(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
func GinSuccess(c *gin.Context, data any) {
|
|
||||||
c.JSON(http.StatusOK, ApiSuccess(data))
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package apiresp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func httpJson(w http.ResponseWriter, data any) {
|
|
||||||
body, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "json marshal error: "+err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func HttpError(w http.ResponseWriter, err error) {
|
|
||||||
httpJson(w, ParseError(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
func HttpSuccess(w http.ResponseWriter, data any) {
|
|
||||||
httpJson(w, ApiSuccess(data))
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package apiresp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ApiResponse struct {
|
|
||||||
ErrCode int `json:"errCode"`
|
|
||||||
ErrMsg string `json:"errMsg"`
|
|
||||||
ErrDlt string `json:"errDlt"`
|
|
||||||
Data any `json:"data,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAllFieldsPrivate(v any) bool {
|
|
||||||
typeOf := reflect.TypeOf(v)
|
|
||||||
if typeOf == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if typeOf.Kind() == reflect.Ptr {
|
|
||||||
typeOf = typeOf.Elem()
|
|
||||||
}
|
|
||||||
if typeOf.Kind() != reflect.Struct {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
num := typeOf.NumField()
|
|
||||||
for i := 0; i < num; i++ {
|
|
||||||
c := typeOf.Field(i).Name[0]
|
|
||||||
if c >= 'A' && c <= 'Z' {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func ApiSuccess(data any) *ApiResponse {
|
|
||||||
if format, ok := data.(ApiFormat); ok {
|
|
||||||
format.ApiFormat()
|
|
||||||
}
|
|
||||||
if isAllFieldsPrivate(data) {
|
|
||||||
return &ApiResponse{}
|
|
||||||
}
|
|
||||||
return &ApiResponse{
|
|
||||||
Data: data,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseError(err error) *ApiResponse {
|
|
||||||
if err == nil {
|
|
||||||
return ApiSuccess(nil)
|
|
||||||
}
|
|
||||||
unwrap := errs.Unwrap(err)
|
|
||||||
if codeErr, ok := unwrap.(errs.CodeError); ok {
|
|
||||||
resp := ApiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()}
|
|
||||||
if resp.ErrDlt == "" {
|
|
||||||
resp.ErrDlt = err.Error()
|
|
||||||
}
|
|
||||||
return &resp
|
|
||||||
}
|
|
||||||
return &ApiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error()}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package checker
|
|
||||||
|
|
||||||
type Checker interface {
|
|
||||||
Check() error
|
|
||||||
}
|
|
||||||
|
|
||||||
func Validate(args any) error {
|
|
||||||
if checker, ok := args.(Checker); ok {
|
|
||||||
if err := checker.Check(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package ormutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GormPage[E any](db *gorm.DB, pageNumber, showNumber int32) (uint32, []*E, error) {
|
|
||||||
var count int64
|
|
||||||
var model E
|
|
||||||
if err := db.Model(&model).Count(&count).Error; err != nil {
|
|
||||||
return 0, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
var es []*E
|
|
||||||
if err := db.Limit(int(showNumber)).Offset(int((pageNumber - 1) * showNumber)).Find(&es).Error; err != nil {
|
|
||||||
return 0, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
return uint32(count), es, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GormSearch[E any](db *gorm.DB, fields []string, value string, pageNumber, showNumber int32) (uint32, []*E, error) {
|
|
||||||
if len(fields) > 0 && value != "" {
|
|
||||||
val := "%" + value + "%"
|
|
||||||
arr := make([]string, 0, len(fields))
|
|
||||||
vals := make([]interface{}, 0, len(fields))
|
|
||||||
for _, field := range fields {
|
|
||||||
arr = append(arr, fmt.Sprintf("`%s` like ?", field))
|
|
||||||
vals = append(vals, val)
|
|
||||||
}
|
|
||||||
db = db.Where(strings.Join(arr, " or "), vals...)
|
|
||||||
}
|
|
||||||
return GormPage[E](db, pageNumber, showNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GormIn[E any](db **gorm.DB, field string, es []E) {
|
|
||||||
if len(es) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*db = (*db).Where(field+" in (?)", es)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MapCount(db *gorm.DB, field string) (map[string]uint32, error) {
|
|
||||||
var items []struct {
|
|
||||||
ID string `gorm:"column:id"`
|
|
||||||
Count uint32 `gorm:"column:count"`
|
|
||||||
}
|
|
||||||
if err := db.Select(field + " as id, count(1) as count").Group(field).Find(&items).Error; err != nil {
|
|
||||||
return nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
m := make(map[string]uint32)
|
|
||||||
for _, item := range items {
|
|
||||||
m[item.ID] = item.Count
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue