mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/models"
|
|
)
|
|
|
|
// EditionRepository defines CRUD methods specific to Edition.
|
|
type EditionRepository interface {
|
|
BaseRepository[models.Edition]
|
|
ListByBookID(ctx context.Context, bookID uint) ([]models.Edition, error)
|
|
FindByISBN(ctx context.Context, isbn string) (*models.Edition, error)
|
|
}
|
|
|
|
type editionRepository struct {
|
|
BaseRepository[models.Edition]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewEditionRepository creates a new EditionRepository.
|
|
func NewEditionRepository(db *gorm.DB) EditionRepository {
|
|
return &editionRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[models.Edition](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// ListByBookID finds editions by book ID
|
|
func (r *editionRepository) ListByBookID(ctx context.Context, bookID uint) ([]models.Edition, error) {
|
|
var editions []models.Edition
|
|
if err := r.db.WithContext(ctx).Where("book_id = ?", bookID).Find(&editions).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return editions, nil
|
|
}
|
|
|
|
// FindByISBN finds an edition by ISBN
|
|
func (r *editionRepository) FindByISBN(ctx context.Context, isbn string) (*models.Edition, error) {
|
|
var edition models.Edition
|
|
if err := r.db.WithContext(ctx).Where("isbn = ?", isbn).First(&edition).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &edition, nil
|
|
}
|