-
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
Showing
21 changed files
with
696 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"plpgsqlLanguageServer.database": "eenergy", | ||
"plpgsqlLanguageServer.user": "root", | ||
"plpgsqlLanguageServer.password": "secret", | ||
"plpgsqlLanguageServer.definitionFiles": [ | ||
// Support glob. | ||
"**/*.sql", | ||
"**/*.psql", | ||
"**/*.pgsql" | ||
], | ||
// The supported extention types are ['*.pgsql', '*.psql']. | ||
// If you want to use this extension in '*.sql', add the following settings. | ||
"files.associations": { | ||
"*.sql": "postgres" | ||
} | ||
} | ||
|
||
|
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,34 @@ | ||
DROP FUNCTION IF EXISTS update_verify_email; | ||
CREATE OR REPLACE FUNCTION update_verify_email( | ||
p_id bigint, | ||
p_secret_code VARCHAR | ||
) | ||
RETURNS TABLE ( | ||
id bigint, | ||
username VARCHAR, | ||
email VARCHAR, | ||
secret_code VARCHAR, | ||
is_used BOOLEAN, | ||
created_at TIMESTAMP WITH TIME ZONE, | ||
expires_at_at TIMESTAMP WITH TIME ZONE | ||
) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
UPDATE verify_emails | ||
SET is_used = TRUE | ||
WHERE | ||
verify_emails.id = p_id | ||
AND verify_emails.secret_code = p_secret_code | ||
AND verify_emails.is_used = FALSE | ||
AND verify_emails.expired_at > now() | ||
RETURNING | ||
verify_emails.id, | ||
verify_emails.username, | ||
verify_emails.email, | ||
verify_emails.secret_code, | ||
verify_emails.is_used, | ||
verify_emails.created_at, | ||
verify_emails.expired_at; | ||
|
||
END; | ||
$$ LANGUAGE plpgsql; |
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,43 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
type VerifyEmailTxParams struct { | ||
EmailId int64 | ||
SecretCode string | ||
} | ||
|
||
type VerifyEmailTxResult struct { | ||
User User | ||
VerifyEmail VerifyEmail | ||
} | ||
|
||
func (store *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error) { | ||
var result VerifyEmailTxResult | ||
|
||
err := store.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
result.VerifyEmail, err = q.UpdatVerifyEmail(ctx, UpdatVerifyEmailParams{ | ||
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 | ||
} |
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
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,41 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
|
||
db "github.com/aradwann/eenergy/db/store" | ||
"github.com/aradwann/eenergy/pb" | ||
"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) VerifyEmail(ctx context.Context, req *pb.VerifyEmailRequest) (*pb.VerifyEmailResponse, error) { | ||
violations := validateVerifyEmailRequest(req) | ||
if violations != nil { | ||
return nil, invalidArgumentError(violations) | ||
} | ||
txResult, err := server.store.VerifyEmailTx(ctx, db.VerifyEmailTxParams{ | ||
EmailId: req.GetEmailId(), | ||
SecretCode: req.GetSecretCode(), | ||
}) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to verify email") | ||
} | ||
rsp := &pb.VerifyEmailResponse{ | ||
IsVerified: txResult.User.IsEmailVerified, | ||
} | ||
return rsp, nil | ||
} | ||
|
||
func validateVerifyEmailRequest(req *pb.VerifyEmailRequest) (violations []*errdetails.BadRequest_FieldViolation) { | ||
if err := val.ValidateEmailId(req.GetEmailId()); err != nil { | ||
violations = append(violations, fieldViolation("email_id", err)) | ||
} | ||
if err := val.ValidateSecretCode(req.GetSecretCode()); err != nil { | ||
violations = append(violations, fieldViolation("secret_code", err)) | ||
} | ||
|
||
return | ||
} |
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.