101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"accounting-app/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Transaction image repository errors
|
|
var (
|
|
ErrTransactionImageNotFound = errors.New("transaction image not found")
|
|
ErrMaxImagesExceeded = errors.New("maximum images per transaction exceeded")
|
|
)
|
|
|
|
// TransactionImageRepository handles database operations for transaction images
|
|
type TransactionImageRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewTransactionImageRepository creates a new TransactionImageRepository instance
|
|
func NewTransactionImageRepository(db *gorm.DB) *TransactionImageRepository {
|
|
return &TransactionImageRepository{db: db}
|
|
}
|
|
|
|
// Create creates a new transaction image in the database
|
|
func (r *TransactionImageRepository) Create(image *models.TransactionImage) error {
|
|
if err := r.db.Create(image).Error; err != nil {
|
|
return fmt.Errorf("failed to create transaction image: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a transaction image by its ID
|
|
func (r *TransactionImageRepository) GetByID(id uint) (*models.TransactionImage, error) {
|
|
var image models.TransactionImage
|
|
if err := r.db.First(&image, id).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrTransactionImageNotFound
|
|
}
|
|
return nil, fmt.Errorf("failed to get transaction image: %w", err)
|
|
}
|
|
return &image, nil
|
|
}
|
|
|
|
// GetByTransactionID retrieves all images for a specific transaction
|
|
func (r *TransactionImageRepository) GetByTransactionID(transactionID uint) ([]models.TransactionImage, error) {
|
|
var images []models.TransactionImage
|
|
if err := r.db.Where("transaction_id = ?", transactionID).
|
|
Order("created_at ASC").
|
|
Find(&images).Error; err != nil {
|
|
return nil, fmt.Errorf("failed to get transaction images: %w", err)
|
|
}
|
|
return images, nil
|
|
}
|
|
|
|
// CountByTransactionID returns the count of images for a transaction
|
|
func (r *TransactionImageRepository) CountByTransactionID(transactionID uint) (int64, error) {
|
|
var count int64
|
|
if err := r.db.Model(&models.TransactionImage{}).
|
|
Where("transaction_id = ?", transactionID).
|
|
Count(&count).Error; err != nil {
|
|
return 0, fmt.Errorf("failed to count transaction images: %w", err)
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// Delete deletes a transaction image by its ID
|
|
func (r *TransactionImageRepository) Delete(id uint) error {
|
|
result := r.db.Delete(&models.TransactionImage{}, id)
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to delete transaction image: %w", result.Error)
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return ErrTransactionImageNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteByTransactionID deletes all images for a specific transaction
|
|
func (r *TransactionImageRepository) DeleteByTransactionID(transactionID uint) error {
|
|
if err := r.db.Where("transaction_id = ?", transactionID).
|
|
Delete(&models.TransactionImage{}).Error; err != nil {
|
|
return fmt.Errorf("failed to delete transaction images: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ExistsByID checks if a transaction image with the given ID exists
|
|
func (r *TransactionImageRepository) ExistsByID(id uint) (bool, error) {
|
|
var count int64
|
|
if err := r.db.Model(&models.TransactionImage{}).
|
|
Where("id = ?", id).
|
|
Count(&count).Error; err != nil {
|
|
return false, fmt.Errorf("failed to check transaction image existence: %w", err)
|
|
}
|
|
return count > 0, nil
|
|
}
|