-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add create account, list accounts, list entries RPCs
- Loading branch information
Showing
36 changed files
with
2,593 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
db "github.com/aradwann/eenergy/db/store" | ||
"github.com/aradwann/eenergy/pb" | ||
"github.com/aradwann/eenergy/util" | ||
"github.com/aradwann/eenergy/val" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"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.authorizeUser(ctx, []string{util.AdminRole, util.UserRole}) | ||
if err != nil { | ||
return nil, unauthenticatedError(err) | ||
} | ||
violations := validateCreateAccountRequest(req) | ||
if violations != nil { | ||
return nil, invalidArgumentError(violations) | ||
} | ||
|
||
owner := "" | ||
if authPayload.Role == util.UserRole { | ||
owner = authPayload.Username | ||
} else if authPayload.Role == util.AdminRole { | ||
owner = req.GetUsername() | ||
} | ||
|
||
arg := db.CreateAccountParams{ | ||
Owner: owner, | ||
Balance: 0, | ||
Unit: util.KWH, | ||
} | ||
|
||
acc, err := server.store.CreateAccount(ctx, arg) | ||
if err != nil { | ||
if errors.Is(err, db.ErrRecordNotFound) { | ||
return nil, status.Errorf(codes.NotFound, "user not found") | ||
} | ||
return nil, status.Errorf(codes.Internal, "failed to create account: %s", err) | ||
} | ||
|
||
rsp := &pb.CreateAccountResponse{ | ||
Account: convertAccount(acc), | ||
} | ||
return rsp, nil | ||
} | ||
|
||
func validateCreateAccountRequest(req *pb.CreateAccountRequest) (violations []*errdetails.BadRequest_FieldViolation) { | ||
if req.GetUsername() != "" { | ||
if err := val.ValidateUsername(req.GetUsername()); err != nil { | ||
violations = append(violations, fieldViolation("username", err)) | ||
} | ||
} | ||
return | ||
} |
Oops, something went wrong.