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

58 lines
1.8 KiB
Go

package repositories
import (
"context"
"gorm.io/gorm"
"tercul/internal/models"
)
// CollectionRepository defines CRUD methods specific to Collection.
type CollectionRepository interface {
BaseRepository[models.Collection]
ListByUserID(ctx context.Context, userID uint) ([]models.Collection, error)
ListPublic(ctx context.Context) ([]models.Collection, error)
ListByWorkID(ctx context.Context, workID uint) ([]models.Collection, error)
}
type collectionRepository struct {
BaseRepository[models.Collection]
db *gorm.DB
}
// NewCollectionRepository creates a new CollectionRepository.
func NewCollectionRepository(db *gorm.DB) CollectionRepository {
return &collectionRepository{
BaseRepository: NewBaseRepositoryImpl[models.Collection](db),
db: db,
}
}
// ListByUserID finds collections by user ID
func (r *collectionRepository) ListByUserID(ctx context.Context, userID uint) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}
// ListPublic finds public collections
func (r *collectionRepository) ListPublic(ctx context.Context) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Where("is_public = ?", true).Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}
// ListByWorkID finds collections by work ID
func (r *collectionRepository) ListByWorkID(ctx context.Context, workID uint) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Joins("JOIN collection_works ON collection_works.collection_id = collections.id").
Where("collection_works.work_id = ?", workID).
Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}