mirror of
https://github.com/usememos/memos.git
synced 2025-01-03 03:38:23 +03:00
feat: add support for ListActivity (#2365)
Add support for ListActivity
This commit is contained in:
parent
3822c26e32
commit
fa2bba51c1
@ -17,6 +17,14 @@ type Activity struct {
|
||||
Payload string
|
||||
}
|
||||
|
||||
type FindActivity struct {
|
||||
ID *int32
|
||||
}
|
||||
|
||||
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
|
||||
return s.driver.CreateActivity(ctx, create)
|
||||
}
|
||||
|
||||
func (s *Store) ListActivity(ctx context.Context, find *FindActivity) ([]*Activity, error) {
|
||||
return s.driver.ListActivity(ctx, find)
|
||||
}
|
||||
|
@ -37,22 +37,50 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
|
||||
return nil, errors.Wrap(err, "failed to get last insert id")
|
||||
}
|
||||
|
||||
return d.FindActivity(ctx, id)
|
||||
}
|
||||
id32 := int32(id)
|
||||
|
||||
func (d *DB) FindActivity(ctx context.Context, id int64) (*store.Activity, error) {
|
||||
var activity store.Activity
|
||||
stmt := "SELECT `id`, `creator_id`, `type`, `level`, `payload`, UNIX_TIMESTAMP(`created_ts`) FROM `activity` WHERE `id` = ?"
|
||||
if err := d.db.QueryRowContext(ctx, stmt, id).Scan(
|
||||
&activity.ID,
|
||||
&activity.CreatorID,
|
||||
&activity.Type,
|
||||
&activity.Level,
|
||||
&activity.Payload,
|
||||
&activity.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to db.QueryRow")
|
||||
list, err := d.ListActivity(ctx, &store.FindActivity{ID: &id32})
|
||||
if err != nil || len(list) == 0 {
|
||||
return nil, errors.Wrap(err, "failed to find activity")
|
||||
}
|
||||
|
||||
return &activity, nil
|
||||
return list[0], nil
|
||||
}
|
||||
|
||||
func (d *DB) ListActivity(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
||||
}
|
||||
|
||||
query := "SELECT `id`, `creator_id`, `type`, `level`, `payload`, UNIX_TIMESTAMP(`created_ts`) FROM `activity` WHERE " + strings.Join(where, " AND ")
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := []*store.Activity{}
|
||||
for rows.Next() {
|
||||
activity := &store.Activity{}
|
||||
if err := rows.Scan(
|
||||
&activity.ID,
|
||||
&activity.CreatorID,
|
||||
&activity.Type,
|
||||
&activity.Level,
|
||||
&activity.Payload,
|
||||
&activity.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list = append(list, activity)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
@ -34,3 +34,41 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
|
||||
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func (d *DB) ListActivity(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
||||
}
|
||||
|
||||
query := "SELECT `id`, `creator_id`, `type`, `level`, `payload`, `created_ts` FROM `activity` WHERE " + strings.Join(where, " AND ")
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := []*store.Activity{}
|
||||
for rows.Next() {
|
||||
activity := &store.Activity{}
|
||||
if err := rows.Scan(
|
||||
&activity.ID,
|
||||
&activity.CreatorID,
|
||||
&activity.Type,
|
||||
&activity.Level,
|
||||
&activity.Payload,
|
||||
&activity.CreatedTs,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list = append(list, activity)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ type Driver interface {
|
||||
|
||||
// Activity model related methods.
|
||||
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
|
||||
ListActivity(ctx context.Context, find *FindActivity) ([]*Activity, error)
|
||||
|
||||
// Resource model related methods.
|
||||
CreateResource(ctx context.Context, create *Resource) (*Resource, error)
|
||||
|
Loading…
Reference in New Issue
Block a user