mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
35 lines
765 B
Go
35 lines
765 B
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Exists reports whether a record with the given ID exists for type T.
|
|
func Exists[T any](ctx context.Context, db *gorm.DB, id uint) (bool, error) {
|
|
var count int64
|
|
if err := db.WithContext(ctx).
|
|
Model(new(T)).
|
|
Where("id = ?", id).
|
|
Count(&count).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
func ExistsByName[T any](ctx context.Context, db *gorm.DB, name string, excludeID *uint) (bool, error) {
|
|
var count int64
|
|
q := db.WithContext(ctx).
|
|
Model(new(T)).
|
|
Where("name = ?", name).
|
|
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
|
|
}
|