mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
type collectionRepository struct {
|
|
domain.BaseRepository[domain.Collection]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCollectionRepository creates a new CollectionRepository.
|
|
func NewCollectionRepository(db *gorm.DB) domain.CollectionRepository {
|
|
return &collectionRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Collection](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// ListByUserID finds collections by user ID
|
|
func (r *collectionRepository) ListByUserID(ctx context.Context, userID uint) ([]domain.Collection, error) {
|
|
var collections []domain.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) ([]domain.Collection, error) {
|
|
var collections []domain.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) ([]domain.Collection, error) {
|
|
var collections []domain.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
|
|
}
|