-
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.
- Loading branch information
1 parent
a9ad1b2
commit c5b329c
Showing
20 changed files
with
693 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,48 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
// VerifyEmailTxParams contains the input parameters of the verify email transaction | ||
type VerifyEmailTxParams struct { | ||
EmailId int64 | ||
SecretCode string | ||
} | ||
|
||
// VerifyEmailTxResult is the result of the verify email transaction | ||
type VerifyEmailTxResult struct { | ||
User User | ||
VerifyEmail VerifyEmail | ||
} | ||
|
||
// VerifyEmailTx performs a transaction to verify an email | ||
func (s *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error) { | ||
var result VerifyEmailTxResult | ||
|
||
err := s.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
|
||
result.VerifyEmail, err = q.UpdateVerifyEmail(ctx, UpdateVerifyEmailParams{ | ||
ID: arg.EmailId, | ||
SecretCode: arg.SecretCode, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
result.User, err = q.UpdateUser(ctx, UpdateUserParams{ | ||
Username: result.VerifyEmail.Username, | ||
IsEmailVerified: sql.NullBool{ | ||
Bool: true, | ||
Valid: true, | ||
}, | ||
}) | ||
|
||
return err | ||
}) | ||
|
||
return result, err | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,47 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
|
||
db "github.com/blessedmadukoma/go-simple-bank/db/sqlc" | ||
"github.com/blessedmadukoma/go-simple-bank/gvalidator" | ||
"github.com/blessedmadukoma/go-simple-bank/pb" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (srv *Server) VerifyEmail(ctx context.Context, req *pb.VerifyEmailRequest) (*pb.VerifyEmailResponse, error) { | ||
violations := validateVerifyEmailRequest(req) | ||
if violations != nil { | ||
return nil, invalidArgumentError(violations) | ||
} | ||
|
||
txResult, err := srv.store.VerifyEmailTx(ctx, db.VerifyEmailTxParams{ | ||
EmailId: req.GetEmailId(), | ||
SecretCode: req.GetSecretCode(), | ||
}) | ||
|
||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to veriy email: %v", err) | ||
} | ||
|
||
rsp := &pb.VerifyEmailResponse{ | ||
IsVerified: txResult.User.IsEmailVerified, | ||
} | ||
|
||
return rsp, nil | ||
} | ||
|
||
// validateVerifyEmailRequest validates the request body of VerifyEmailRequest. | ||
func validateVerifyEmailRequest(req *pb.VerifyEmailRequest) (violations []*errdetails.BadRequest_FieldViolation) { | ||
if err := gvalidator.ValidateEmailID(req.GetEmailId()); err != nil { | ||
violations = append(violations, fieldViolation("email_id", err)) | ||
} | ||
|
||
if err := gvalidator.ValidateSecretCode(req.GetSecretCode()); err != nil { | ||
violations = append(violations, fieldViolation("secret_code", err)) | ||
} | ||
|
||
return violations | ||
} |
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
Oops, something went wrong.