apskel-pos-backend/internal/repository/notification_delivery_repository.go
2026-05-10 10:57:38 +07:00

60 lines
2.0 KiB
Go

package repository
import (
"context"
"apskel-pos-be/internal/entities"
"github.com/google/uuid"
"gorm.io/gorm"
)
type NotificationDeliveryRepository interface {
Create(ctx context.Context, delivery *entities.NotificationDelivery) error
BulkCreate(ctx context.Context, deliveries []*entities.NotificationDelivery) error
GetByID(ctx context.Context, id uuid.UUID) (*entities.NotificationDelivery, error)
Update(ctx context.Context, delivery *entities.NotificationDelivery) error
ListByReceiverID(ctx context.Context, receiverID uuid.UUID) ([]*entities.NotificationDelivery, error)
}
type NotificationDeliveryRepositoryImpl struct {
db *gorm.DB
}
func NewNotificationDeliveryRepository(db *gorm.DB) *NotificationDeliveryRepositoryImpl {
return &NotificationDeliveryRepositoryImpl{db: db}
}
func (r *NotificationDeliveryRepositoryImpl) Create(ctx context.Context, delivery *entities.NotificationDelivery) error {
return r.db.WithContext(ctx).Create(delivery).Error
}
func (r *NotificationDeliveryRepositoryImpl) BulkCreate(ctx context.Context, deliveries []*entities.NotificationDelivery) error {
if len(deliveries) == 0 {
return nil
}
return r.db.WithContext(ctx).Create(&deliveries).Error
}
func (r *NotificationDeliveryRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.NotificationDelivery, error) {
var delivery entities.NotificationDelivery
err := r.db.WithContext(ctx).First(&delivery, "id = ?", id).Error
if err != nil {
return nil, err
}
return &delivery, nil
}
func (r *NotificationDeliveryRepositoryImpl) Update(ctx context.Context, delivery *entities.NotificationDelivery) error {
return r.db.WithContext(ctx).Save(delivery).Error
}
func (r *NotificationDeliveryRepositoryImpl) ListByReceiverID(ctx context.Context, receiverID uuid.UUID) ([]*entities.NotificationDelivery, error) {
var deliveries []*entities.NotificationDelivery
err := r.db.WithContext(ctx).
Where("notification_receiver_id = ?", receiverID).
Order("created_at DESC").
Find(&deliveries).Error
return deliveries, err
}