Update method

This commit is contained in:
Eden Kirin
2024-06-26 11:24:58 +02:00
parent 450345ba6b
commit 326866dc49
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package repository
import (
"repo-pattern/app/repository/smartfilter"
"gorm.io/gorm/schema"
)
type UpdateMethod[T schema.Tabler] struct {
repo *RepoBase[T]
}
func (m *UpdateMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
func (m UpdateMethod[T]) Update(filter interface{}, values map[string]interface{}) (int64, error) {
var (
model T
)
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
if err != nil {
return 0, err
}
result := query.Updates(values)
if result.Error != nil {
return 0, result.Error
}
return result.RowsAffected, nil
}

View File

@ -24,6 +24,7 @@ type RepoBase[T schema.Tabler] struct {
ExistsMethod[T] ExistsMethod[T]
CountMethod[T] CountMethod[T]
SaveMethod[T] SaveMethod[T]
UpdateMethod[T]
DeleteMethod[T] DeleteMethod[T]
} }
@ -51,6 +52,7 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
&m.ExistsMethod, &m.ExistsMethod,
&m.CountMethod, &m.CountMethod,
&m.SaveMethod, &m.SaveMethod,
&m.UpdateMethod,
&m.DeleteMethod, &m.DeleteMethod,
} }
m.InitMethods(methods) m.InitMethods(methods)