From aa7e20ffad7ff22a9e0d5ca386ac77a99307cf28 Mon Sep 17 00:00:00 2001 From: goldeneggg Date: Thu, 31 May 2018 17:17:57 +0900 Subject: [PATCH 1/3] add DeleteModel method --- writers.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/writers.go b/writers.go index 4fa99dc..e19f816 100644 --- a/writers.go +++ b/writers.go @@ -438,6 +438,7 @@ type {{$ut.ModelName}}Storage interface { Add(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, {{$ut.LowerName}} *{{$ut.ModelName}}) (error) Update(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, {{$ut.LowerName}} *{{$ut.ModelName}}) (error) Delete(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, {{ $ut.PKAttributes}}) (error) + DeleteByModel(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, obj *{{$ut.ModelName}}) (error) {{range $rname, $rmt := $ut.RenderTo}}{{/* */}}{{range $vname, $view := $rmt.Views}}{{ $mtd := $ut.Project $rname $vname }} @@ -565,6 +566,26 @@ func (m *{{$ut.ModelName}}DB) Delete(ctx context.Context{{ if $ut.DynamicTableNa return nil } +// DeleteByModel removes a single record of assigned model. +// If record is deleted by this method, some defined callbacks of gorm are executed too. +func (m *{{$ut.ModelName}}DB) DeleteByModel(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, obj *{{$ut.ModelName}}) error { + defer goa.MeasureSince([]string{"goa","db","{{goify $ut.ModelName false}}", "deleteByModel"}, time.Now()) + + if obj == nil { + err := fmt.Errorf("assigned obj is nil") + goa.LogError(ctx, "error deleting {{$ut.ModelName}}", "error", err.Error()) + return err + } + + err := m.Db{{ if $ut.DynamicTableName }}.Table(tableName){{ end }}.Delete(obj).Error + if err != nil { + goa.LogError(ctx, "error deleting {{$ut.ModelName}}", "error", err.Error()) + return err + } + {{ if $ut.Cached }} go m.cache.Delete(strconv.Itoa(id)) {{ end }} + return nil +} + {{ range $bfn, $bf := $ut.BuiltFrom }} // {{$ut.ModelName}}From{{$bfn}} Converts source {{goify $bfn true}} to target {{$ut.ModelName}} model // only copying the non-nil fields from the source. From 0e153d1df713b45ff9042eb72679a2efa1da2cfa Mon Sep 17 00:00:00 2001 From: goldeneggg Date: Thu, 31 May 2018 17:51:51 +0900 Subject: [PATCH 2/3] fix --- writers.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/writers.go b/writers.go index e19f816..ee35003 100644 --- a/writers.go +++ b/writers.go @@ -571,12 +571,6 @@ func (m *{{$ut.ModelName}}DB) Delete(ctx context.Context{{ if $ut.DynamicTableNa func (m *{{$ut.ModelName}}DB) DeleteByModel(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, obj *{{$ut.ModelName}}) error { defer goa.MeasureSince([]string{"goa","db","{{goify $ut.ModelName false}}", "deleteByModel"}, time.Now()) - if obj == nil { - err := fmt.Errorf("assigned obj is nil") - goa.LogError(ctx, "error deleting {{$ut.ModelName}}", "error", err.Error()) - return err - } - err := m.Db{{ if $ut.DynamicTableName }}.Table(tableName){{ end }}.Delete(obj).Error if err != nil { goa.LogError(ctx, "error deleting {{$ut.ModelName}}", "error", err.Error()) From 02d82f0b793ba2c1c325188bbef52150ff560001 Mon Sep 17 00:00:00 2001 From: goldeneggg Date: Wed, 6 Jun 2018 12:29:41 +0900 Subject: [PATCH 3/3] fix comment --- writers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/writers.go b/writers.go index ee35003..0c7ac49 100644 --- a/writers.go +++ b/writers.go @@ -566,8 +566,8 @@ func (m *{{$ut.ModelName}}DB) Delete(ctx context.Context{{ if $ut.DynamicTableNa return nil } -// DeleteByModel removes a single record of assigned model. -// If record is deleted by this method, some defined callbacks of gorm are executed too. +// DeleteModel removes a single record given the corresponding model. +// Exisiting gorm deletion callbacks are executed if a record is deleted. func (m *{{$ut.ModelName}}DB) DeleteByModel(ctx context.Context{{ if $ut.DynamicTableName }}, tableName string{{ end }}, obj *{{$ut.ModelName}}) error { defer goa.MeasureSince([]string{"goa","db","{{goify $ut.ModelName false}}", "deleteByModel"}, time.Now())