80 lines
2.9 KiB
Go
80 lines
2.9 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"veza-backend-api/internal/models"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// NotificationRepository defines the interface for notification data access
|
|
type NotificationRepository interface {
|
|
Create(ctx context.Context, notification *models.Notification) error
|
|
GetByUserID(ctx context.Context, userID uuid.UUID, unreadOnly bool, limit int) ([]*models.Notification, error)
|
|
MarkAsRead(ctx context.Context, notificationID, userID uuid.UUID) error
|
|
MarkAllAsRead(ctx context.Context, userID uuid.UUID) error
|
|
Delete(ctx context.Context, notificationID, userID uuid.UUID) error
|
|
DeleteAll(ctx context.Context, userID uuid.UUID) error
|
|
CountUnread(ctx context.Context, userID uuid.UUID) (int64, error)
|
|
}
|
|
|
|
type notificationRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewNotificationRepository creates a new NotificationRepository
|
|
func NewNotificationRepository(db *gorm.DB) NotificationRepository {
|
|
return ¬ificationRepository{db: db}
|
|
}
|
|
|
|
func (r *notificationRepository) Create(ctx context.Context, notification *models.Notification) error {
|
|
return r.db.WithContext(ctx).Create(notification).Error
|
|
}
|
|
|
|
func (r *notificationRepository) GetByUserID(ctx context.Context, userID uuid.UUID, unreadOnly bool, limit int) ([]*models.Notification, error) {
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
query := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
|
if unreadOnly {
|
|
query = query.Where("read = ?", false)
|
|
}
|
|
var notifications []*models.Notification
|
|
if err := query.Order("created_at DESC").Limit(limit).Find(¬ifications).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return notifications, nil
|
|
}
|
|
|
|
func (r *notificationRepository) MarkAsRead(ctx context.Context, notificationID, userID uuid.UUID) error {
|
|
now := time.Now()
|
|
return r.db.WithContext(ctx).Model(&models.Notification{}).
|
|
Where("id = ? AND user_id = ?", notificationID, userID).
|
|
Updates(map[string]interface{}{"read": true, "read_at": now}).Error
|
|
}
|
|
|
|
func (r *notificationRepository) MarkAllAsRead(ctx context.Context, userID uuid.UUID) error {
|
|
now := time.Now()
|
|
return r.db.WithContext(ctx).Model(&models.Notification{}).
|
|
Where("user_id = ? AND read = ?", userID, false).
|
|
Updates(map[string]interface{}{"read": true, "read_at": now}).Error
|
|
}
|
|
|
|
func (r *notificationRepository) Delete(ctx context.Context, notificationID, userID uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Where("id = ? AND user_id = ?", notificationID, userID).
|
|
Delete(&models.Notification{}).Error
|
|
}
|
|
|
|
func (r *notificationRepository) DeleteAll(ctx context.Context, userID uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&models.Notification{}).Error
|
|
}
|
|
|
|
func (r *notificationRepository) CountUnread(ctx context.Context, userID uuid.UUID) (int64, error) {
|
|
var count int64
|
|
err := r.db.WithContext(ctx).Model(&models.Notification{}).
|
|
Where("user_id = ? AND read = ?", userID, false).Count(&count).Error
|
|
return count, err
|
|
}
|