Compare commits
2 Commits
bdc978aec1
...
326866dc49
| Author | SHA1 | Date | |
|---|---|---|---|
| 326866dc49 | |||
| 450345ba6b |
31
app/repository/method_delete.go
Normal file
31
app/repository/method_delete.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"repo-pattern/app/repository/smartfilter"
|
||||||
|
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeleteMethod[T schema.Tabler] struct {
|
||||||
|
repo *RepoBase[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *DeleteMethod[T]) Init(repo *RepoBase[T]) {
|
||||||
|
m.repo = repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m DeleteMethod[T]) Delete(filter interface{}) (int64, error) {
|
||||||
|
var (
|
||||||
|
model T
|
||||||
|
)
|
||||||
|
|
||||||
|
query, err := smartfilter.ToQuery(model, filter, m.repo.dbConn)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
result := query.Delete(&model)
|
||||||
|
if result.Error != nil {
|
||||||
|
return 0, result.Error
|
||||||
|
}
|
||||||
|
return result.RowsAffected, nil
|
||||||
|
}
|
||||||
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
|
||||||
|
}
|
||||||
@ -5,6 +5,8 @@ import (
|
|||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DEFAULT_ID_FIELD = "id"
|
||||||
|
|
||||||
type MethodInitInterface[T schema.Tabler] interface {
|
type MethodInitInterface[T schema.Tabler] interface {
|
||||||
Init(repo *RepoBase[T])
|
Init(repo *RepoBase[T])
|
||||||
}
|
}
|
||||||
@ -22,6 +24,8 @@ type RepoBase[T schema.Tabler] struct {
|
|||||||
ExistsMethod[T]
|
ExistsMethod[T]
|
||||||
CountMethod[T]
|
CountMethod[T]
|
||||||
SaveMethod[T]
|
SaveMethod[T]
|
||||||
|
UpdateMethod[T]
|
||||||
|
DeleteMethod[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *RepoBase[T]) InitMethods(methods []MethodInitInterface[T]) {
|
func (repo *RepoBase[T]) InitMethods(methods []MethodInitInterface[T]) {
|
||||||
@ -35,7 +39,7 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
|||||||
|
|
||||||
if options == nil {
|
if options == nil {
|
||||||
// no options provided? set defaults
|
// no options provided? set defaults
|
||||||
m.IdField = "id"
|
m.IdField = DEFAULT_ID_FIELD
|
||||||
} else {
|
} else {
|
||||||
if len(options.IdField) > 0 {
|
if len(options.IdField) > 0 {
|
||||||
m.IdField = options.IdField
|
m.IdField = options.IdField
|
||||||
@ -48,6 +52,8 @@ 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.InitMethods(methods)
|
m.InitMethods(methods)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user