-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_create_account.go
42 lines (31 loc) · 1.01 KB
/
rpc_create_account.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
38
39
40
41
42
package gapi
import (
"context"
db "github.com/dbracic21-foi/simplebank/db/sqlc"
"github.com/dbracic21-foi/simplebank/pb"
"github.com/dbracic21-foi/simplebank/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (server *Server) CreateAccount(ctx context.Context, req *pb.CreateAccountRequest) (*pb.CreateAccountResponse, error) {
authPayload, err := server.authorizationUser(ctx, []string{util.BankRole, util.DepositorRole})
if err != nil {
return nil, unauthenticatedError(err)
}
if authPayload.Username != req.GetOwner() {
return nil, status.Errorf(codes.PermissionDenied, "cannot create account for other user")
}
arg := db.CreateAccountParams{
Owner: req.GetOwner(),
Balance: req.GetBalance(),
Currency: req.GetCurrency(),
}
account, err := server.store.CreateAccount(ctx, arg)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create user %s", err)
}
rsp := &pb.CreateAccountResponse{
Account: convertAccount(account),
}
return rsp, nil
}