access.go 4.9 KB
package service

import (
	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/access/command"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/access/query"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
)

type AccessService struct {
}

func (svr *AccessService) CreateAccess(header *protocol.RequestHeader, request *command.CreateAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.CreateAccessResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newAccess := &domain.Access{
		AccessName: request.AccessName,
		AccessCode: request.AccessCode,
		AccessType: request.AccessType,
		Sort:       request.Sort,
		Object:     request.Object,
		Action:     request.Action,
		Module:     request.Module,
	}

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	if m, err := AccessRepository.Save(newAccess); err != nil {
		return nil, err
	} else {
		rsp = m
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) UpdateAccess(header *protocol.RequestHeader, request *command.UpdateAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.UpdateAccessResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if err = access.Update(common.ObjectToMap(request)); err != nil {
		return
	}
	if access, err = AccessRepository.Save(access); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) GetAccess(header *protocol.RequestHeader, request *query.GetAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &query.GetAccessResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = access
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) DeleteAccess(header *protocol.RequestHeader, request *command.DeleteAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &command.DeleteAccessResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access *domain.Access
	if access, err = AccessRepository.FindOne(common.ObjectToMap(request)); err != nil {
		return
	}
	if access, err = AccessRepository.Remove(access); err != nil {
		return
	}
	rsp = access
	err = transactionContext.CommitTransaction()
	return
}

func (svr *AccessService) ListAccess(header *protocol.RequestHeader, request *query.ListAccessRequest) (rsp interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &query.ListAccessResponse{}
	if err = request.ValidateCommand(); err != nil {
		err = protocol.NewCustomMessage(2, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
	var access []*domain.Access
	var total int64
	if total, access, err = AccessRepository.Find(common.ObjectToMap(request)); err != nil {
		return
	}
	rsp = map[string]interface{}{
		"total": total,
		"list":  access,
	}
	err = transactionContext.CommitTransaction()
	return
}

func NewAccessService(options map[string]interface{}) *AccessService {
	svr := &AccessService{}
	return svr
}