-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(marketing): 添加新event及生产者、引入业务无关的event key生成器,修复相关测试 (#134)
Signed-off-by: longyue0521 <[email protected]>
- Loading branch information
1 parent
4497161
commit fea1a31
Showing
9 changed files
with
366 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
internal/marketing/internal/event/mocks/permission.mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
internal/marketing/internal/event/producer/credit_event_producer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package producer | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ecodeclub/mq-api" | ||
"github.com/ecodeclub/webook/internal/marketing/internal/event" | ||
) | ||
|
||
//go:generate mockgen -source=./credit_event_producer.go -package=evtmocks -destination=../mocks/credit.mock.go -typed CreditEventProducer | ||
type CreditEventProducer interface { | ||
Produce(ctx context.Context, evt event.CreditIncreaseEvent) error | ||
} | ||
|
||
type creditEventProducer struct { | ||
producer mq.Producer | ||
} | ||
|
||
func NewCreditEventProducer(q mq.MQ) (CreditEventProducer, error) { | ||
producer, err := q.Producer(event.PermissionEventName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &creditEventProducer{ | ||
producer: producer, | ||
}, nil | ||
} | ||
|
||
func (s *creditEventProducer) Produce(ctx context.Context, evt event.CreditIncreaseEvent) error { | ||
data, err := json.Marshal(&evt) | ||
if err != nil { | ||
return fmt.Errorf("序列化失败: %w", err) | ||
} | ||
_, err = s.producer.Produce(ctx, &mq.Message{ | ||
Key: []byte(evt.Key), | ||
Value: data, | ||
}) | ||
return err | ||
} |
55 changes: 55 additions & 0 deletions
55
internal/marketing/internal/event/producer/permission_event_producer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 ecodeclub | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package producer | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ecodeclub/mq-api" | ||
"github.com/ecodeclub/webook/internal/marketing/internal/event" | ||
) | ||
|
||
//go:generate mockgen -source=./permission_event_producer.go -package=evtmocks -destination=../mocks/permission.mock.go -typed PermissionEventProducer | ||
type PermissionEventProducer interface { | ||
Produce(ctx context.Context, evt event.PermissionEvent) error | ||
} | ||
|
||
type permissionEventProducer struct { | ||
producer mq.Producer | ||
} | ||
|
||
func NewPermissionEventProducer(q mq.MQ) (PermissionEventProducer, error) { | ||
producer, err := q.Producer(event.PermissionEventName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &permissionEventProducer{ | ||
producer: producer, | ||
}, nil | ||
} | ||
|
||
func (s *permissionEventProducer) Produce(ctx context.Context, evt event.PermissionEvent) error { | ||
data, err := json.Marshal(&evt) | ||
if err != nil { | ||
return fmt.Errorf("序列化失败: %w", err) | ||
} | ||
_, err = s.producer.Produce(ctx, &mq.Message{ | ||
Key: []byte(evt.Key), | ||
Value: data, | ||
}) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.