package contactlinks import ( "context" "errors" "github.com/google/uuid" "gorm.io/gorm" ) var ErrNotFound = errors.New("contact link not found") type Repository interface { Create(ctx context.Context, link *ContactLink) error FindByID(ctx context.Context, id uuid.UUID) (*ContactLink, error) List(ctx context.Context, activeOnly bool) ([]*ContactLink, error) Update(ctx context.Context, link *ContactLink) error Delete(ctx context.Context, id uuid.UUID) error } type gormRepository struct { db *gorm.DB } func NewRepository(db *gorm.DB) Repository { return &gormRepository{db: db} } func (r *gormRepository) Create(ctx context.Context, link *ContactLink) error { return r.db.WithContext(ctx).Create(link).Error } func (r *gormRepository) FindByID(ctx context.Context, id uuid.UUID) (*ContactLink, error) { var link ContactLink err := r.db.WithContext(ctx).Where("id = ?", id).First(&link).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrNotFound } if err != nil { return nil, err } return &link, nil } func (r *gormRepository) List(ctx context.Context, activeOnly bool) ([]*ContactLink, error) { q := r.db.WithContext(ctx).Order("position asc, label asc") if activeOnly { q = q.Where("is_active = ?", true) } var list []*ContactLink if err := q.Find(&list).Error; err != nil { return nil, err } return list, nil } func (r *gormRepository) Update(ctx context.Context, link *ContactLink) error { return r.db.WithContext(ctx).Save(link).Error } func (r *gormRepository) Delete(ctx context.Context, id uuid.UUID) error { res := r.db.WithContext(ctx).Delete(&ContactLink{}, "id = ?", id) if res.Error != nil { return res.Error } if res.RowsAffected == 0 { return ErrNotFound } return nil }