Cleanup
This commit is contained in:
35
app/repository/method_exists.go
Normal file
35
app/repository/method_exists.go
Normal file
@ -0,0 +1,35 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type ExistsMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
|
||||
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
|
||||
var (
|
||||
model T
|
||||
)
|
||||
|
||||
query := m.DbConn.Model(model)
|
||||
|
||||
query, err := smartfilter.ToQuery(model, filter, query)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
result := query.Select("*").First(&model)
|
||||
|
||||
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
|
||||
return exists, nil
|
||||
}
|
||||
Reference in New Issue
Block a user