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,18 +4,17 @@ 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`"}
placeholder := []string{"?", "?", "?"}
args := []any{create.Name, create.Url, create.CreatorId}
args := []any{create.Name, create.Url, create.CreatorID}
stmt := "INSERT INTO `webhook` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") 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,
@ -23,12 +22,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` = ?"), append(args, *find.ID)
@ -56,22 +55,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)
}
@ -82,7 +81,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 = ?"), append(args, update.RowStatus.String())
@ -96,20 +95,20 @@ func (d *DB) UpdateWebhook(ctx context.Context, update *store.UpdateWebhook) (*s
args = append(args, update.ID)
stmt := "UPDATE `webhook` SET " + strings.Join(set, ", ") + " WHERE `id` = ? RETURNING `id`, `created_ts`, `updated_ts`, `row_status`, `creator_id`, `name`, `url`"
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
}