diff --git a/admin/logout.go b/admin/logout.go index f251959..800df99 100644 --- a/admin/logout.go +++ b/admin/logout.go @@ -3,6 +3,7 @@ package admin import ( "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + "github.com/god-jason/bucket/api" ) func logout(ctx *gin.Context) { diff --git a/export/export.go b/export/export.go index 7c3e8d9..f02cc0c 100644 --- a/export/export.go +++ b/export/export.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" + "github.com/god-jason/bucket/api" "github.com/god-jason/bucket/mongodb" "go.mongodb.org/mongo-driver/bson" ) @@ -18,7 +19,7 @@ func ApiExport(table, filename string) gin.HandlerFunc { err := mongodb.Find(table, bson.D{{Key: "_id", Value: bson.E{Key: "$in", Value: ids}}}, nil, 0, 0, &datum) if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } diff --git a/export/import.go b/export/import.go index 73d2cc8..3723579 100644 --- a/export/import.go +++ b/export/import.go @@ -4,6 +4,7 @@ import ( "archive/zip" "encoding/json" "github.com/gin-gonic/gin" + "github.com/god-jason/bucket/api" "github.com/god-jason/bucket/mongodb" "go.mongodb.org/mongo-driver/bson/primitive" "io" @@ -13,20 +14,20 @@ func ApiImport(table string) gin.HandlerFunc { return func(ctx *gin.Context) { formFile, err := ctx.FormFile("file") if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } file, err := formFile.Open() if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } defer file.Close() reader, err := zip.NewReader(file, formFile.Size) if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } @@ -40,26 +41,26 @@ func ApiImport(table string) gin.HandlerFunc { reader, err := file.Open() buf, err := io.ReadAll(reader) if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } var data []any err = json.Unmarshal(buf, &data) if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } ids, err := mongodb.InsertMany(table, data) if err != nil { - Error(ctx, err) + api.Error(ctx, err) return } idss = append(idss, ids...) } - OK(ctx, idss) + api.OK(ctx, idss) } } diff --git a/export/reply.go b/export/reply.go deleted file mode 100644 index 7186f25..0000000 --- a/export/reply.go +++ /dev/null @@ -1,33 +0,0 @@ -package export - -import ( - "github.com/gin-gonic/gin" - "net/http" -) - -type ReplyData[T any] struct { - Data T `json:"data"` - Error string `json:"error,omitempty"` -} - -type ReplyList[T any] struct { - Data []T `json:"data"` - Total int64 `json:"total"` - Error string `json:"error,omitempty"` -} - -func List(ctx *gin.Context, data any, total int64) { - ctx.JSON(http.StatusOK, gin.H{"data": data, "total": total}) -} - -func OK(ctx *gin.Context, data any) { - ctx.JSON(http.StatusOK, gin.H{"data": data}) -} - -func Fail(ctx *gin.Context, err string) { - ctx.JSON(http.StatusOK, gin.H{"error": err}) -} - -func Error(ctx *gin.Context, err error) { - ctx.JSON(http.StatusOK, gin.H{"error": err.Error()}) -} diff --git a/function/web.go b/function/api.go similarity index 100% rename from function/web.go rename to function/api.go