Skip to content

Commit

Permalink
Merge pull request #44 from PseudoMera/grpc
Browse files Browse the repository at this point in the history
Grpc
  • Loading branch information
PseudoMera authored Jun 13, 2024
2 parents caea171 + d3b6e16 commit 7d524e2
Show file tree
Hide file tree
Showing 21 changed files with 3,792 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ services:
args:
BUILD_PATH: user
ports:
- 3000:3000
- 3000:3000
- 3010:3010
restart: always
environment:
CONNECTION_STRING: "postgresql://postgres:postgres@db:5432/postgres?sslmode=disable"
HTTP_SERVER_PORT: "3000"
GRPC_SERVER_PORT: "3010"
env_file:
- .env
depends_on:
Expand All @@ -47,6 +49,7 @@ services:
BUILD_PATH: order
ports:
- 3001:3000
- 3011:3010
restart: always
environment:
CONNECTION_STRING: "postgresql://postgres:postgres@db:5432/postgres?sslmode=disable"
Expand All @@ -67,6 +70,7 @@ services:
BUILD_PATH: product
ports:
- 3002:3000
- 3012:3010
restart: always
environment:
CONNECTION_STRING: "postgresql://postgres:postgres@db:5432/postgres?sslmode=disable"
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/testcontainers/testcontainers-go v0.31.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.31.0
golang.org/x/crypto v0.23.0
google.golang.org/grpc v1.58.3
google.golang.org/protobuf v1.33.0
)

require (
Expand Down Expand Up @@ -56,11 +58,10 @@ require (
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731190214-cbb8c96f2d6d // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
9 changes: 9 additions & 0 deletions order/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import (
const (
connectionString = "CONNECTION_STRING"
httpServerPort = "HTTP_SERVER_PORT"
grpcServerPort = "GRPC_SERVER_PORT"
)

var (
errEmptyConnectionString = errors.New("env variable 'CONNECTION_STRING' cannot be empty")
errEmptyHTTPServerPort = errors.New("env variable 'HTTP_SERVER_PORT' cannot be empty")
errEmptyGRPCServerPort = errors.New("env variable 'GRPC_SERVER_PORT' cannot be empty")
)

type config struct {
connectionString string
httpServerPort string
grpcServerPort string
}

func getConfig() config {
Expand All @@ -31,8 +34,14 @@ func getConfig() config {
panic(errEmptyHTTPServerPort)
}

grpcPort := os.Getenv(grpcServerPort)
if grpcPort == "" {
panic(errEmptyGRPCServerPort)
}

return config{
connectionString: cstr,
httpServerPort: httpPort,
grpcServerPort: grpcPort,
}
}
119 changes: 119 additions & 0 deletions order/grpc/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package grpc

import (
context "context"

"github.com/PseudoMera/virtual-store/order/store"
)

type OrderServer struct {
db *store.Store
UnimplementedOrderServiceServer
}

func NewOrderServer(db *store.Store) *OrderServer {
return &OrderServer{
db: db,
}
}

func (os *OrderServer) CreateOrder(ctx context.Context, req *CreateOrderRequest) (*CreateOrderResponse, error) {
if req.UserID == 0 {
return nil, nil
}
if req.Status == "" {
return nil, nil
}
if req.TotalPrice == 0.0 {
return nil, nil
}

id, err := os.db.StoreOrder(ctx, store.Order{
UserID: int(req.UserID),
Status: store.OrderStatus(req.Status),
TotalPrice: float64(req.TotalPrice),
})

return &CreateOrderResponse{
Id: int64(id),
}, err
}

func (os *OrderServer) GetOrder(ctx context.Context, req *GetOrderRequest) (*Order, error) {
if req.Id == 0 {
return nil, nil
}

order, err := os.db.RetrieveOrder(ctx, int(req.Id))
return &Order{
Id: int64(order.ID),
UserID: int64(order.UserID),
TotalPrice: float32(order.TotalPrice),
Status: string(order.Status),
}, err
}

func (os *OrderServer) GetOrdersByUser(ctx context.Context, req *GetOrdersByUserRequest) (*GetOrdersByUserResponse, error) {
if req.UserID == 0 {
return nil, nil
}

orders, err := os.db.RetrieveOrdersByUserID(ctx, int(req.UserID))
if err != nil {
return nil, err
}

parsedOrders := make([]*Order, len(orders))
for i := range orders {
parsedOrders[i] = &Order{
Id: int64(orders[i].ID),
UserID: int64(orders[i].UserID),
TotalPrice: float32(orders[i].TotalPrice),
Status: string(orders[i].Status),
}
}

return &GetOrdersByUserResponse{
Orders: parsedOrders,
}, nil
}

func (os *OrderServer) UpdateOrder(ctx context.Context, req *UpdateOrderRequest) (*SuccessResponse, error) {
if req.Id == 0 {
return nil, nil
}
if req.Status == "" {
return nil, nil
}
if req.TotalPrice == 0.0 {
return nil, nil
}

if err := os.db.UpdateOrder(ctx, int(req.Id), store.Order{
ID: int(req.Id),
TotalPrice: float64(req.TotalPrice),
Status: store.OrderStatus(req.Status),
}); err != nil {
return nil, err
}

return &SuccessResponse{
Msg: "Success!",
}, nil
}

func (os *OrderServer) UpdateOrderStatus(ctx context.Context, req *UpdateOrderStatusRequest) (*SuccessResponse, error) {
if req.Id == 0 {
return nil, nil
}
if req.Status == "" {
return nil, nil
}
if err := os.db.UpdateOrderStatus(ctx, int(req.Id), store.OrderStatus(req.Status)); err != nil {
return nil, err
}

return &SuccessResponse{
Msg: "Success!",
}, nil
}
Loading

0 comments on commit 7d524e2

Please sign in to comment.