refactored for maintainability
This commit is contained in:
19
internal/db/isunique.go
Normal file
19
internal/db/isunique.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func IsUnique(ctx context.Context, tx bun.Tx, model any, field, value string) (bool, error) {
|
||||
count, err := tx.NewSelect().
|
||||
Model(model).
|
||||
Where("? = ?", bun.Ident(field), value).
|
||||
Count(ctx)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "tx.NewSelect")
|
||||
}
|
||||
return count == 0, nil
|
||||
}
|
||||
Reference in New Issue
Block a user