Skip to content

Commit

Permalink
Merge pull request #31 from bruce-mig/ft/get-transfer
Browse files Browse the repository at this point in the history
Ft/get transfer
  • Loading branch information
bruce-mig committed Mar 4, 2024
2 parents fcf3b8b + 23bfd01 commit 9c18912
Show file tree
Hide file tree
Showing 11 changed files with 991 additions and 140 deletions.
2 changes: 1 addition & 1 deletion doc/statik/statik.go

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions doc/swagger/simple_bank.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,39 @@
]
}
},
"/v1/transfers/{id}": {
"get": {
"summary": "Retrieve a single transfer by id",
"description": "Use this API to get a single transfer by transfer id",
"operationId": "SimpleBank_GetTransfer",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/pbGetTransferResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string",
"format": "int64"
}
],
"tags": [
"SimpleBank"
]
}
},
"/v1/update_user": {
"patch": {
"summary": "Update an existing user",
Expand Down Expand Up @@ -563,6 +596,14 @@
}
}
},
"pbGetTransferResponse": {
"type": "object",
"properties": {
"transfer": {
"$ref": "#/definitions/pbTransfer"
}
}
},
"pbListAccountsResponse": {
"type": "object",
"properties": {
Expand Down
87 changes: 87 additions & 0 deletions gapi/rpc_get_transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gapi

import (
"context"
"errors"

db "github.com/bruce-mig/simple-bank/db/sqlc"
"github.com/bruce-mig/simple-bank/pb"
"github.com/bruce-mig/simple-bank/util"
"github.com/bruce-mig/simple-bank/val"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (server *Server) GetTransfer(ctx context.Context, req *pb.GetTransferRequest) (*pb.GetTransferResponse, error) {
authPayload, err := server.authorizeUser(ctx, []string{util.BankerRole, util.DepositorRole})
if err != nil {
return nil, unauthenticatedError(err)
}

violations := validateGetTransferRequest(req)
if violations != nil {
return nil, invalidArgumentError(violations)
}

transfer, err := server.store.GetTransfer(ctx, req.GetId())
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, status.Errorf(
codes.NotFound,
"transfer not found",
)
}
return nil, status.Errorf(
codes.Internal,
"failed to get transfer",
)
}

fromAccount, err := server.store.GetAccount(ctx, transfer.FromAccountID)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, status.Errorf(
codes.NotFound,
"account not found",
)
}
return nil, status.Errorf(
codes.Internal,
"failed to get account",
)
}

toAccount, err := server.store.GetAccount(ctx, transfer.ToAccountID)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, status.Errorf(
codes.NotFound,
"account not found",
)
}
return nil, status.Errorf(
codes.Internal,
"failed to get account",
)
}
if authPayload.Role != util.BankerRole && fromAccount.Owner != authPayload.Username && toAccount.Owner != authPayload.Username {
return nil, status.Errorf(
codes.PermissionDenied,
"transfer doesn't belong to the authenticated user",
)
}

res := &pb.GetTransferResponse{
Transfer: convertTransfer(transfer),
}
return res, nil
}

func validateGetTransferRequest(req *pb.GetTransferRequest) (violations []*errdetails.BadRequest_FieldViolation) {
if err := val.ValidateTransferID(req.GetId()); err != nil {
violations = append(violations, fieldViolation("id", err))
}

return violations
}
Loading

0 comments on commit 9c18912

Please sign in to comment.