mirror of
https://github.com/aykhans/slash-e.git
synced 2025-07-04 20:28:00 +00:00
refactor: update visibilities
This commit is contained in:
@ -92,7 +92,7 @@ func (d *DB) UpdateCollection(ctx context.Context, update *store.UpdateCollectio
|
||||
collection.ShortcutIds = append(collection.ShortcutIds, shortcutID)
|
||||
}
|
||||
}
|
||||
collection.Visibility = convertVisibilityStringToStorepb(visibility)
|
||||
collection.Visibility = store.ConvertVisibilityStringToStorepb(visibility)
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ func (d *DB) ListCollections(ctx context.Context, find *store.FindCollection) ([
|
||||
collection.ShortcutIds = append(collection.ShortcutIds, shortcutID)
|
||||
}
|
||||
}
|
||||
collection.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
||||
collection.Visibility = store.ConvertVisibilityStringToStorepb(visibility)
|
||||
list = append(list, collection)
|
||||
}
|
||||
|
||||
@ -189,6 +189,5 @@ func vacuumCollection(ctx context.Context, tx *sql.Tx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -32,27 +32,21 @@ func (d *DB) CreateShortcut(ctx context.Context, create *storepb.Shortcut) (*sto
|
||||
` + strings.Join(set, ", ") + `
|
||||
)
|
||||
VALUES (` + strings.Join(placeholder, ",") + `)
|
||||
RETURNING id, created_ts, updated_ts, row_status
|
||||
RETURNING id, created_ts, updated_ts
|
||||
`
|
||||
var rowStatus string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&create.Id,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
&rowStatus,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
create.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
shortcut := create
|
||||
return shortcut, nil
|
||||
}
|
||||
|
||||
func (d *DB) UpdateShortcut(ctx context.Context, update *store.UpdateShortcut) (*storepb.Shortcut, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if update.RowStatus != nil {
|
||||
set, args = append(set, "row_status = ?"), append(args, update.RowStatus.String())
|
||||
}
|
||||
if update.Name != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *update.Name)
|
||||
}
|
||||
@ -89,16 +83,15 @@ func (d *DB) UpdateShortcut(ctx context.Context, update *store.UpdateShortcut) (
|
||||
` + strings.Join(set, ", ") + `
|
||||
WHERE
|
||||
id = ?
|
||||
RETURNING id, creator_id, created_ts, updated_ts, row_status, name, link, title, description, visibility, tag, og_metadata
|
||||
RETURNING id, creator_id, created_ts, updated_ts, name, link, title, description, visibility, tag, og_metadata
|
||||
`
|
||||
shortcut := &storepb.Shortcut{}
|
||||
var rowStatus, visibility, tags, openGraphMetadataString string
|
||||
var visibility, tags, openGraphMetadataString string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&shortcut.Id,
|
||||
&shortcut.CreatorId,
|
||||
&shortcut.CreatedTs,
|
||||
&shortcut.UpdatedTs,
|
||||
&rowStatus,
|
||||
&shortcut.Name,
|
||||
&shortcut.Link,
|
||||
&shortcut.Title,
|
||||
@ -109,8 +102,7 @@ func (d *DB) UpdateShortcut(ctx context.Context, update *store.UpdateShortcut) (
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shortcut.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
shortcut.Visibility = convertVisibilityStringToStorepb(visibility)
|
||||
shortcut.Visibility = store.ConvertVisibilityStringToStorepb(visibility)
|
||||
shortcut.Tags = filterTags(strings.Split(tags, " "))
|
||||
var ogMetadata storepb.OpenGraphMetadata
|
||||
if err := protojson.Unmarshal([]byte(openGraphMetadataString), &ogMetadata); err != nil {
|
||||
@ -128,9 +120,6 @@ func (d *DB) ListShortcuts(ctx context.Context, find *store.FindShortcut) ([]*st
|
||||
if v := find.CreatorID; v != nil {
|
||||
where, args = append(where, "creator_id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.RowStatus; v != nil {
|
||||
where, args = append(where, "row_status = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.Name; v != nil {
|
||||
where, args = append(where, "name = ?"), append(args, *v)
|
||||
}
|
||||
@ -138,7 +127,7 @@ func (d *DB) ListShortcuts(ctx context.Context, find *store.FindShortcut) ([]*st
|
||||
list := []string{}
|
||||
for _, visibility := range v {
|
||||
list = append(list, fmt.Sprintf("$%d", len(args)+1))
|
||||
args = append(args, visibility)
|
||||
args = append(args, visibility.String())
|
||||
}
|
||||
where = append(where, fmt.Sprintf("visibility in (%s)", strings.Join(list, ",")))
|
||||
}
|
||||
@ -152,7 +141,6 @@ func (d *DB) ListShortcuts(ctx context.Context, find *store.FindShortcut) ([]*st
|
||||
creator_id,
|
||||
created_ts,
|
||||
updated_ts,
|
||||
row_status,
|
||||
name,
|
||||
link,
|
||||
title,
|
||||
@ -173,13 +161,12 @@ func (d *DB) ListShortcuts(ctx context.Context, find *store.FindShortcut) ([]*st
|
||||
list := make([]*storepb.Shortcut, 0)
|
||||
for rows.Next() {
|
||||
shortcut := &storepb.Shortcut{}
|
||||
var rowStatus, visibility, tags, openGraphMetadataString string
|
||||
var visibility, tags, openGraphMetadataString string
|
||||
if err := rows.Scan(
|
||||
&shortcut.Id,
|
||||
&shortcut.CreatorId,
|
||||
&shortcut.CreatedTs,
|
||||
&shortcut.UpdatedTs,
|
||||
&rowStatus,
|
||||
&shortcut.Name,
|
||||
&shortcut.Link,
|
||||
&shortcut.Title,
|
||||
@ -190,8 +177,7 @@ func (d *DB) ListShortcuts(ctx context.Context, find *store.FindShortcut) ([]*st
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shortcut.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
shortcut.Visibility = storepb.Visibility(storepb.Visibility_value[visibility])
|
||||
shortcut.Visibility = store.ConvertVisibilityStringToStorepb(visibility)
|
||||
shortcut.Tags = filterTags(strings.Split(tags, " "))
|
||||
var ogMetadata storepb.OpenGraphMetadata
|
||||
if err := protojson.Unmarshal([]byte(openGraphMetadataString), &ogMetadata); err != nil {
|
||||
@ -234,7 +220,3 @@ func filterTags(tags []string) []string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func convertVisibilityStringToStorepb(visibility string) storepb.Visibility {
|
||||
return storepb.Visibility(storepb.Visibility_value[visibility])
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e
|
||||
VALUES (?, ?, ?, ?)
|
||||
RETURNING id, created_ts, updated_ts, row_status
|
||||
`
|
||||
var rowStatus string
|
||||
if err := d.db.QueryRowContext(ctx, stmt,
|
||||
create.Email,
|
||||
create.Nickname,
|
||||
@ -28,19 +29,20 @@ func (d *DB) CreateUser(ctx context.Context, create *store.User) (*store.User, e
|
||||
&create.ID,
|
||||
&create.CreatedTs,
|
||||
&create.UpdatedTs,
|
||||
&create.RowStatus,
|
||||
&rowStatus,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user := create
|
||||
user.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.User, error) {
|
||||
set, args := []string{}, []any{}
|
||||
if v := update.RowStatus; v != nil {
|
||||
set, args = append(set, "row_status = ?"), append(args, *v)
|
||||
set, args = append(set, "row_status = ?"), append(args, v.String())
|
||||
}
|
||||
if v := update.Email; v != nil {
|
||||
set, args = append(set, "email = ?"), append(args, *v)
|
||||
@ -67,11 +69,12 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U
|
||||
`
|
||||
args = append(args, update.ID)
|
||||
user := &store.User{}
|
||||
var rowStatus string
|
||||
if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
|
||||
&user.ID,
|
||||
&user.CreatedTs,
|
||||
&user.UpdatedTs,
|
||||
&user.RowStatus,
|
||||
&rowStatus,
|
||||
&user.Email,
|
||||
&user.Nickname,
|
||||
&user.PasswordHash,
|
||||
@ -79,7 +82,7 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@ -125,11 +128,12 @@ func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User
|
||||
list := make([]*store.User, 0)
|
||||
for rows.Next() {
|
||||
user := &store.User{}
|
||||
var rowStatus string
|
||||
if err := rows.Scan(
|
||||
&user.ID,
|
||||
&user.CreatedTs,
|
||||
&user.UpdatedTs,
|
||||
&user.RowStatus,
|
||||
&rowStatus,
|
||||
&user.Email,
|
||||
&user.Nickname,
|
||||
&user.PasswordHash,
|
||||
@ -137,6 +141,7 @@ func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.RowStatus = store.ConvertRowStatusStringToStorepb(rowStatus)
|
||||
list = append(list, user)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user