Skip to content

Commit

Permalink
refactor code #3
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Nov 15, 2022
1 parent 6b993da commit 52d588f
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 174 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/Masterminds/squirrel v1.5.3
github.com/georgysavva/scany v1.2.1
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/golang/glog v1.0.0
github.com/google/uuid v1.3.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/cockroach-go/v2 v2.1.1/go.mod h1:7NtUnP6eK+l6k483WSYNrq3Kb23bWV10IRV1TyeSpwM=
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
github.com/cockroachdb/cockroach-go/v2 v2.2.0/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
Expand Down Expand Up @@ -410,6 +412,8 @@ github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXt
github.com/gabriel-vasile/mimetype v1.3.1/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/georgysavva/scany v1.2.1 h1:91PAMBpwBtDjvn46TaLQmuVhxpAG6p6sjQaU4zPHPSM=
github.com/georgysavva/scany v1.2.1/go.mod h1:vGBpL5XRLOocMFFa55pj0P04DrL3I7qKVRL49K6Eu5o=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -486,6 +490,8 @@ github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
Expand Down Expand Up @@ -1060,6 +1066,7 @@ github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
14 changes: 5 additions & 9 deletions internal/counter/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/rabbitmq/amqp091-go"
"github.com/thangchung/go-coffeeshop/cmd/counter/config"
"github.com/thangchung/go-coffeeshop/internal/counter/domain"
"github.com/thangchung/go-coffeeshop/internal/counter/features/orders/command"
"github.com/thangchung/go-coffeeshop/internal/counter/features/orders/eventhandlers"
"github.com/thangchung/go-coffeeshop/internal/counter/features/orders/query"
"github.com/thangchung/go-coffeeshop/internal/counter/features/orders/repo"
counterGrpc "github.com/thangchung/go-coffeeshop/internal/counter/grpc"
"github.com/thangchung/go-coffeeshop/pkg/event"
Expand Down Expand Up @@ -110,15 +108,14 @@ func (a *App) Run() error {

a.logger.Info("Order Publisher initialized")

// repository
orderRepo := repo.NewOrderRepo(pg)

// domain service
productDomainSvc := counterGrpc.NewProductDomainService(conn)

// CQRS components
orderQuery := query.NewOrderQuery(ctx, repo.NewOrderRepo(pg))
orderCommand := command.NewOrderCommand(ctx, repo.NewOrderRepo(pg))

// event handlers.
a.handler = eventhandlers.NewBaristaOrderUpdatedEventHandler(repo.NewOrderRepo(pg))
a.handler = eventhandlers.NewBaristaOrderUpdatedEventHandler(orderRepo)

// consumers
consumer, err := rabConsumer.NewConsumer(
Expand Down Expand Up @@ -162,8 +159,7 @@ func (a *App) Run() error {
amqpConn,
a.cfg,
a.logger,
orderCommand,
orderQuery,
orderRepo,
productDomainSvc,
*baristaOrderPub,
*kitchenOrderPub,
Expand Down
13 changes: 2 additions & 11 deletions internal/counter/domain/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

type (
OrderRepo interface {
GetAll(context.Context) ([]gen.OrderDto, error)
GetOrderByID(context.Context, uuid.UUID) (*Order, error)
GetAll(context.Context) ([]*Order, error)
GetByID(context.Context, uuid.UUID) (*Order, error)
Create(context.Context, *gen.OrderDto) error
Update(context.Context, *gen.OrderDto) (*gen.OrderDto, error)
}
Expand All @@ -20,15 +20,6 @@ type (
GetItemsByType(context.Context, *gen.PlaceOrderRequest, bool) (*gen.GetItemsByTypeResponse, error)
}

OrderCommand interface {
Create(context.Context, *gen.OrderDto) error
Update(context.Context, *gen.OrderDto) (*gen.OrderDto, error)
}

OrderQuery interface {
GetAll(ctx context.Context) ([]gen.OrderDto, error)
}

BaristaOrderUpdatedEventHandler interface {
Handle(context.Context, *event.BaristaOrderUpdated) error
}
Expand Down
13 changes: 7 additions & 6 deletions internal/counter/domain/line_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
)

type LineItem struct {
ID uuid.UUID
ItemType gen.ItemType
Name string
Price float32
ItemStatus gen.Status
IsBaristaOrder bool
ID uuid.UUID `json:"id" db:"id"`
ItemType gen.ItemType `json:"item_type" db:"item_type"`
Name string `json:"name" db:"name"`
Price float32 `json:"price" db:"price"`
ItemStatus gen.Status `json:"item_status" db:"item_status"`
IsBaristaOrder bool `json:"is_barista_order" db:"is_barista_order"`
OrderID uuid.UUID `json:"order_id" db:"order_id"` // shadow field
}

func NewLineItem(itemType gen.ItemType, name string, price float32, itemStatus gen.Status, isBarista bool) *LineItem {
Expand Down
8 changes: 8 additions & 0 deletions internal/counter/domain/model/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package model

import "github.com/thangchung/go-coffeeshop/internal/counter/domain"

type OrderListResult struct {
Order *domain.Order `db:"o"`
LineItem *domain.LineItem `db:"l"`
}
20 changes: 10 additions & 10 deletions internal/counter/domain/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
)

type Order struct {
ID uuid.UUID
OrderSource gen.OrderSource
LoyaltyMemberID uuid.UUID
OrderStatus gen.Status
Location gen.Location
LineItems []LineItem
ID uuid.UUID `json:"id" db:"id"`
OrderSource gen.OrderSource `json:"order_source" db:"order_source"`
LoyaltyMemberID uuid.UUID `json:"loyalty_member_id" db:"loyalty_member_id"`
OrderStatus gen.Status `json:"order_status" db:"order_status"`
Location gen.Location `json:"location" db:"location"`
LineItems []*LineItem
}

func NewOrder(
Expand Down Expand Up @@ -77,7 +77,7 @@ func CreateOrderFrom(
true,
)

order.LineItems = append(order.LineItems, *lineItem)
order.LineItems = append(order.LineItems, lineItem)
}
})

Expand Down Expand Up @@ -110,7 +110,7 @@ func CreateOrderFrom(
false,
)

order.LineItems = append(order.LineItems, *lineItem)
order.LineItems = append(order.LineItems, lineItem)
}
})

Expand All @@ -127,7 +127,7 @@ func (o *Order) Apply(event *events.BaristaOrderUpdated) error {
return nil // we dont do anything
}

_, index, ok := lo.FindIndexOf(o.LineItems, func(i LineItem) bool {
_, index, ok := lo.FindIndexOf(o.LineItems, func(i *LineItem) bool {
return i.ItemType == event.ItemType
})

Expand Down Expand Up @@ -196,7 +196,7 @@ func publishBaristaOrderEvent(
}
}

func checkFulfilledStatus(lineItems []LineItem) bool {
func checkFulfilledStatus(lineItems []*LineItem) bool {
for _, item := range lineItems {
if item.ItemStatus != gen.Status_FULFILLED {
return false
Expand Down
39 changes: 0 additions & 39 deletions internal/counter/features/orders/command/command.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewBaristaOrderUpdatedEventHandler(orderRepo domain.OrderRepo) domain.Baris
}

func (h *baristaOrderUpdatedEventHandler) Handle(ctx context.Context, e *event.BaristaOrderUpdated) error {
order, err := h.orderRepo.GetOrderByID(ctx, e.OrderID)
order, err := h.orderRepo.GetByID(ctx, e.OrderID)
if err != nil {
return fmt.Errorf("NewBaristaOrderUpdatedEventHandler-Handle-h.orderRepo.GetOrderByID(ctx, e.OrderID): %w", err)
}
Expand Down
30 changes: 0 additions & 30 deletions internal/counter/features/orders/query/query.go

This file was deleted.

Loading

0 comments on commit 52d588f

Please sign in to comment.