diff --git a/api/v1/shortcut.go b/api/v1/shortcut.go index 58e35b6..58f160d 100644 --- a/api/v1/shortcut.go +++ b/api/v1/shortcut.go @@ -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, } } diff --git a/store/activity.go b/store/activity.go index 80487a2..9406ccc 100644 --- a/store/activity.go +++ b/store/activity.go @@ -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