Skip to content

Commit

Permalink
chore: update cosy
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Feb 7, 2024
1 parent e12e83c commit 9be508c
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tmp_dir = "tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "CGO_ENABLED=1 go build -tags=jsoniter -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'\" -o ./tmp/main ."
cmd = "CGO_ENABLED=1 go build -tags=jsoniter -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'\" -v -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
Expand Down
136 changes: 68 additions & 68 deletions api/cosy/cosy.go
Original file line number Diff line number Diff line change
@@ -1,117 +1,117 @@
package cosy

import (
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)

var validate *validator.Validate

func init() {
validate = validator.New()
validate = validator.New()
}

type Ctx[T any] struct {
ctx *gin.Context
rules gin.H
Payload map[string]interface{}
Model T
OriginModel T
table string
tableArgs []interface{}
abort bool
nextHandler *gin.HandlerFunc
skipAssociationsOnCreate bool
beforeDecodeHookFunc []func(ctx *Ctx[T])
beforeExecuteHookFunc []func(ctx *Ctx[T])
executedHookFunc []func(ctx *Ctx[T])
gormScopes []func(tx *gorm.DB) *gorm.DB
preloads []string
scan func(tx *gorm.DB) any
transformer func(*T) any
permanentlyDelete bool
SelectedFields []string
itemKey string
ctx *gin.Context
rules gin.H
Payload map[string]interface{}
Model T
OriginModel T
table string
tableArgs []interface{}
abort bool
nextHandler *gin.HandlerFunc
skipAssociationsOnCreate bool
beforeDecodeHookFunc []func(ctx *Ctx[T])
beforeExecuteHookFunc []func(ctx *Ctx[T])
executedHookFunc []func(ctx *Ctx[T])
gormScopes []func(tx *gorm.DB) *gorm.DB
preloads []string
scan func(tx *gorm.DB) any
transformer func(*T) any
permanentlyDelete bool
SelectedFields []string
itemKey string
}

func Core[T any](c *gin.Context) *Ctx[T] {
return &Ctx[T]{
ctx: c,
gormScopes: make([]func(tx *gorm.DB) *gorm.DB, 0),
beforeExecuteHookFunc: make([]func(ctx *Ctx[T]), 0),
beforeDecodeHookFunc: make([]func(ctx *Ctx[T]), 0),
itemKey: "`id`",
skipAssociationsOnCreate: true,
}
return &Ctx[T]{
ctx: c,
gormScopes: make([]func(tx *gorm.DB) *gorm.DB, 0),
beforeExecuteHookFunc: make([]func(ctx *Ctx[T]), 0),
beforeDecodeHookFunc: make([]func(ctx *Ctx[T]), 0),
itemKey: "`id`",
skipAssociationsOnCreate: true,
}
}

func (c *Ctx[T]) SetTable(table string, args ...interface{}) *Ctx[T] {
c.table = table
c.tableArgs = args
return c
c.table = table
c.tableArgs = args
return c
}

func (c *Ctx[T]) SetItemKey(key string) *Ctx[T] {
c.itemKey = key
return c
c.itemKey = key
return c
}

func (c *Ctx[T]) SetValidRules(rules gin.H) *Ctx[T] {
c.rules = rules
c.rules = rules

return c
return c
}

func (c *Ctx[T]) SetPreloads(args ...string) *Ctx[T] {
c.preloads = append(c.preloads, args...)
return c
c.preloads = append(c.preloads, args...)
return c
}

func (c *Ctx[T]) validate() (errs gin.H) {
c.Payload = make(gin.H)
c.Payload = make(gin.H)

_ = c.ctx.ShouldBindJSON(&c.Payload)
_ = c.ctx.ShouldBindJSON(&c.Payload)

errs = validate.ValidateMap(c.Payload, c.rules)
errs = validate.ValidateMap(c.Payload, c.rules)

if len(errs) > 0 {
logger.Debug(errs)
for k := range errs {
errs[k] = c.rules[k]
}
return
}
// Make sure that the key in c.Payload is also the key of rules
validated := make(map[string]interface{})
if len(errs) > 0 {
logger.Debug(errs)
for k := range errs {
errs[k] = c.rules[k]
}
return
}
// Make sure that the key in c.Payload is also the key of rules
validated := make(map[string]interface{})

for k, v := range c.Payload {
if _, ok := c.rules[k]; ok {
validated[k] = v
}
}
for k, v := range c.Payload {
if _, ok := c.rules[k]; ok {
validated[k] = v
}
}

c.Payload = validated
c.Payload = validated

return
return
}

func (c *Ctx[T]) SetScan(scan func(tx *gorm.DB) any) *Ctx[T] {
c.scan = scan
return c
c.scan = scan
return c
}

func (c *Ctx[T]) SetTransformer(t func(m *T) any) *Ctx[T] {
c.transformer = t
return c
c.transformer = t
return c
}

func (c *Ctx[T]) AbortWithError(err error) {
c.abort = true
errHandler(c.ctx, err)
c.abort = true
errHandler(c.ctx, err)
}

func (c *Ctx[T]) Abort() {
c.abort = true
c.abort = true
}
15 changes: 8 additions & 7 deletions api/cosy/create.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cosy

import (
"github.com/gin-gonic/gin"
"github.com/0xJacky/Nginx-UI/api/cosy/map2struct"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm/clause"
"net/http"
)
Expand Down Expand Up @@ -52,12 +52,6 @@ func (c *Ctx[T]) Create() {
return
}

tx := db.Preload(clause.Associations)
for _, v := range c.preloads {
tx = tx.Preload(v)
}
tx.Table(c.table, c.tableArgs...).First(&c.Model)

if len(c.executedHookFunc) > 0 {
for _, v := range c.executedHookFunc {
v(c)
Expand All @@ -67,6 +61,13 @@ func (c *Ctx[T]) Create() {
}
}
}

tx := db.Preload(clause.Associations)
for _, v := range c.preloads {
tx = tx.Preload(v)
}
tx.Table(c.table, c.tableArgs...).First(&c.Model)

if c.nextHandler != nil {
(*c.nextHandler)(c.ctx)
} else {
Expand Down
2 changes: 1 addition & 1 deletion api/cosy/custom.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cosy

import (
"github.com/gin-gonic/gin"
"github.com/0xJacky/Nginx-UI/api/cosy/map2struct"
"github.com/gin-gonic/gin"
"net/http"
)

Expand Down
17 changes: 9 additions & 8 deletions api/cosy/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosy

import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/spf13/cast"
"gorm.io/gorm"
"net/http"
)
Expand All @@ -20,31 +21,31 @@ func (c *Ctx[T]) Destroy() {
c.beforeExecuteHook()

db := model.UseDB()
var dbModel T

result := db

if cast.ToBool(c.ctx.Query("permanent")) || c.permanentlyDelete {
result = result.Unscoped()
}

if len(c.gormScopes) > 0 {
result = result.Scopes(c.gormScopes...)
}

var err error
session := result.Session(&gorm.Session{})
if c.table != "" {
err = session.Table(c.table, c.tableArgs...).Take(&dbModel, id).Error
err = session.Table(c.table, c.tableArgs...).Take(c.OriginModel, id).Error
} else {
err = session.First(&dbModel, id).Error
err = session.First(&c.OriginModel, id).Error
}

if err != nil {
errHandler(c.ctx, err)
return
}

if c.permanentlyDelete {
result = result.Unscoped()
}

err = result.Delete(&dbModel).Error
err = result.Delete(&c.OriginModel).Error
if err != nil {
errHandler(c.ctx, err)
return
Expand Down
2 changes: 1 addition & 1 deletion api/cosy/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cosy

import (
"errors"
"github.com/gin-gonic/gin"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/cosy/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cosy

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
Expand Down
Loading

0 comments on commit 9be508c

Please sign in to comment.