Skip to content

Commit

Permalink
refactor product-service #3
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Nov 21, 2022
1 parent d83a5f2 commit 891db9a
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 139 deletions.
33 changes: 0 additions & 33 deletions cmd/barista/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,4 @@ func main() {
glog.Fatal(err)
os.Exit(1)
}

// amqpConn, err := rabbitmq.NewRabbitMQConn(cfg.RabbitMQ.URL, logger)
// if err != nil {
// logger.Fatal("app - Run - rabbitmq.NewRabbitMQConn: %s", err.Error())
// }
// defer amqpConn.Close()

// handler := eventhandlers.NewBaristaOrderedEventHandler()
// consumer, err := baristaRabbitMQ.NewOrderConsumer(amqpConn, handler, logger)

// if err != nil {
// logger.Fatal("app - Run - baristaRabbitMQ.NewOrderConsumer: %s", err.Error())
// }

// ctx, cancel := context.WithCancel(context.Background())

// go func() {
// err := consumer.StartConsumer(cfg.RabbitMQ.WorkerPoolSize, cfg.RabbitMQ.Exchange, cfg.RabbitMQ.Queue, cfg.RabbitMQ.RoutingKey, cfg.RabbitMQ.ConsumerTag)
// if err != nil {
// logger.Error("StartConsumer: %v", err)
// cancel()
// }
// }()

// quit := make(chan os.Signal, 1)
// signal.Notify(quit, os.Interrupt, syscall.SIGTERM)

// select {
// case v := <-quit:
// logger.Error("signal.Notify: %v", v)
// case done := <-ctx.Done():
// logger.Error("ctx.Done: %v", done)
// }
}
4 changes: 0 additions & 4 deletions cmd/product/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"fmt"
"os"
"reflect"

"github.com/golang/glog"
"github.com/thangchung/go-coffeeshop/cmd/product/config"
Expand All @@ -18,8 +16,6 @@ func main() {
glog.Fatal(err)
}

fmt.Println(reflect.TypeOf(struct{}{}))

mylog := mylogger.New(cfg.Level)

a := app.New(mylog, cfg)
Expand Down
105 changes: 4 additions & 101 deletions internal/product/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package app
import (
"context"
"net"
"strings"

"github.com/thangchung/go-coffeeshop/cmd/product/config"
productRepo "github.com/thangchung/go-coffeeshop/internal/product/features/products/repo"
productGrpc "github.com/thangchung/go-coffeeshop/internal/product/grpc"
mylogger "github.com/thangchung/go-coffeeshop/pkg/logger"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
"google.golang.org/grpc"
)

Expand All @@ -18,100 +18,6 @@ type App struct {
address string
}

type ProductServiceServerImpl struct {
gen.UnimplementedProductServiceServer
logger *mylogger.Logger
}

var ItemTypes = map[string]gen.ItemTypeDto{
"CAPPUCCINO": {
Name: "CAPPUCCINO",
Type: 0,
Price: 4.5,
},
"COFFEE_BLACK": {
Name: "COFFEE_BLACK",
Type: 1,
Price: 3,
},
"COFFEE_WITH_ROOM": {
Name: "COFFEE_WITH_ROOM",
Type: 2,
Price: 3,
},
"ESPRESSO": {
Name: "ESPRESSO",
Type: 3,
Price: 3.5,
},
"ESPRESSO_DOUBLE": {
Name: "ESPRESSO_DOUBLE",
Type: 4,
Price: 4.5,
},
"LATTE": {
Name: "LATTE",
Type: 5,
Price: 4.5,
},
"CAKEPOP": {
Name: "CAKEPOP",
Type: 6,
Price: 2.5,
},
"CROISSANT": {
Name: "CROISSANT",
Type: 7,
Price: 3.25,
},
"MUFFIN": {
Name: "MUFFIN",
Type: 8,
Price: 3,
},
"CROISSANT_CHOCOLATE": {
Name: "CROISSANT_CHOCOLATE",
Type: 9,
Price: 3.5,
},
}

func (g *ProductServiceServerImpl) GetItemTypes(ctx context.Context, request *gen.GetItemTypesRequest) (*gen.GetItemTypesResponse, error) {
g.logger.Info("GET: GetItemTypes")

res := gen.GetItemTypesResponse{}

for _, v := range ItemTypes {
res.ItemTypes = append(res.ItemTypes, &gen.ItemTypeDto{
Name: v.Name,
Type: v.Type,
Price: v.Price,
})
}

return &res, nil
}

func (g *ProductServiceServerImpl) GetItemsByType(ctx context.Context, request *gen.GetItemsByTypeRequest) (*gen.GetItemsByTypeResponse, error) {
g.logger.Info("GET: GetItemsByType with %s", request.ItemTypes)

res := gen.GetItemsByTypeResponse{}

itemTypes := strings.Split(request.ItemTypes, ",")

for _, itemType := range itemTypes {
item := ItemTypes[itemType]
if item.Name != "" {
res.Items = append(res.Items, &gen.ItemDto{
Price: item.Price,
Type: item.Type,
})
}
}

return &res, nil
}

func New(log *mylogger.Logger, cfg *config.Config) *App {
return &App{
logger: log,
Expand All @@ -125,10 +31,7 @@ func (a *App) Run(ctx context.Context) error {
a.logger.Info("Init %s %s\n", a.cfg.Name, a.cfg.Version)

// Repository
// ...

// Use case
// ...
repo := productRepo.NewOrderRepo()

// gRPC Server
l, err := net.Listen(a.network, a.address)
Expand All @@ -143,7 +46,7 @@ func (a *App) Run(ctx context.Context) error {
}()

s := grpc.NewServer()
gen.RegisterProductServiceServer(s, &ProductServiceServerImpl{logger: a.logger})
productGrpc.NewProductServiceServerGrpc(s, a.logger, repo)

go func() {
defer s.GracefulStop()
Expand Down
14 changes: 14 additions & 0 deletions internal/product/domain/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain

import (
"context"

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

type (
ProductRepo interface {
GetAll(context.Context) ([]*gen.ItemTypeDto, error)
GetByTypes(context.Context, []string) ([]*gen.ItemDto, error)
}
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package entity
package domain

type ItemTypeDto struct {
Name string `json:"name"`
Expand Down
101 changes: 101 additions & 0 deletions internal/product/features/products/repo/products_postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package repo

import (
"context"

"github.com/thangchung/go-coffeeshop/internal/product/domain"
"github.com/thangchung/go-coffeeshop/proto/gen"
)

var _ domain.ProductRepo = (*productRepo)(nil)

type productRepo struct {
itemTypes map[string]gen.ItemTypeDto
}

func NewOrderRepo() domain.ProductRepo {
return &productRepo{
itemTypes: map[string]gen.ItemTypeDto{
"CAPPUCCINO": {
Name: "CAPPUCCINO",
Type: 0,
Price: 4.5,
},
"COFFEE_BLACK": {
Name: "COFFEE_BLACK",
Type: 1,
Price: 3,
},
"COFFEE_WITH_ROOM": {
Name: "COFFEE_WITH_ROOM",
Type: 2,
Price: 3,
},
"ESPRESSO": {
Name: "ESPRESSO",
Type: 3,
Price: 3.5,
},
"ESPRESSO_DOUBLE": {
Name: "ESPRESSO_DOUBLE",
Type: 4,
Price: 4.5,
},
"LATTE": {
Name: "LATTE",
Type: 5,
Price: 4.5,
},
"CAKEPOP": {
Name: "CAKEPOP",
Type: 6,
Price: 2.5,
},
"CROISSANT": {
Name: "CROISSANT",
Type: 7,
Price: 3.25,
},
"MUFFIN": {
Name: "MUFFIN",
Type: 8,
Price: 3,
},
"CROISSANT_CHOCOLATE": {
Name: "CROISSANT_CHOCOLATE",
Type: 9,
Price: 3.5,
},
},
}
}

func (p *productRepo) GetAll(ctx context.Context) ([]*gen.ItemTypeDto, error) {
results := make([]*gen.ItemTypeDto, 0)

for _, v := range p.itemTypes {
results = append(results, &gen.ItemTypeDto{
Name: v.Name,
Type: v.Type,
Price: v.Price,
})
}

return results, nil
}

func (p *productRepo) GetByTypes(ctx context.Context, itemTypes []string) ([]*gen.ItemDto, error) {
results := make([]*gen.ItemDto, 0)

for _, itemType := range itemTypes {
item := p.itemTypes[itemType]
if item.Name != "" {
results = append(results, &gen.ItemDto{
Price: item.Price,
Type: item.Type,
})
}
}

return results, nil
}
66 changes: 66 additions & 0 deletions internal/product/grpc/product_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package grpc

import (
"context"
"strings"

"github.com/pkg/errors"
"github.com/thangchung/go-coffeeshop/internal/product/domain"
mylogger "github.com/thangchung/go-coffeeshop/pkg/logger"
"github.com/thangchung/go-coffeeshop/proto/gen"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

type ProductServiceServerImpl struct {
gen.UnimplementedProductServiceServer
repo domain.ProductRepo
logger *mylogger.Logger
}

func NewProductServiceServerGrpc(
grpcServer *grpc.Server,
log *mylogger.Logger,
repo domain.ProductRepo,
) {
svc := ProductServiceServerImpl{
logger: log,
repo: repo,
}

gen.RegisterProductServiceServer(grpcServer, &svc)

reflection.Register(grpcServer)
}

func (g *ProductServiceServerImpl) GetItemTypes(ctx context.Context, request *gen.GetItemTypesRequest) (*gen.GetItemTypesResponse, error) {
g.logger.Info("GET: GetItemTypes")

res := gen.GetItemTypesResponse{}

results, err := g.repo.GetAll(ctx)
if err != nil {
return nil, errors.Wrap(err, "ProductServiceServerImpl-g.repo.GetAll")
}

res.ItemTypes = append(res.ItemTypes, results...)

return &res, nil
}

func (g *ProductServiceServerImpl) GetItemsByType(ctx context.Context, request *gen.GetItemsByTypeRequest) (*gen.GetItemsByTypeResponse, error) {
g.logger.Info("GET: GetItemsByType with %s", request.ItemTypes)

res := gen.GetItemsByTypeResponse{}

itemTypes := strings.Split(request.ItemTypes, ",")

results, err := g.repo.GetByTypes(ctx, itemTypes)
if err != nil {
return nil, errors.Wrap(err, "ProductServiceServerImpl-g.repo.GetItemsByType")
}

res.Items = append(res.Items, results...)

return &res, nil
}

0 comments on commit 891db9a

Please sign in to comment.