notification
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user