chore: add user.open_id field

This commit is contained in:
Steven
2022-09-12 21:57:36 +08:00
parent 0ba7a9e519
commit 20651d21e1
7 changed files with 94 additions and 75 deletions

View File

@ -55,7 +55,8 @@ CREATE TABLE user (
row_status TEXT NOT NULL CHECK (row_status IN ('NORMAL', 'ARCHIVED')) DEFAULT 'NORMAL',
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
password_hash TEXT NOT NULL
password_hash TEXT NOT NULL,
open_id TEXT NOT NULL UNIQUE
);
INSERT INTO
@ -75,6 +76,12 @@ WHERE
rowid = old.rowid;
END;
CREATE INDEX user_id_index ON user(id);
CREATE UNIQUE INDEX user_email_index ON user(email);
CREATE UNIQUE INDEX user_open_id_index ON user(open_id);
-- user_setting
CREATE TABLE user_setting (
user_id INTEGER NOT NULL,

View File

@ -3,7 +3,8 @@ INSERT INTO
`id`,
`email`,
`name`,
`password_hash`
`password_hash`,
`open_id`
)
VALUES
(
@ -11,39 +12,6 @@ VALUES
'demo@iamcorgi.com',
'Demo Host',
-- raw password: secret
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK',
'demo_open_id'
);
INSERT INTO
user (
`id`,
`email`,
`name`,
`password_hash`
)
VALUES
(
102,
'jack@iamcorgi.com',
'Jack',
-- raw password: secret
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
);
INSERT INTO
user (
`id`,
`row_status`,
`email`,
`name`,
`password_hash`
)
VALUES
(
103,
'ARCHIVED',
'bob@iamcorgi.com',
'Bob',
-- raw password: secret
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
);

View File

@ -24,6 +24,7 @@ type userRaw struct {
Email string
Name string
PasswordHash string
OpenID string
}
func (raw *userRaw) toUser() *api.User {
@ -37,6 +38,7 @@ func (raw *userRaw) toUser() *api.User {
Email: raw.Email,
Name: raw.Name,
PasswordHash: raw.PasswordHash,
OpenID: raw.OpenID,
}
}
@ -174,24 +176,27 @@ func createUser(ctx context.Context, tx *sql.Tx, create *api.UserCreate) (*userR
INSERT INTO user (
email,
name,
password_hash
password_hash,
open_id
)
VALUES (?, ?, ?)
RETURNING id, email, name, password_hash, created_ts, updated_ts, row_status
VALUES (?, ?, ?, ?)
RETURNING id, created_ts, updated_ts, row_status, email, name, password_hash, open_id
`
var userRaw userRaw
if err := tx.QueryRowContext(ctx, query,
create.Email,
create.Name,
create.PasswordHash,
create.OpenID,
).Scan(
&userRaw.ID,
&userRaw.Email,
&userRaw.Name,
&userRaw.PasswordHash,
&userRaw.CreatedTs,
&userRaw.UpdatedTs,
&userRaw.RowStatus,
&userRaw.Email,
&userRaw.Name,
&userRaw.PasswordHash,
&userRaw.OpenID,
); err != nil {
return nil, FormatError(err)
}
@ -214,6 +219,9 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
if v := patch.PasswordHash; v != nil {
set, args = append(set, "password_hash = ?"), append(args, *v)
}
if v := patch.OpenID; v != nil {
set, args = append(set, "open_id = ?"), append(args, *v)
}
args = append(args, patch.ID)
@ -221,7 +229,7 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
UPDATE user
SET ` + strings.Join(set, ", ") + `
WHERE id = ?
RETURNING id, created_ts, updated_ts, row_status, email, name, password_hash
RETURNING id, created_ts, updated_ts, row_status, email, name, password_hash, open_id
`
row, err := tx.QueryContext(ctx, query, args...)
if err != nil {
@ -239,6 +247,7 @@ func patchUser(ctx context.Context, tx *sql.Tx, patch *api.UserPatch) (*userRaw,
&userRaw.Email,
&userRaw.Name,
&userRaw.PasswordHash,
&userRaw.OpenID,
); err != nil {
return nil, FormatError(err)
}
@ -265,6 +274,9 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
if v := find.Name; v != nil {
where, args = append(where, "name = ?"), append(args, *v)
}
if v := find.OpenID; v != nil {
where, args = append(where, "open_id = ?"), append(args, *v)
}
query := `
SELECT
@ -274,7 +286,8 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
row_status,
email,
name,
password_hash
password_hash,
open_id
FROM user
WHERE ` + strings.Join(where, " AND ") + `
ORDER BY updated_ts DESC, created_ts DESC, row_status DESC
@ -296,6 +309,7 @@ func findUserList(ctx context.Context, tx *sql.Tx, find *api.UserFind) ([]*userR
&userRaw.Email,
&userRaw.Name,
&userRaw.PasswordHash,
&userRaw.OpenID,
); err != nil {
return nil, FormatError(err)
}