chore: add view field into shortcut api

This commit is contained in:
Steven 2023-06-24 16:14:08 +08:00
parent d61c99f95a
commit 19cdea6bca
2 changed files with 23 additions and 2 deletions

View File

@ -54,6 +54,7 @@ type Shortcut struct {
Description string `json:"description"`
Visibility Visibility `json:"visibility"`
Tags []string `json:"tags"`
View int `json:"view"`
}
type CreateShortcutRequest struct {
@ -279,9 +280,20 @@ func (s *APIV1Service) composeShortcut(ctx context.Context, shortcut *Shortcut)
ID: &shortcut.CreatorID,
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Failed to get creator")
}
shortcut.Creator = convertUserFromStore(user)
activityList, err := s.Store.ListActivities(ctx, &store.FindActivity{
Type: store.ActivityShortcutView,
Level: store.ActivityInfo,
Where: []string{fmt.Sprintf("json_extract(payload, '$.shortcutId') = %d", shortcut.ID)},
})
if err != nil {
return nil, errors.Wrap(err, "Failed to list activities")
}
shortcut.View = len(activityList)
return shortcut, nil
}
@ -299,6 +311,11 @@ func convertVisibilityToStore(visibility Visibility) store.Visibility {
}
func convertShortcutFromStore(shortcut *store.Shortcut) *Shortcut {
tags := []string{}
if shortcut.Tag != "" {
tags = append(tags, strings.Split(shortcut.Tag, " ")...)
}
return &Shortcut{
ID: shortcut.ID,
CreatedTs: shortcut.CreatedTs,
@ -309,6 +326,6 @@ func convertShortcutFromStore(shortcut *store.Shortcut) *Shortcut {
Description: shortcut.Description,
Visibility: Visibility(shortcut.Visibility),
RowStatus: RowStatus(shortcut.RowStatus),
Tags: strings.Split(shortcut.Tag, " "),
Tags: tags,
}
}

View File

@ -60,6 +60,7 @@ type Activity struct {
type FindActivity struct {
Type ActivityType
Level ActivityLevel
Where []string
}
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
@ -142,6 +143,9 @@ func listActivities(ctx context.Context, tx *sql.Tx, find *FindActivity) ([]*Act
if find.Level != "" {
where, args = append(where, "level = ?"), append(args, find.Level.String())
}
if find.Where != nil {
where = append(where, find.Where...)
}
query := `
SELECT