fix: create memo without some attributes (#2355)

This commit is contained in:
Athurg Gooth 2023-10-08 18:29:12 +08:00 committed by GitHub
parent 1fbd568dfe
commit eaac17a236
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 29 deletions

View file

@ -13,14 +13,36 @@ import (
) )
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) { func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
stmt := "INSERT INTO `memo` (`creator_id`, `content`, `visibility`) VALUES (?, ?, ?)" fields := []string{"`creator_id`", "`content`", "`visibility`"}
result, err := d.db.ExecContext( placeholder := []string{"?", "?", "?"}
ctx, args := []any{create.CreatorID, create.Content, create.Visibility}
stmt,
create.CreatorID, if create.ID != 0 {
create.Content, fields = append(fields, "`id`")
create.Visibility, placeholder = append(placeholder, "?")
) args = append(args, create.ID)
}
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.RowStatus != "" {
fields = append(fields, "`row_status`")
placeholder = append(placeholder, "?")
args = append(args, create.RowStatus)
}
stmt := "INSERT INTO memo (" + 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
} }

View file

@ -5,7 +5,6 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -14,28 +13,36 @@ import (
) )
func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) { func (d *DB) CreateMemo(ctx context.Context, create *store.Memo) (*store.Memo, error) {
if create.CreatedTs == 0 { fields := []string{"`creator_id`", "`content`", "`visibility`"}
create.CreatedTs = time.Now().Unix() placeholder := []string{"?", "?", "?"}
args := []any{create.CreatorID, create.Content, create.Visibility}
if create.ID != 0 {
fields = append(fields, "`id`")
placeholder = append(placeholder, "?")
args = append(args, create.ID)
} }
stmt := ` if create.CreatedTs != 0 {
INSERT INTO memo ( fields = append(fields, "`created_ts`")
creator_id, placeholder = append(placeholder, "?")
created_ts, args = append(args, create.CreatedTs)
content, }
visibility
) if create.UpdatedTs != 0 {
VALUES (?, ?, ?, ?) fields = append(fields, "`updated_ts`")
RETURNING id, created_ts, updated_ts, row_status placeholder = append(placeholder, "?")
` args = append(args, create.UpdatedTs)
if err := d.db.QueryRowContext( }
ctx,
stmt, if create.RowStatus != "" {
create.CreatorID, fields = append(fields, "`row_status`")
create.CreatedTs, placeholder = append(placeholder, "?")
create.Content, args = append(args, create.RowStatus)
create.Visibility, }
).Scan(
stmt := "INSERT INTO memo (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`, `updated_ts`, `row_status`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID, &create.ID,
&create.CreatedTs, &create.CreatedTs,
&create.UpdatedTs, &create.UpdatedTs,