pg_role_access_repository.go 3.0 KB
package repository

import (
	"fmt"
	"github.com/tiptok/gocomm/common"
	. "github.com/tiptok/gocomm/pkg/orm/pgx"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)

type RoleAccessRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *RoleAccessRepository) Save(dm *domain.RoleAccess) (*domain.RoleAccess, error) {
	var (
		err error
		m   = &models.RoleAccess{}
		tx  = repository.transactionContext.PgTx
	)
	if err = common.GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if err = tx.Insert(m); err != nil {
			return nil, err
		}
		return dm, nil
	}
	if err = tx.Update(m); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *RoleAccessRepository) Remove(RoleAccess *domain.RoleAccess) (*domain.RoleAccess, error) {
	var (
		tx              = repository.transactionContext.PgTx
		RoleAccessModel = &models.RoleAccess{Id: RoleAccess.Identify().(int64)}
	)
	if _, err := tx.Model(RoleAccessModel).Where("id = ?", RoleAccess.Id).Delete(); err != nil {
		return RoleAccess, err
	}
	return RoleAccess, nil
}

func (repository *RoleAccessRepository) FindOne(queryOptions map[string]interface{}) (*domain.RoleAccess, error) {
	tx := repository.transactionContext.PgTx
	RoleAccessModel := new(models.RoleAccess)
	query := NewQuery(tx.Model(RoleAccessModel), queryOptions)
	query.SetWhere("id = ?", "id")
	if err := query.First(); err != nil {
		return nil, fmt.Errorf("query row not found")
	}
	if RoleAccessModel.Id == 0 {
		return nil, fmt.Errorf("query row not found")
	}
	return repository.transformPgModelToDomainModel(RoleAccessModel)
}

func (repository *RoleAccessRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RoleAccess, error) {
	tx := repository.transactionContext.PgTx
	var RoleAccessModels []*models.RoleAccess
	RoleAccesss := make([]*domain.RoleAccess, 0)
	query := NewQuery(tx.Model(&RoleAccessModels), queryOptions).
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime")
	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, RoleAccesss, err
	}
	for _, RoleAccessModel := range RoleAccessModels {
		if RoleAccess, err := repository.transformPgModelToDomainModel(RoleAccessModel); err != nil {
			return 0, RoleAccesss, err
		} else {
			RoleAccesss = append(RoleAccesss, RoleAccess)
		}
	}
	return int64(query.AffectRow), RoleAccesss, nil
}

func (repository *RoleAccessRepository) transformPgModelToDomainModel(RoleAccessModel *models.RoleAccess) (*domain.RoleAccess, error) {
	m := &domain.RoleAccess{}
	err := common.GobModelTransform(m, RoleAccessModel)
	return m, err
}

func NewRoleAccessRepository(transactionContext *transaction.TransactionContext) (*RoleAccessRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &RoleAccessRepository{transactionContext: transactionContext}, nil
}