65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apskel-pos-be/internal/entities"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type NotificationRepository interface {
|
|
Create(ctx context.Context, notification *entities.Notification) error
|
|
GetByID(ctx context.Context, id uuid.UUID) (*entities.Notification, error)
|
|
Update(ctx context.Context, notification *entities.Notification) error
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Notification, int64, error)
|
|
}
|
|
|
|
type NotificationRepositoryImpl struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewNotificationRepository(db *gorm.DB) *NotificationRepositoryImpl {
|
|
return &NotificationRepositoryImpl{db: db}
|
|
}
|
|
|
|
func (r *NotificationRepositoryImpl) Create(ctx context.Context, notification *entities.Notification) error {
|
|
return r.db.WithContext(ctx).Create(notification).Error
|
|
}
|
|
|
|
func (r *NotificationRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.Notification, error) {
|
|
var notification entities.Notification
|
|
err := r.db.WithContext(ctx).First(¬ification, "id = ?", id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ¬ification, nil
|
|
}
|
|
|
|
func (r *NotificationRepositoryImpl) Update(ctx context.Context, notification *entities.Notification) error {
|
|
return r.db.WithContext(ctx).Save(notification).Error
|
|
}
|
|
|
|
func (r *NotificationRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Delete(&entities.Notification{}, "id = ?", id).Error
|
|
}
|
|
|
|
func (r *NotificationRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Notification, int64, error) {
|
|
var notifications []*entities.Notification
|
|
var total int64
|
|
|
|
query := r.db.WithContext(ctx).Model(&entities.Notification{})
|
|
for key, value := range filters {
|
|
query = query.Where(key+" = ?", value)
|
|
}
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := query.Order("created_at DESC").Limit(limit).Offset(offset).Find(¬ifications).Error
|
|
return notifications, total, err
|
|
}
|