Repo options

This commit is contained in:
Eden Kirin
2024-06-25 01:19:40 +02:00
parent c2bcbea5d2
commit 4615d55309
8 changed files with 112 additions and 37 deletions

View File

@ -9,11 +9,11 @@ import (
)
type ExistsMethod[T schema.Tabler] struct {
DbConn *gorm.DB
repo *RepoBase[T]
}
func (m *ExistsMethod[T]) Init(dbConn *gorm.DB) {
m.DbConn = dbConn
func (m *ExistsMethod[T]) Init(repo *RepoBase[T]) {
m.repo = repo
}
func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
@ -21,14 +21,14 @@ func (m ExistsMethod[T]) Exists(filter interface{}) (bool, error) {
model T
)
query := m.DbConn.Model(model)
query := m.repo.dbConn.Model(model)
query, err := smartfilter.ToQuery(model, filter, query)
if err != nil {
return false, err
}
result := query.Select("*").First(&model)
result := query.Select(m.repo.IdField).Take(&model)
exists := !errors.Is(result.Error, gorm.ErrRecordNotFound) && result.Error == nil
return exists, nil