109 lines
3.6 KiB
Go
109 lines
3.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apskel-pos-be/internal/entities"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type NotificationReceiverRepository interface {
|
|
Create(ctx context.Context, receiver *entities.NotificationReceiver) error
|
|
BulkCreate(ctx context.Context, receivers []*entities.NotificationReceiver) error
|
|
GetByID(ctx context.Context, id uuid.UUID) (*entities.NotificationReceiver, error)
|
|
GetByNotificationAndUser(ctx context.Context, notificationID, userID uuid.UUID) (*entities.NotificationReceiver, error)
|
|
Update(ctx context.Context, receiver *entities.NotificationReceiver) error
|
|
ListByUserID(ctx context.Context, userID uuid.UUID, isRead *bool, limit, offset int) ([]*entities.NotificationReceiver, int64, error)
|
|
CountUnreadByUserID(ctx context.Context, userID uuid.UUID) (int64, error)
|
|
SoftDeleteByID(ctx context.Context, id uuid.UUID) error
|
|
}
|
|
|
|
type NotificationReceiverRepositoryImpl struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewNotificationReceiverRepository(db *gorm.DB) *NotificationReceiverRepositoryImpl {
|
|
return &NotificationReceiverRepositoryImpl{db: db}
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) Create(ctx context.Context, receiver *entities.NotificationReceiver) error {
|
|
return r.db.WithContext(ctx).Create(receiver).Error
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) BulkCreate(ctx context.Context, receivers []*entities.NotificationReceiver) error {
|
|
if len(receivers) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.WithContext(ctx).Create(&receivers).Error
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.NotificationReceiver, error) {
|
|
var receiver entities.NotificationReceiver
|
|
err := r.db.WithContext(ctx).
|
|
Preload("Notification").
|
|
First(&receiver, "id = ? AND is_deleted = false", id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &receiver, nil
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) GetByNotificationAndUser(ctx context.Context, notificationID, userID uuid.UUID) (*entities.NotificationReceiver, error) {
|
|
var receiver entities.NotificationReceiver
|
|
err := r.db.WithContext(ctx).
|
|
Where("notification_id = ? AND user_id = ? AND is_deleted = false", notificationID, userID).
|
|
First(&receiver).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &receiver, nil
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) Update(ctx context.Context, receiver *entities.NotificationReceiver) error {
|
|
return r.db.WithContext(ctx).Save(receiver).Error
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) ListByUserID(ctx context.Context, userID uuid.UUID, isRead *bool, limit, offset int) ([]*entities.NotificationReceiver, int64, error) {
|
|
var receivers []*entities.NotificationReceiver
|
|
var total int64
|
|
|
|
query := r.db.WithContext(ctx).
|
|
Model(&entities.NotificationReceiver{}).
|
|
Where("user_id = ? AND is_deleted = false", userID)
|
|
|
|
if isRead != nil {
|
|
query = query.Where("is_read = ?", *isRead)
|
|
}
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := query.
|
|
Preload("Notification").
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&receivers).Error
|
|
|
|
return receivers, total, err
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) CountUnreadByUserID(ctx context.Context, userID uuid.UUID) (int64, error) {
|
|
var count int64
|
|
err := r.db.WithContext(ctx).
|
|
Model(&entities.NotificationReceiver{}).
|
|
Where("user_id = ? AND is_read = false AND is_deleted = false", userID).
|
|
Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *NotificationReceiverRepositoryImpl) SoftDeleteByID(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).
|
|
Model(&entities.NotificationReceiver{}).
|
|
Where("id = ?", id).
|
|
Updates(map[string]interface{}{"is_deleted": true}).Error
|
|
}
|