-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathgrpc.go
37 lines (29 loc) · 972 Bytes
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/genproto/users"
)
type GrpcServer struct {
db db
}
func (g GrpcServer) GetTrainingBalance(ctx context.Context, request *users.GetTrainingBalanceRequest) (*users.GetTrainingBalanceResponse, error) {
user, err := g.db.GetUser(ctx, request.UserId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &users.GetTrainingBalanceResponse{Amount: int64(user.Balance)}, nil
}
func (g GrpcServer) UpdateTrainingBalance(
ctx context.Context,
req *users.UpdateTrainingBalanceRequest,
) (*empty.Empty, error) {
err := g.db.UpdateBalance(ctx, req.UserId, int(req.AmountChange))
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to update balance: %s", err))
}
return &empty.Empty{}, nil
}