Update method
This commit is contained in:
31
app/repository/method_update.go
Normal file
31
app/repository/method_update.go
Normal 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
|
||||
}
|
||||
@ -24,6 +24,7 @@ type RepoBase[T schema.Tabler] struct {
|
||||
ExistsMethod[T]
|
||||
CountMethod[T]
|
||||
SaveMethod[T]
|
||||
UpdateMethod[T]
|
||||
DeleteMethod[T]
|
||||
}
|
||||
|
||||
@ -51,6 +52,7 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
||||
&m.ExistsMethod,
|
||||
&m.CountMethod,
|
||||
&m.SaveMethod,
|
||||
&m.UpdateMethod,
|
||||
&m.DeleteMethod,
|
||||
}
|
||||
m.InitMethods(methods)
|
||||
|
||||
Reference in New Issue
Block a user