Skip to content

Commit

Permalink
add events #3
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Oct 27, 2022
1 parent e8ea4e6 commit c8d0229
Show file tree
Hide file tree
Showing 16 changed files with 799 additions and 143 deletions.
24 changes: 23 additions & 1 deletion client.http
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,26 @@ content-type: application/json

###
GET {{host}}/v1/fulfillment-orders HTTP/1.1
content-type: application/json
content-type: application/json

###
POST {{host}}/v1/api/orders HTTP/1.1
content-type: application/json

{
"commandType": 0,
"orderSource": 0,
"location": 0,
"loyaltyMemberId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"baristaItems": [
{
"itemType": {{$randomInt 0 5}}
}
],
"kitchenItems": [
{
"itemType": {{$randomInt 6 9}}
}
],
"timestamp": "2022-07-04T11:38:00.210Z"
}
5 changes: 3 additions & 2 deletions cmd/barista/event/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ func (c *Consumer) Listen(topics []string) error {
var payload Payload

_ = json.Unmarshal(d.Body, &payload)
messageType := d.Type

go func() {
switch payload.Name {
case "drink_made":
switch messageType {
case "barista.ordered":
fmt.Println(payload)
default:
fmt.Println("default")
Expand Down
114 changes: 57 additions & 57 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,66 @@ services:
networks:
- coffeeshop-network

proxy:
build:
context: .
dockerfile: ./docker/Dockerfile-proxy
image: go-coffeeshop-proxy
environment:
APP_NAME: 'proxy-service in docker'
GRPC_PRODUCT_HOST: 'product'
GRPC_PRODUCT_PORT: 5001
GRPC_COUNTER_HOST: 'counter'
GRPC_COUNTER_PORT: 5002
ports:
- 5000:5000
depends_on:
- product
- counter
networks:
- coffeeshop-network
# proxy:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-proxy
# image: go-coffeeshop-proxy
# environment:
# APP_NAME: 'proxy-service in docker'
# GRPC_PRODUCT_HOST: 'product'
# GRPC_PRODUCT_PORT: 5001
# GRPC_COUNTER_HOST: 'counter'
# GRPC_COUNTER_PORT: 5002
# ports:
# - 5000:5000
# depends_on:
# - product
# - counter
# networks:
# - coffeeshop-network

product:
build:
context: .
dockerfile: ./docker/Dockerfile-product
image: go-coffeeshop-product
environment:
APP_NAME: 'product-service in docker'
ports:
- 5001:5001
networks:
- coffeeshop-network
# product:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-product
# image: go-coffeeshop-product
# environment:
# APP_NAME: 'product-service in docker'
# ports:
# - 5001:5001
# networks:
# - coffeeshop-network

counter:
build:
context: .
dockerfile: ./docker/Dockerfile-counter
image: go-coffeeshop-counter
environment:
APP_NAME: 'counter-service in docker'
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
ports:
- 5002:5002
depends_on:
- postgres
- rabbitmq
networks:
- coffeeshop-network
# counter:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-counter
# image: go-coffeeshop-counter
# environment:
# APP_NAME: 'counter-service in docker'
# RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
# ports:
# - 5002:5002
# depends_on:
# - postgres
# - rabbitmq
# networks:
# - coffeeshop-network

barista:
build:
context: .
dockerfile: ./docker/Dockerfile-barista
image: go-coffeeshop-barista
environment:
APP_NAME: 'barista-service in docker'
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
depends_on:
- postgres
- rabbitmq
networks:
- coffeeshop-network
# barista:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-barista
# image: go-coffeeshop-barista
# environment:
# APP_NAME: 'barista-service in docker'
# RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
# depends_on:
# - postgres
# - rabbitmq
# networks:
# - coffeeshop-network

networks:
coffeeshop-network:
9 changes: 6 additions & 3 deletions docker/Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ RUN LATEST_COMPOSE_VERSION=$(curl -sSL "https://api.github.com/repos/docker/comp
&& curl -sSL "https://github.com/docker/compose/releases/download/${LATEST_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o ~/.docker/cli-plugins/docker-compose \
&& chmod +x ~/.docker/cli-plugins/docker-compose

# Substitute BIN for your bin directory.
# Substitute VERSION for the current released version.
# Install Buf
RUN BIN="/usr/local/bin" && \
VERSION="1.8.0" && \
curl -sSL \
"https://github.com/bufbuild/buf/releases/download/v${VERSION}/buf-$(uname -s)-$(uname -m)" \
-o "${BIN}/buf" && \
chmod +x "${BIN}/buf"
chmod +x "${BIN}/buf"

# Install dbmate
RUN curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64 \
&& chmod +x /usr/local/bin/dbmate
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/golang/glog v1.0.0
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0
github.com/ilyakaznacheev/cleanenv v1.3.0
github.com/rabbitmq/amqp091-go v1.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0 h1:kr3j8iIMR4ywO/O0rvksXaJvauGGCMg2zAZIiNZ9uIQ=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0/go.mod h1:ummNFgdgLhhX7aIiy35vVmQNS0rWXknfPE0qe6fmFXg=
github.com/ilyakaznacheev/cleanenv v1.3.0 h1:RapuLclPPUbmdd5Bi5UXScwMEZA6+ZNLU5OW9itPjj0=
Expand Down
40 changes: 40 additions & 0 deletions internal/counter/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"time"

"github.com/google/uuid"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/thangchung/go-coffeeshop/cmd/counter/config"
"github.com/thangchung/go-coffeeshop/internal/counter/event"
mylogger "github.com/thangchung/go-coffeeshop/pkg/logger"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
"google.golang.org/grpc"
Expand Down Expand Up @@ -85,6 +88,43 @@ func (g *CounterServiceServerImpl) GetListOrderFulfillment(ctx context.Context,
func (g *CounterServiceServerImpl) PlaceOrder(ctx context.Context, request *gen.PlaceOrderRequest) (*gen.PlaceOrderResponse, error) {
g.logger.Info("POST: PlaceOrder")

fmt.Println(request)

ch, err := g.rabbitConn.Channel()
if err != nil {
panic(err)
}
defer ch.Close()

eventBytes, err := json.Marshal(event.BaristaOrdered{
OrderID: uuid.New(),
ItemLineID: uuid.UUID,
})
if err != nil {
g.logger.LogError(err)
}

err = ch.PublishWithContext(
ctx,
OrderTopic,
"log.INFO",
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Type: "barista.ordered",
Body: eventBytes,
},
)

if err != nil {
g.logger.LogError(err)

return nil, err
}

g.logger.Info("Sending message: %s -> %s", event, "orders_topic")

res := gen.PlaceOrderResponse{}

return &res, nil
Expand Down
26 changes: 26 additions & 0 deletions internal/counter/entity/line_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package entity

import (
"github.com/google/uuid"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
)

type LineItem struct {
ID uuid.UUID
ItemType gen.ItemType
Name string
Price float32
ItemStatus gen.Status
IsBaristaOrder bool
}

func NewLineItem(itemType gen.ItemType, name string, price float32, itemStatus gen.Status, isBarista bool) *LineItem {
return &LineItem{
ID: uuid.New(),
ItemType: itemType,
Name: name,
Price: price,
ItemStatus: itemStatus,
IsBaristaOrder: isBarista,
}
}
44 changes: 44 additions & 0 deletions internal/counter/entity/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package entity

import (
"github.com/google/uuid"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
)

type Order struct {
ID uuid.UUID
OrderSource gen.OrderSource
LoyaltyMemberID uuid.UUID
OrderStatus gen.Status
Location gen.Location
LineItems []LineItem
}

func NewOrder(orderSource gen.OrderSource, loyaltyMemberID uuid.UUID, orderStatus gen.Status, location gen.Location) *Order {
return &Order{
ID: uuid.New(),
OrderSource: orderSource,
LoyaltyMemberID: loyaltyMemberID,
OrderStatus: orderStatus,
Location: location,
}
}

func (o *Order) From(request *gen.PlaceOrderRequest) (*Order, error) {
loyaltyMemberID, err := uuid.Parse(request.LoyaltyMemberId)
if err != nil {
return nil, err
}

order := NewOrder(request.OrderSource, loyaltyMemberID, gen.Status_IN_PROGRESS, request.Location)

if len(request.BaristaItems) > 0 {

}

if len(request.KitchenItems) > 0 {

}

return order, nil
}
40 changes: 40 additions & 0 deletions internal/counter/event/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package event

import (
"time"

"github.com/google/uuid"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
)

type BaristaOrdered struct {
OrderID uuid.UUID `json:"orderId"`
ItemLineID uuid.UUID `json:"itemLineId"`
ItemType gen.ItemType `json:"itemType"`
}

type KitchenOrdered struct {
OrderID uuid.UUID `json:"orderId"`
ItemLineID uuid.UUID `json:"itemLineId"`
ItemType gen.ItemType `json:"itemType"`
}

type BaristaOrderUpdated struct {
OrderID uuid.UUID `json:"orderId"`
ItemLineID uuid.UUID `json:"itemLineId"`
Name string `json:"name"`
ItemType gen.ItemType `json:"itemType"`
TimeIn time.Time `json:"timeIn"`
MadeBy string `json:"madeBy"`
TimeUp time.Time `json:"timeUp"`
}

type KitchenOrderUpdated struct {
OrderID uuid.UUID `json:"orderId"`
ItemLineID uuid.UUID `json:"itemLineId"`
Name string `json:"name"`
ItemType gen.ItemType `json:"itemType"`
TimeIn time.Time `json:"timeIn"`
MadeBy string `json:"madeBy"`
TimeUp time.Time `json:"timeUp"`
}

0 comments on commit c8d0229

Please sign in to comment.