-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ some refactoring in cqrs
- Loading branch information
1 parent
5ff36d2
commit 03a94a7
Showing
78 changed files
with
1,288 additions
and
651 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cqrs | ||
|
||
type command struct { | ||
TypeInfo | ||
} | ||
|
||
type Command interface { | ||
isCommand() | ||
|
||
TypeInfo | ||
} | ||
|
||
func NewCommandByT[T any]() Command { | ||
c := &command{TypeInfo: NewTypeInfoT[T]()} | ||
|
||
return c | ||
} | ||
|
||
func (c *command) isCommand() { | ||
} | ||
|
||
func IsCommand(obj interface{}) bool { | ||
if _, ok := obj.(Command); ok { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,15 @@ | ||
package cqrs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/mehdihadeli/go-mediatr" | ||
) | ||
|
||
type CommandHandler[TCommand Command, TResponse any] interface { | ||
Handle(ctx context.Context, command TCommand) (TResponse, error) | ||
} | ||
|
||
type CommandHandlerVoid[TCommand Command] interface { | ||
Handle(ctx context.Context, command TCommand) (*mediatr.Unit, error) | ||
} |
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,44 @@ | ||
package cqrs | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/typemapper" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
uuid "github.com/satori/go.uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Command(t *testing.T) { | ||
command := &CreateProductTest{ | ||
Command: NewCommandByT[*CreateProductTest](), | ||
ProductID: uuid.NewV4(), | ||
Name: gofakeit.Name(), | ||
CreatedAt: time.Now(), | ||
Description: gofakeit.AdjectiveDescriptive(), | ||
Price: gofakeit.Price(100, 1000), | ||
} | ||
|
||
isImplementedCommand := typemapper.ImplementedInterfaceT[Command](command) | ||
assert.True(t, isImplementedCommand) | ||
|
||
var i interface{} = command | ||
_, ok := i.(Command) | ||
_, ok2 := i.(TypeInfo) | ||
assert.True(t, ok) | ||
assert.True(t, ok2) | ||
assert.Equal(t, command.ShortTypeName(), "*CreateProductTest") | ||
assert.Equal(t, command.FullTypeName(), "*cqrs.CreateProductTest") | ||
} | ||
|
||
type CreateProductTest struct { | ||
Command | ||
|
||
Name string | ||
ProductID uuid.UUID | ||
Description string | ||
Price float64 | ||
CreatedAt time.Time | ||
} |
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,25 @@ | ||
package cqrs | ||
|
||
type internalCommand struct { | ||
Command | ||
} | ||
|
||
type InternalCommand interface { | ||
Command | ||
isInternalCommand() | ||
} | ||
|
||
func NewInternalCommandByT[T any]() InternalCommand { | ||
return &internalCommand{Command: NewCommandByT[T]()} | ||
} | ||
|
||
func (c *internalCommand) isInternalCommand() { | ||
} | ||
|
||
func IsInternalCommand(obj interface{}) bool { | ||
if _, ok := obj.(InternalCommand); ok { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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 @@ | ||
package cqrs |
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,25 @@ | ||
package cqrs | ||
|
||
type query struct { | ||
TypeInfo | ||
} | ||
|
||
type Query interface { | ||
TypeInfo | ||
isQuery() | ||
} | ||
|
||
func NewQueryByT[T any]() Query { | ||
return &query{TypeInfo: NewTypeInfoT[T]()} | ||
} | ||
|
||
func (q *query) isQuery() { | ||
} | ||
|
||
func IsQuery(obj interface{}) bool { | ||
if _, ok := obj.(Query); ok { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,9 @@ | ||
package cqrs | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type QueryHandler[TQuery Query, TResponse any] interface { | ||
Handle(ctx context.Context, query TQuery) (TResponse, error) | ||
} |
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,38 @@ | ||
package cqrs | ||
|
||
import ( | ||
"testing" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Query(t *testing.T) { | ||
query := &GetProductById{ | ||
Query: NewQueryByT[GetProductById](), | ||
ProductID: uuid.NewV4(), | ||
} | ||
|
||
assert.True(t, IsQuery(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) | ||
//} | ||
|
||
type GetProductById struct { | ||
Query | ||
ProductID uuid.UUID | ||
} |
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,39 @@ | ||
package cqrs | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/typemapper" | ||
) | ||
|
||
type TypeInfo interface { | ||
ShortTypeName() string | ||
FullTypeName() string | ||
Type() reflect.Type | ||
} | ||
|
||
type typeInfo struct { | ||
shortTypeName string | ||
fullTypeName string | ||
typ reflect.Type | ||
} | ||
|
||
func NewTypeInfoT[T any]() TypeInfo { | ||
name := typemapper.GetGenericTypeNameByT[T]() | ||
fullName := typemapper.GetGenericFullTypeNameByT[T]() | ||
typ := typemapper.GetGenericTypeByT[T]() | ||
|
||
return &typeInfo{fullTypeName: fullName, typ: typ, shortTypeName: name} | ||
} | ||
|
||
func (t *typeInfo) ShortTypeName() string { | ||
return t.shortTypeName | ||
} | ||
|
||
func (t *typeInfo) FullTypeName() string { | ||
return t.fullTypeName | ||
} | ||
|
||
func (t *typeInfo) Type() reflect.Type { | ||
return t.typ | ||
} |
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
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
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
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
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
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
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
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.