chore: tweak store definition

This commit is contained in:
Steven
2024-04-13 11:54:37 +08:00
parent 6ee3b0f704
commit cebc46adc7
25 changed files with 298 additions and 895 deletions

View File

@ -4,17 +4,16 @@ import (
"context"
"strings"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*storepb.Webhook, error) {
func (d *DB) CreateWebhook(ctx context.Context, create *store.Webhook) (*store.Webhook, error) {
fields := []string{"name", "url", "creator_id"}
args := []any{create.Name, create.Url, create.CreatorId}
args := []any{create.Name, create.Url, create.CreatorID}
stmt := "INSERT INTO webhook (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts, updated_ts, row_status"
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&create.Id,
&create.ID,
&create.CreatedTs,
&create.UpdatedTs,
&rowStatus,
@ -22,12 +21,12 @@ func (d *DB) CreateWebhook(ctx context.Context, create *storepb.Webhook) (*store
return nil, err
}
create.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
create.RowStatus = store.RowStatus(rowStatus)
webhook := create
return webhook, nil
}
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*storepb.Webhook, error) {
func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*store.Webhook, error) {
where, args := []string{"1 = 1"}, []any{}
if find.ID != nil {
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
@ -55,22 +54,22 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
}
defer rows.Close()
list := []*storepb.Webhook{}
list := []*store.Webhook{}
for rows.Next() {
webhook := &storepb.Webhook{}
webhook := &store.Webhook{}
var rowStatus string
if err := rows.Scan(
&webhook.Id,
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorId,
&webhook.CreatorID,
&webhook.Name,
&webhook.Url,
); err != nil {
return nil, err
}
webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
webhook.RowStatus = store.RowStatus(rowStatus)
list = append(list, webhook)
}
@ -81,7 +80,7 @@ func (d *DB) ListWebhooks(ctx context.Context, find *store.FindWebhook) ([]*stor
return list, nil
}
func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*storepb.Webhook, error) {
func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*store.Webhook, error) {
set, args := []string{}, []any{}
if update.RowStatus != nil {
set, args = append(set, "row_status = "+placeholder(len(args)+1)), append(args, update.RowStatus.String())
@ -95,20 +94,20 @@ func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*s
stmt := "UPDATE webhook SET " + strings.Join(set, ", ") + " WHERE id = " + placeholder(len(args)+1) + " RETURNING id, created_ts, updated_ts, row_status, creator_id, name, url"
args = append(args, update.ID)
webhook := &storepb.Webhook{}
webhook := &store.Webhook{}
var rowStatus string
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&webhook.Id,
&webhook.ID,
&webhook.CreatedTs,
&webhook.UpdatedTs,
&rowStatus,
&webhook.CreatorId,
&webhook.CreatorID,
&webhook.Name,
&webhook.Url,
); err != nil {
return nil, err
}
webhook.RowStatus = storepb.RowStatus(storepb.RowStatus_value[rowStatus])
webhook.RowStatus = store.RowStatus(rowStatus)
return webhook, nil
}