update_app_page_logic.go 1.6 KB
package page

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAppPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateAppPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAppPageLogic {
	return &UpdateAppPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateAppPageLogic) UpdateAppPage(req *types.AppPageUpdateRequest) (resp *types.AppPageUpdateResponse, err error) {
	var (
		conn    = l.svcCtx.DefaultDBConn()
		appPage *domain.AppPage
	)
	if appPage, err = l.svcCtx.AppPageRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErr(err)
	}
	resp = &types.AppPageUpdateResponse{}
	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		appPage.Charts = req.Charts
		if req.Cover != "" {
			appPage.Cover = req.Cover
		}
		if req.Name != "" {
			appPage.Name = req.Name
		}
		if appPage, err = l.svcCtx.AppPageRepository.UpdateWithVersion(l.ctx, conn, appPage); err != nil {
			return err
		}
		return nil
	}, true); err != nil {
		return nil, xerr.NewErrMsgErr("更新页面失败", err)
	}
	return
}