-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d83a5f2
commit 891db9a
Showing
7 changed files
with
186 additions
and
139 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
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
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) | ||
} | ||
) |
2 changes: 1 addition & 1 deletion
2
internal/product/entity/models.go → internal/product/domain/models.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package entity | ||
package domain | ||
|
||
type ItemTypeDto struct { | ||
Name string `json:"name"` | ||
|
101 changes: 101 additions & 0 deletions
101
internal/product/features/products/repo/products_postgres.go
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,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 | ||
} |
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,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 | ||
} |