pg_read_sent_notification_service.go 1.5 KB
package domain_service

import (
	"fmt"
	coreDomain "github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
)

type ReadSentNotificationService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (service *ReadSentNotificationService) Read(sentNotificationId int64) (*domain.SentNotification, error) {
	var sentNotificationRepository domain.SentNotificationRepository
	if repository, err := repository.NewSentNotificationRepository(service.transactionContext); err != nil {
		return nil, err
	} else {
		sentNotificationRepository = repository
	}
	sentNotification, err := sentNotificationRepository.FindOne(map[string]interface{}{
		"sentNotificationId": sentNotificationId,
	})
	if err != nil {
		return nil, err
	}
	if sentNotification == nil {
		return nil, fmt.Errorf("无效的任务")
	}
	if err := sentNotification.Read(); err != nil {
		return nil, err
	}
	if sentNotification, err := sentNotificationRepository.Save(sentNotification); err != nil {
		return nil, err
	} else {
		return sentNotification, nil
	}
}

func NewReadSentNotificationService(transactionContext *pgTransaction.TransactionContext) (*ReadSentNotificationService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &ReadSentNotificationService{
			transactionContext: transactionContext,
		}, nil
	}
}