-
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
15 changed files
with
217 additions
and
15 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,10 @@ | ||
-- Drop the foreign key constraint on 'verify_emails' | ||
ALTER TABLE verify_emails | ||
DROP CONSTRAINT IF EXISTS fk_verify_emails_users; | ||
|
||
-- Drop the 'verify_emails' table | ||
DROP TABLE IF EXISTS verify_emails; | ||
|
||
-- Remove the 'is_email_verified' column from 'users' | ||
ALTER TABLE users | ||
DROP COLUMN IF EXISTS is_email_verified; |
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,15 @@ | ||
ALTER TABLE users ADD COLUMN is_email_verified BOOLEAN DEFAULT false; | ||
|
||
CREATE TABLE "verify_emails" | ||
( | ||
"id" bigserial PRIMARY KEY, | ||
"username" varchar NOT NULL, | ||
"email" varchar NOT NULL, | ||
"secret_code" varchar NOT NULL, | ||
"is_used" boolean NOT NULL DEFAULT false, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()), | ||
"expired_at" timestamptz NOT NULL DEFAULT (now() + INTERVAL '15 minutes') | ||
); | ||
|
||
ALTER TABLE "verify_emails" ADD FOREIGN KEY ("username") REFERENCES "users" ("username"); | ||
|
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,30 @@ | ||
DROP FUNCTION IF EXISTS create_verify_email; | ||
CREATE OR REPLACE FUNCTION create_verify_email( | ||
p_username VARCHAR, | ||
p_email VARCHAR, | ||
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 | ||
INSERT INTO verify_emails (username, email, secret_code) | ||
VALUES (p_username, p_email, p_secret_code) | ||
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; |
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
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,93 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"log" | ||
) | ||
|
||
type CreateVerifyEmail struct { | ||
Username string `json:"username"` | ||
Email string `json:"email"` | ||
SecretCode string `json:"secret_code"` | ||
} | ||
|
||
func (q *Queries) CreateVerifyEmail(ctx context.Context, arg CreateVerifyEmail) (VerifyEmail, error) { | ||
var verifyEmail VerifyEmail | ||
row := q.callStoredFunction(ctx, "create_verify_email", | ||
arg.Username, | ||
arg.Email, | ||
arg.SecretCode, | ||
) | ||
|
||
err := scanVerifyEmailFromRow(row, &verifyEmail) | ||
if err != nil { | ||
return verifyEmail, err | ||
} | ||
return verifyEmail, nil | ||
} | ||
|
||
// func (q *Queries) GetUser(ctx context.Context, username string) (User, error) { | ||
// var user User | ||
// row := q.callStoredFunction(ctx, "get_user", username) | ||
// err := scanUserFromRow(row, &user) | ||
// if err != nil { | ||
// return user, err | ||
// } | ||
// return user, nil | ||
// } | ||
|
||
// type UpdateUserParams struct { | ||
// HashedPassword sql.NullString `json:"hashed_password"` | ||
// PasswordChangedAt sql.NullTime `json:"password_changed_at"` | ||
// FullName sql.NullString `json:"fullname"` | ||
// Email sql.NullString `json:"email"` | ||
// Username string `json:"username"` | ||
// } | ||
|
||
// func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) { | ||
// var user User | ||
// params := []interface{}{ | ||
// arg.Username, | ||
// arg.HashedPassword, | ||
// arg.PasswordChangedAt, | ||
// arg.FullName, | ||
// arg.Email, | ||
// } | ||
// row := q.callStoredFunction(ctx, "update_user", params...) | ||
|
||
// // Execute the stored procedure and scan the results into the variables | ||
// err := scanUserFromRow(row, &user) | ||
// if err != nil { | ||
// return User{}, err | ||
// } | ||
|
||
// return user, nil | ||
// } | ||
|
||
func scanVerifyEmailFromRow(row *sql.Row, verifyEmail *VerifyEmail) error { | ||
err := row.Scan( | ||
&verifyEmail.ID, | ||
&verifyEmail.Username, | ||
&verifyEmail.Email, | ||
&verifyEmail.SecretCode, | ||
&verifyEmail.IsUsed, | ||
&verifyEmail.CreatedAt, | ||
&verifyEmail.ExpiredAt, | ||
) | ||
|
||
// Check for errors after scanning | ||
if err != nil { | ||
// Handle scan-related errors | ||
if err == sql.ErrNoRows { | ||
// fmt.Println("No rows were returned.") | ||
return err | ||
} else { | ||
// Log and return other scan-related errors | ||
log.Printf("Error scanning row: %s", err) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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