mirror of
https://github.com/usememos/memos.git
synced 2025-01-06 23:36:31 +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
|
Payload string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FindActivity struct {
|
||||||
|
ID *int32
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
|
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
|
||||||
return s.driver.CreateActivity(ctx, create)
|
return s.driver.CreateActivity(ctx, create)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) ListActivity(ctx context.Context, find *FindActivity) ([]*Activity, error) {
|
||||||
|
return s.driver.ListActivity(ctx, find)
|
||||||
|
}
|
||||||
|
@ -37,13 +37,34 @@ func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store
|
|||||||
return nil, errors.Wrap(err, "failed to get last insert id")
|
return nil, errors.Wrap(err, "failed to get last insert id")
|
||||||
}
|
}
|
||||||
|
|
||||||
return d.FindActivity(ctx, id)
|
id32 := int32(id)
|
||||||
|
|
||||||
|
list, err := d.ListActivity(ctx, &store.FindActivity{ID: &id32})
|
||||||
|
if err != nil || len(list) == 0 {
|
||||||
|
return nil, errors.Wrap(err, "failed to find activity")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DB) FindActivity(ctx context.Context, id int64) (*store.Activity, error) {
|
return list[0], nil
|
||||||
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(
|
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.ID,
|
||||||
&activity.CreatorID,
|
&activity.CreatorID,
|
||||||
&activity.Type,
|
&activity.Type,
|
||||||
@ -51,8 +72,15 @@ func (d *DB) FindActivity(ctx context.Context, id int64) (*store.Activity, error
|
|||||||
&activity.Payload,
|
&activity.Payload,
|
||||||
&activity.CreatedTs,
|
&activity.CreatedTs,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to db.QueryRow")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &activity, nil
|
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
|
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.
|
// Activity model related methods.
|
||||||
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
|
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
|
||||||
|
ListActivity(ctx context.Context, find *FindActivity) ([]*Activity, error)
|
||||||
|
|
||||||
// Resource model related methods.
|
// Resource model related methods.
|
||||||
CreateResource(ctx context.Context, create *Resource) (*Resource, error)
|
CreateResource(ctx context.Context, create *Resource) (*Resource, error)
|
||||||
|
Loading…
Reference in New Issue
Block a user