mirror of
https://github.com/usememos/memos.git
synced 2024-11-11 09:22:51 +08:00
50 lines
814 B
Go
50 lines
814 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type Activity struct {
|
|
ID int
|
|
|
|
// Standard fields
|
|
CreatorID int
|
|
CreatedTs int64
|
|
|
|
// Domain specific fields
|
|
Type string
|
|
Level string
|
|
Payload string
|
|
}
|
|
|
|
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
query := `
|
|
INSERT INTO activity (
|
|
creator_id,
|
|
type,
|
|
level,
|
|
payload
|
|
)
|
|
VALUES (?, ?, ?, ?)
|
|
RETURNING id, created_ts
|
|
`
|
|
if err := tx.QueryRowContext(ctx, query, create.CreatorID, create.Type, create.Level, create.Payload).Scan(
|
|
&create.ID,
|
|
&create.CreatedTs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
activity := create
|
|
return activity, nil
|
|
}
|