Skip to content

Commit

Permalink
Merge pull request #167 from goldeneggg/add_another_delete_method
Browse files Browse the repository at this point in the history
PROPOSAL: Add another method for deletion
  • Loading branch information
raphael authored Nov 17, 2024
2 parents f82e629 + 02d82f0 commit 831c505
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -565,6 +566,20 @@ func (m *{{$ut.ModelName}}DB) Delete(ctx context.Context{{ if $ut.DynamicTableNa
return nil
}
// 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())
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.
Expand Down

0 comments on commit 831c505

Please sign in to comment.