mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: fix postgres stmts
This commit is contained in:
@ -2,8 +2,8 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
|
||||
@ -21,50 +21,29 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
|
||||
payloadString = string(bytes)
|
||||
}
|
||||
|
||||
qb := squirrel.Insert("activity").
|
||||
Columns("creator_id", "type", "level", "payload").
|
||||
PlaceholderFormat(squirrel.Dollar)
|
||||
|
||||
values := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString}
|
||||
qb = qb.Values(values...).Suffix("RETURNING id")
|
||||
|
||||
stmt, args, err := qb.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to construct query")
|
||||
}
|
||||
|
||||
var id int32
|
||||
err = d.db.QueryRowContext(ctx, stmt, args...).Scan(&id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to execute statement and retrieve ID")
|
||||
}
|
||||
|
||||
list, err := d.ListActivities(ctx, &store.FindActivity{ID: &id})
|
||||
if err != nil || len(list) == 0 {
|
||||
return nil, errors.Wrap(err, "failed to find activity")
|
||||
}
|
||||
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
|
||||
qb := squirrel.Select("id", "created_ts", "creator_id", "type", "level", "payload").
|
||||
From("activity").
|
||||
Where("1 = 1").
|
||||
PlaceholderFormat(squirrel.Dollar)
|
||||
|
||||
if find.ID != nil {
|
||||
qb = qb.Where(squirrel.Eq{"id": *find.ID})
|
||||
}
|
||||
if find.Type != nil {
|
||||
qb = qb.Where(squirrel.Eq{"type": find.Type.String()})
|
||||
}
|
||||
|
||||
query, args, err := qb.ToSql()
|
||||
if err != nil {
|
||||
fields := []string{"creator_id", "type", "level", "payload"}
|
||||
args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString}
|
||||
stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.ID,
|
||||
&create.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
|
||||
}
|
||||
if find.Type != nil {
|
||||
where, args = append(where, "type = "+placeholder(len(args)+1)), append(args, find.Type.String())
|
||||
}
|
||||
|
||||
query := "SELECT id, creator_id, type, level, payload, created_ts FROM activity WHERE " + strings.Join(where, " AND ") + " ORDER BY created_ts DESC"
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -77,17 +56,17 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
|
||||
var payloadBytes []byte
|
||||
if err := rows.Scan(
|
||||
&activity.ID,
|
||||
&activity.CreatedTs,
|
||||
&activity.CreatorID,
|
||||
&activity.Type,
|
||||
&activity.Level,
|
||||
&payloadBytes,
|
||||
&activity.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := &storepb.ActivityPayload{}
|
||||
if err := protojson.Unmarshal(payloadBytes, payload); err != nil {
|
||||
if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
activity.Payload = payload
|
||||
|
Reference in New Issue
Block a user