mirror of
https://github.com/usememos/memos.git
synced 2025-01-05 04:58:02 +03:00
fix: create user without some attributes (#2357)
This commit is contained in:
parent
55e0fbf24e
commit
7680be1a2f
@ -10,15 +10,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||||
stmt := "INSERT INTO `user` (`username`, `role`, `email`, `nickname`, `password_hash`, `avatar_url`) VALUES (?, ?, ?, ?, ?, ?)"
|
fields := []string{"`username`", "`role`", "`email`", "`nickname`", "`password_hash`", "`avatar_url`"}
|
||||||
result, err := d.db.ExecContext(ctx, stmt,
|
placeholder := []string{"?", "?", "?", "?", "?", "?"}
|
||||||
create.Username,
|
args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash, create.AvatarURL}
|
||||||
create.Role,
|
|
||||||
create.Email,
|
if create.RowStatus != "" {
|
||||||
create.Nickname,
|
fields = append(fields, "`row_status`")
|
||||||
create.PasswordHash,
|
placeholder = append(placeholder, "?")
|
||||||
create.AvatarURL,
|
args = append(args, create.RowStatus)
|
||||||
)
|
}
|
||||||
|
|
||||||
|
if create.CreatedTs != 0 {
|
||||||
|
fields = append(fields, "`created_ts`")
|
||||||
|
placeholder = append(placeholder, "FROM_UNIXTIME(?)")
|
||||||
|
args = append(args, create.CreatedTs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if create.UpdatedTs != 0 {
|
||||||
|
fields = append(fields, "`updated_ts`")
|
||||||
|
placeholder = append(placeholder, "FROM_UNIXTIME(?)")
|
||||||
|
args = append(args, create.UpdatedTs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if create.ID != 0 {
|
||||||
|
fields = append(fields, "`id`")
|
||||||
|
placeholder = append(placeholder, "?")
|
||||||
|
args = append(args, create.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := "INSERT INTO user (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
|
||||||
|
result, err := d.db.ExecContext(ctx, stmt, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -8,26 +8,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, error) {
|
||||||
stmt := `
|
fields := []string{"`username`", "`role`", "`email`", "`nickname`", "`password_hash`"}
|
||||||
INSERT INTO user (
|
placeholder := []string{"?", "?", "?", "?", "?"}
|
||||||
username,
|
args := []any{create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash}
|
||||||
role,
|
|
||||||
email,
|
if create.AvatarURL != "" {
|
||||||
nickname,
|
fields = append(fields, "`avatar_url`")
|
||||||
password_hash
|
placeholder = append(placeholder, "?")
|
||||||
)
|
args = append(args, create.AvatarURL)
|
||||||
VALUES (?, ?, ?, ?, ?)
|
}
|
||||||
RETURNING id, avatar_url, created_ts, updated_ts, row_status
|
|
||||||
`
|
if create.RowStatus != "" {
|
||||||
if err := d.db.QueryRowContext(
|
fields = append(fields, "`row_status`")
|
||||||
ctx,
|
placeholder = append(placeholder, "?")
|
||||||
stmt,
|
args = append(args, create.RowStatus)
|
||||||
create.Username,
|
}
|
||||||
create.Role,
|
|
||||||
create.Email,
|
if create.CreatedTs != 0 {
|
||||||
create.Nickname,
|
fields = append(fields, "`created_ts`")
|
||||||
create.PasswordHash,
|
placeholder = append(placeholder, "?")
|
||||||
).Scan(
|
args = append(args, create.CreatedTs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if create.UpdatedTs != 0 {
|
||||||
|
fields = append(fields, "`updated_ts`")
|
||||||
|
placeholder = append(placeholder, "?")
|
||||||
|
args = append(args, create.UpdatedTs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if create.ID != 0 {
|
||||||
|
fields = append(fields, "`id`")
|
||||||
|
placeholder = append(placeholder, "?")
|
||||||
|
args = append(args, create.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := "INSERT INTO user (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING id, avatar_url, created_ts, updated_ts, row_status"
|
||||||
|
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||||
&create.ID,
|
&create.ID,
|
||||||
&create.AvatarURL,
|
&create.AvatarURL,
|
||||||
&create.CreatedTs,
|
&create.CreatedTs,
|
||||||
|
Loading…
Reference in New Issue
Block a user