Skip to content

Commit

Permalink
add wallet to services client
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jul 27, 2022
1 parent f30451e commit 81ca749
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import (
"github.com/micro/services/url/proto"
"github.com/micro/services/user/proto"
"github.com/micro/services/vehicle/proto"
"github.com/micro/services/wallet/proto"
"github.com/micro/services/weather/proto"
"github.com/micro/services/wordle/proto"
"github.com/micro/services/youtube/proto"
Expand Down Expand Up @@ -144,6 +145,7 @@ type Client struct {
Url url.UrlService
User user.UserService
Vehicle vehicle.VehicleService
Wallet wallet.WalletService
Weather weather.WeatherService
Wordle wordle.WordleService
Youtube youtube.YoutubeService
Expand Down Expand Up @@ -219,6 +221,7 @@ func NewClient(c client.Client) *Client {
Url: url.NewUrlService("url", c),
User: user.NewUserService("user", c),
Vehicle: vehicle.NewVehicleService("vehicle", c),
Wallet: wallet.NewWalletService("wallet", c),
Weather: weather.NewWeatherService("weather", c),
Wordle: wordle.NewWordleService("wordle", c),
Youtube: youtube.NewYoutubeService("youtube", c),
Expand Down
2 changes: 1 addition & 1 deletion services.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

SERVICES=`find . -maxdepth 2 -type d -name proto | cut -f 2 -d /`
SERVICES=`find . -maxdepth 2 -type d -name proto | cut -f 2 -d / | sort`

cat << EOF
package services
Expand Down
14 changes: 11 additions & 3 deletions wallet/handler/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func (b *Wallet) Transfer(ctx context.Context, req *pb.TransferRequest, rsp *pb.
return errors.BadRequest("wallet.transfer", "missing ids")
}

// check the wallets exist
for _, id := range []string{req.FromId, req.ToId} {
_, err := store.Read(fmt.Sprintf("%s/%s/%s", accountPrefix, tnt, id), store.ReadLimit(1))
if err != nil {
return errors.BadRequest("wallet.transfer", "invalid account")
}
}

amount, err := b.c.Read(ctx, redis.Key(tnt, req.FromId), "$balance$")
if amount < req.Amount {
return errors.BadRequest("wallet.transfer", "insufficient credit")
Expand Down Expand Up @@ -387,9 +395,9 @@ func (w *Wallet) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResp

// add default
rsp.Accounts = append(rsp.Accounts, &pb.Account{
Id: "default",
Name: "Default account",
Balance: bal,
Id: "default",
Name: "Default account",
Balance: bal,
})

return nil
Expand Down

0 comments on commit 81ca749

Please sign in to comment.