36 lines
630 B
Go
36 lines
630 B
Go
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
|
|
}
|