审查视图

pkg/infrastructure/repository/pg_received_message_repository.go 1.6 KB
tangxvhui authored
1 2 3 4 5
package repository

import (
	"time"
tangxvhui authored
6
	"github.com/go-pg/pg/v10"
tangxvhui authored
7 8 9 10 11 12 13 14 15 16 17
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)

type ReceivedMessageRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.ReceivedMessageRepository = (*ReceivedMessageRepository)(nil)
tangxvhui authored
18 19 20 21 22 23
func NewReceivedMessageRepository(tx *pgTransaction.TransactionContext) *ReceivedMessageRepository {
	return &ReceivedMessageRepository{
		transactionContext: tx,
	}
}
tangxvhui authored
24 25 26 27 28 29
func (repo *ReceivedMessageRepository) SaveMessage(param *domain.ReceivedMessage) error {
	message := &models.ReceivedMessage{
		MessageId:   param.MessageId,
		MessageType: param.MessageType,
		MessageBody: param.MessageBody,
		OccurredOn:  param.OccurredOn,
庄敏学 authored
30
		CreatedAt:   time.Now(),
tangxvhui authored
31 32 33 34 35 36 37 38 39 40
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(message).
		Insert()
	return err
}

func (repo *ReceivedMessageRepository) FindMessage(messageId int64) (*domain.ReceivedMessage, error) {
	tx := repo.transactionContext.PgTx
	receivedMessageModel := new(models.ReceivedMessage)
tangxvhui authored
41 42
	query := tx.Model(receivedMessageModel).
		Where("message_id = ?", messageId)
tangxvhui authored
43
	if err := query.First(); err != nil {
tangxvhui authored
44
		if err != pg.ErrNoRows {
tangxvhui authored
45 46 47 48 49 50 51 52
			return nil, err
		}
	}
	message := &domain.ReceivedMessage{
		MessageId:   receivedMessageModel.MessageId,
		MessageType: receivedMessageModel.MessageType,
		MessageBody: receivedMessageModel.MessageBody,
		OccurredOn:  receivedMessageModel.OccurredOn,
庄敏学 authored
53
		CreatedAt:   receivedMessageModel.CreatedAt,
tangxvhui authored
54 55 56 57
	}
	return message, nil

}