fix[BE]: validate query page/limit defaults and add exists helpers

This commit is contained in:
aguhh18
2025-10-23 22:00:55 +07:00
parent 1b02b660b9
commit 79b3dd47b8
19 changed files with 105 additions and 1 deletions
@@ -2,6 +2,7 @@ package repository
import (
"context"
"fmt"
"gorm.io/gorm"
)
@@ -32,3 +33,21 @@ func ExistsByName[T any](ctx context.Context, db *gorm.DB, name string, excludeI
}
return count > 0, nil
}
func ExistsByField[T any](ctx context.Context, db *gorm.DB, field string, value any, excludeID *uint) (bool, error) {
if field == "" {
return false, fmt.Errorf("field is required")
}
var count int64
q := db.WithContext(ctx).
Model(new(T)).
Where(fmt.Sprintf("%s = ?", field), value).
Where("deleted_at IS NULL")
if excludeID != nil {
q = q.Where("id <> ?", *excludeID)
}
if err := q.Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}