feat: update
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package model
|
||||
|
||||
var {{.CamelTable}}UpdateExcludeColumns = []Column{
|
||||
var tbl{{.PascalTable}}UpdateMutableColumns = tbl{{.PascalTable}}.MutableColumns.Except(
|
||||
{{- if .HasCreatedAt}}
|
||||
table.{{.PascalTable}}.CreatedAt,
|
||||
tbl{{.PascalTable}}.CreatedAt,
|
||||
{{- end}}
|
||||
|
||||
{{- if .SoftDelete}}
|
||||
table.{{.PascalTable}}.DeletedAt,
|
||||
tbl{{.PascalTable}}.DeletedAt,
|
||||
{{- end}}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3,9 +3,6 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"{{ .PkgName }}/database/table"
|
||||
|
||||
"github.com/samber/lo"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -14,14 +11,20 @@ import (
|
||||
{{- if .SoftDelete }}
|
||||
func (m *{{.PascalTable}}) CondNotDeleted() Cond {
|
||||
return func(cond BoolExpression) BoolExpression {
|
||||
return cond.AND(table.{{.PascalTable}}.DeletedAt.IS_NULL())
|
||||
return cond.AND(tbl{{.PascalTable}}.DeletedAt.IS_NULL())
|
||||
}
|
||||
}
|
||||
|
||||
func (m *{{.PascalTable}}) CondDeleted() Cond {
|
||||
return func(cond BoolExpression) BoolExpression {
|
||||
return cond.AND(tbl{{.PascalTable}}.DeletedAt.IS_NOT_NULL())
|
||||
}
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
func (m *{{.PascalTable}}) CondID(id int64) Cond {
|
||||
return func(cond BoolExpression) BoolExpression {
|
||||
return cond.AND(table.{{.PascalTable}}.ID.EQ(Int(id)))
|
||||
return cond.AND(tbl{{.PascalTable}}.ID.EQ(Int(id)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +43,7 @@ func (m *{{.PascalTable}}) Create(ctx context.Context) error {
|
||||
{{- end}}
|
||||
|
||||
|
||||
stmt := table.Medias.INSERT(table.{{.PascalTable}}.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
||||
stmt := tblMedias.INSERT(tbl{{.PascalTable}}.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
|
||||
m.log().WithField("func","Create").Info( stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
@@ -54,7 +57,7 @@ func (m *{{.PascalTable}}) Create(ctx context.Context) error {
|
||||
|
||||
|
||||
func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalTable}}) error {
|
||||
stmt := table.{{.PascalTable}}.INSERT(table.{{.PascalTable}}.MutableColumns).MODELS(models)
|
||||
stmt := tbl{{.PascalTable}}.INSERT(tbl{{.PascalTable}}.MutableColumns).MODELS(models)
|
||||
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
@@ -68,7 +71,7 @@ func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalT
|
||||
|
||||
{{- if .SoftDelete }}
|
||||
func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
stmt := tbl{{.PascalTable}}.UPDATE().SET(tbl{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tbl{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
@@ -86,7 +89,7 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.IN(condIds...))
|
||||
stmt := tbl{{.PascalTable}}.UPDATE().SET(tbl{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tbl{{.PascalTable}}.ID.IN(condIds...))
|
||||
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
@@ -94,14 +97,13 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "BatchSoftDelete").Infof("{{.PascalTable}} items soft deleted successfully")
|
||||
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("{{.PascalTable}} items soft deleted successfully")
|
||||
return nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
|
||||
func (m *{{.PascalTable}}) ForceDelete(ctx context.Context) error {
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
stmt := tbl{{.PascalTable}}.DELETE().WHERE(tbl{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
@@ -118,40 +120,72 @@ func (m *{{.PascalTable}}) BatchForceDelete(ctx context.Context, ids []int64) er
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.IN(condIds...))
|
||||
stmt := tbl{{.PascalTable}}.DELETE().WHERE(tbl{{.PascalTable}}.ID.IN(condIds...))
|
||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().WithField("func","BatchForceDelete").Errorf("error deleting {{.PascalTable}} items: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("{{.PascalTable}} items deleted successfully")
|
||||
return nil
|
||||
}
|
||||
{{- else}}
|
||||
func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||
stmt := tbl{{.PascalTable}}.DELETE().WHERE(tbl{{.PascalTable}}.ID.EQ(Int(m.ID)))
|
||||
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func","Delete").Errorf("error deleting {{.PascalTable}} item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "Delete").Infof("{{.PascalTable}} item deleted successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchDelete
|
||||
func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
||||
return Int64(id)
|
||||
})
|
||||
|
||||
stmt := tbl{{.PascalTable}}.DELETE().WHERE(tbl{{.PascalTable}}.ID.IN(condIds...))
|
||||
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func","BatchDelete").Errorf("error deleting {{.PascalTable}} items: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "BatchDelete").Infof("{{.PascalTable}} items deleted successfully")
|
||||
m.log().WithField("func", "BatchDelete").WithField("ids", ids).Infof("{{.PascalTable}} items deleted successfully")
|
||||
return nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
// func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
// {{- if .HasUpdatedAt}}
|
||||
// m.UpdatedAt = time.Now()
|
||||
// {{- end}}
|
||||
func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
{{- if .HasUpdatedAt}}
|
||||
m.UpdatedAt = time.Now()
|
||||
{{- end}}
|
||||
|
||||
// stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns.Except({{.CamelTable}}UpdateExcludeColumns...)).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(table.{{.PascalTable}}.AllColumns)
|
||||
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
stmt := tbl{{.PascalTable}}.UPDATE(tbl{{.PascalTable}}UpdateMutableColumns).MODEL(m).WHERE(tbl{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(tbl{{.PascalTable}}.AllColumns)
|
||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
|
||||
// if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
// m.log().WithField("func","Update").Errorf("error updating {{.PascalTable}} item: %v", err)
|
||||
// return err
|
||||
// }
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func","Update").Errorf("error updating {{.PascalTable}} item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully")
|
||||
// return nil
|
||||
// }
|
||||
m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByCond
|
||||
func (m *{{.PascalTable}}) GetByCond(ctx context.Context, conds ...Cond) (*{{.PascalTable}}, error) {
|
||||
cond := CondTrue(conds...)
|
||||
|
||||
stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(cond)
|
||||
stmt := tbl{{.PascalTable}}.SELECT(tbl{{.PascalTable}}.AllColumns).WHERE(cond)
|
||||
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
@@ -173,7 +207,7 @@ func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64, conds ...Cond)
|
||||
func (m *{{.PascalTable}}) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
||||
cond := CondTrue(conds...)
|
||||
|
||||
tbl := table.{{.PascalTable}}
|
||||
tbl := tbl{{.PascalTable}}
|
||||
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"{{ .PkgName }}/app/service/testx"
|
||||
"{{ .PkgName }}/database"
|
||||
"{{ .PkgName }}/database/schemas/public/table"
|
||||
"{{ .PkgName }}/database/table"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
@@ -38,6 +38,6 @@ func Test_{{ .PascalTable }}(t *testing.T) {
|
||||
|
||||
func (s *{{ .PascalTable }}TestSuite) Test_Demo() {
|
||||
Convey("Test_Demo", s.T(), func() {
|
||||
database.Truncate(context.Background(), db, table.{{ .PascalTable }}.TableName())
|
||||
database.Truncate(context.Background(), db, tbl{{ .PascalTable }}.TableName())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user