tercul-backend/internal/repositories/edge_repository.go
Damir Mukimov fa336cacf3
wip
2025-09-01 00:43:59 +02:00

36 lines
977 B
Go

package repositories
import (
"context"
"gorm.io/gorm"
"tercul/internal/models"
)
// EdgeRepository defines CRUD operations for the polymorphic edge table.
type EdgeRepository interface {
BaseRepository[models.Edge]
ListBySource(ctx context.Context, sourceTable string, sourceID uint) ([]models.Edge, error)
}
type edgeRepository struct {
BaseRepository[models.Edge]
db *gorm.DB
}
// NewEdgeRepository creates a new EdgeRepository.
func NewEdgeRepository(db *gorm.DB) EdgeRepository {
return &edgeRepository{
BaseRepository: NewBaseRepositoryImpl[models.Edge](db),
db: db,
}
}
// ListBySource finds edges by source table and ID
func (r *edgeRepository) ListBySource(ctx context.Context, sourceTable string, sourceID uint) ([]models.Edge, error) {
var edges []models.Edge
if err := r.db.WithContext(ctx).Where("source_table = ? AND source_id = ?", sourceTable, sourceID).Find(&edges).Error; err != nil {
return nil, err
}
return edges, nil
}