handler.go 1.2 KB
package goqueue

import (
	rawjson "encoding/json"
	"fmt"
	"github.com/linmadan/egglib-go/utils/json"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/service"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
)

func UpToChainHandler(k, v string) error {
	log.Logger.Debug(fmt.Sprintf("%s", v), map[string]interface{}{"handler": "UptoChain"})
	blockChainService := service.NewBlockChainService(nil)
	upChainCommand := &command.UpChainCommand{}
	err := json.UnmarshalFromString(v, upChainCommand)
	if err != nil {
		return err
	}
	_, err = blockChainService.UpChain(upChainCommand)
	return err
}

func FilterTopicHandler(topic string, handler func(string, string) error) func(string, string) error {
	return func(k string, v string) error {
		raw := &RawData{}
		err := json.UnmarshalFromString(v, raw)
		if err != nil {
			return err
		}
		if raw.Topic != topic {
			return nil
		}
		return handler(k, string(raw.Data))
	}
}

type RawData struct {
	Topic string             `json:"topic"`
	Data  rawjson.RawMessage `json:"data"`
}