fix: create activity without some attributes (#2356)

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

View file

@ -2,6 +2,7 @@ package mysql
import ( import (
"context" "context"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -9,13 +10,24 @@ import (
) )
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) { func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
stmt := "INSERT INTO activity (`creator_id`, `type`, `level`, `payload`) VALUES (?, ?, ?, ?)" fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
result, err := d.db.ExecContext(ctx, stmt, placeholder := []string{"?", "?", "?", "?"}
create.CreatorID, args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
create.Type,
create.Level, if create.ID != 0 {
create.Payload, fields = append(fields, "`id`")
) 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)
}
stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
result, err := d.db.ExecContext(ctx, stmt, args...)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to execute statement") return nil, errors.Wrap(err, "failed to execute statement")
} }

View file

@ -2,22 +2,30 @@ package sqlite
import ( import (
"context" "context"
"strings"
"github.com/usememos/memos/store" "github.com/usememos/memos/store"
) )
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) { func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
stmt := ` fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
INSERT INTO activity ( placeholder := []string{"?", "?", "?", "?"}
creator_id, args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
type,
level, if create.ID != 0 {
payload fields = append(fields, "`id`")
) placeholder = append(placeholder, "?")
VALUES (?, ?, ?, ?) args = append(args, create.ID)
RETURNING id, created_ts }
`
if err := d.db.QueryRowContext(ctx, stmt, create.CreatorID, create.Type, create.Level, create.Payload).Scan( if create.CreatedTs != 0 {
fields = append(fields, "`created_ts`")
placeholder = append(placeholder, "?")
args = append(args, create.CreatedTs)
}
stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.ID, &create.ID,
&create.CreatedTs, &create.CreatedTs,
); err != nil { ); err != nil {