Skip to content

Commit

Permalink
refactor: ♻️ cqrs refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Jan 19, 2024
1 parent 03a94a7 commit 31a10a5
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 33 deletions.
7 changes: 6 additions & 1 deletion internal/pkg/core/cqrs/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package cqrs

type command struct {
TypeInfo
Request
}

type Command interface {
isCommand()

Request
TypeInfo
}

func NewCommandByT[T any]() Command {
c := &command{TypeInfo: NewTypeInfoT[T]()}
c := &command{
TypeInfo: NewTypeInfoT[T](),
Request: NewRequest(),
}

return c
}
Expand Down
18 changes: 14 additions & 4 deletions internal/pkg/core/cqrs/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ func Test_Command(t *testing.T) {
assert.True(t, isImplementedCommand)

var i interface{} = command
_, ok := i.(Command)
_, ok2 := i.(TypeInfo)
assert.True(t, ok)
assert.True(t, ok2)
_, isCommand := i.(Command)
_, isTypeInfo := i.(TypeInfo)
_, isQuery := i.(Query)
_, isRequest := i.(Request)

assert.True(t, isCommand)
assert.True(t, isTypeInfo)
assert.True(t, isRequest)
assert.False(t, isQuery)

assert.True(t, IsCommand(command))
assert.True(t, IsRequest(command))
assert.False(t, IsQuery(command))

assert.Equal(t, command.ShortTypeName(), "*CreateProductTest")
assert.Equal(t, command.FullTypeName(), "*cqrs.CreateProductTest")
}
Expand Down
30 changes: 30 additions & 0 deletions internal/pkg/core/cqrs/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cqrs

type notification struct {
TypeInfo
}

type Notification interface {
isNotification()

TypeInfo
}

func NewNotificationByT[T any]() Notification {
n := &notification{
TypeInfo: NewTypeInfoT[T](),
}

return n
}

func (c *notification) isNotification() {
}

func IsNotification(obj interface{}) bool {
if _, ok := obj.(Notification); ok {
return true
}

return false
}
10 changes: 8 additions & 2 deletions internal/pkg/core/cqrs/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package cqrs

type query struct {
TypeInfo
Request
}

type Query interface {
TypeInfo
isQuery()

Request
TypeInfo
}

func NewQueryByT[T any]() Query {
return &query{TypeInfo: NewTypeInfoT[T]()}
return &query{
TypeInfo: NewTypeInfoT[T](),
Request: NewRequest(),
}
}

func (q *query) isQuery() {
Expand Down
40 changes: 23 additions & 17 deletions internal/pkg/core/cqrs/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@ package cqrs
import (
"testing"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/typemapper"

uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)

func Test_Query(t *testing.T) {
query := &GetProductById{
Query: NewQueryByT[GetProductById](),
Query: NewQueryByT[*GetProductById](),
ProductID: uuid.NewV4(),
}

isImplementedQuery := typemapper.ImplementedInterfaceT[Query](query)
assert.True(t, isImplementedQuery)

var i interface{} = query
_, isQuery := i.(Query)
_, isTypeInfo := i.(TypeInfo)
_, isCommand := i.(Command)
_, isRequest := i.(Request)

assert.True(t, isQuery)
assert.False(t, isCommand)
assert.True(t, isTypeInfo)
assert.True(t, isRequest)

assert.True(t, IsQuery(query))
}
assert.False(t, IsCommand(query))
assert.True(t, IsRequest(query))

//
//func Test_Query_Is_Catstable_To_Command(t *testing.T) {
// var q Query = NewQuery()
// var c Command = commands.NewCommand()
// query, qok := q.(Query)
// command, cok := c.(Command)
// assert.True(t, qok)
// assert.True(t, cok)
// assert.NotNil(t, query)
// assert.NotNil(t, command)
//
// query, qok = command.(Query)
// assert.False(t, qok)
// assert.Nil(t, query)
//}
assert.Equal(t, query.ShortTypeName(), "*GetProductById")
assert.Equal(t, query.FullTypeName(), "*cqrs.GetProductById")
}

type GetProductById struct {
Query

ProductID uuid.UUID
}
22 changes: 22 additions & 0 deletions internal/pkg/core/cqrs/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cqrs

type request struct{}

type Request interface {
isRequest()
}

func NewRequest() Request {
return &request{}
}

func (r *request) isRequest() {
}

func IsRequest(obj interface{}) bool {
if _, ok := obj.(Request); ok {
return true
}

return false
}
7 changes: 5 additions & 2 deletions internal/pkg/core/cqrs/tx_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cqrs

// https://www.mohitkhare.com/blog/go-naming-conventions/
type ITxRequest interface {
IsTxRequest() bool

type TxRequest interface {
Request

isTxRequest() bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (m *mediatorTransactionPipeline) Handle(
) (interface{}, error) {
requestName := typeMapper.GetSnakeTypeName(request)

txRequest, ok := request.(cqrs.ITxRequest)
if !ok || !txRequest.IsTxRequest() {
txRequest, ok := request.(cqrs.TxRequest)
if !ok || !txRequest.isTxRequest() {
return next(ctx)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewCreateProductWithValidation(
return command, err
}

func (c *CreateProduct) IsTxRequest() bool {
func (c *CreateProduct) isTxRequest() bool {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewDeleteProductWithValidation(productID uuid.UUID) (*DeleteProduct, error)
}

// IsTxRequest for enabling transactions on the mediatr pipeline
func (c *DeleteProduct) IsTxRequest() bool {
func (c *DeleteProduct) isTxRequest() bool {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *deleteProductHandler) RegisterHandler() error {
}

// IsTxRequest for enabling transactions on the mediatr pipeline
func (c *deleteProductHandler) IsTxRequest() bool {
func (c *deleteProductHandler) isTxRequest() bool {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewUpdateProductWithValidation(
}

// IsTxRequest for enabling transactions on the mediatr pipeline
func (c *UpdateProduct) IsTxRequest() bool {
func (c *UpdateProduct) isTxRequest() bool {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *updateProductHandler) RegisterHandler() error {
}

// IsTxRequest for enabling transactions on the mediatr pipeline
func (c *updateProductHandler) IsTxRequest() bool {
func (c *updateProductHandler) isTxRequest() bool {
return true
}

Expand Down

0 comments on commit 31a10a5

Please sign in to comment.