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

@ -8,12 +8,12 @@ import (
"github.com/usememos/memos/store"
)
func (d *DB) UpsertReaction(ctx context.Context, upsert *storepb.Reaction) (*storepb.Reaction, error) {
func (d *DB) UpsertReaction(ctx context.Context, upsert *store.Reaction) (*store.Reaction, error) {
fields := []string{"creator_id", "content_id", "reaction_type"}
args := []interface{}{upsert.CreatorId, upsert.ContentId, upsert.ReactionType.String()}
args := []interface{}{upsert.CreatorID, upsert.ContentID, upsert.ReactionType.String()}
stmt := "INSERT INTO reaction (" + strings.Join(fields, ", ") + ") VALUES (" + placeholders(len(args)) + ") RETURNING id, created_ts"
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
&upsert.Id,
&upsert.ID,
&upsert.CreatedTs,
); err != nil {
return nil, err
@ -23,7 +23,7 @@ func (d *DB) UpsertReaction(ctx context.Context, upsert *storepb.Reaction) (*sto
return reaction, nil
}
func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*storepb.Reaction, error) {
func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*store.Reaction, error) {
where, args := []string{"1 = 1"}, []interface{}{}
if find.ID != nil {
where, args = append(where, "id = "+placeholder(len(args)+1)), append(args, *find.ID)
@ -52,20 +52,20 @@ func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*st
}
defer rows.Close()
list := []*storepb.Reaction{}
list := []*store.Reaction{}
for rows.Next() {
reaction := &storepb.Reaction{}
reaction := &store.Reaction{}
var reactionType string
if err := rows.Scan(
&reaction.Id,
&reaction.ID,
&reaction.CreatedTs,
&reaction.CreatorId,
&reaction.ContentId,
&reaction.CreatorID,
&reaction.ContentID,
&reactionType,
); err != nil {
return nil, err
}
reaction.ReactionType = storepb.Reaction_Type(storepb.Reaction_Type_value[reactionType])
reaction.ReactionType = storepb.ReactionType(storepb.ReactionType_value[reactionType])
list = append(list, reaction)
}