2023-01-01 23:55:02 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
type Activity struct {
|
2023-06-26 23:06:53 +08:00
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
CreatorID int
|
|
|
|
CreatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Type string
|
|
|
|
Level string
|
|
|
|
Payload string
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:56:42 +08:00
|
|
|
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {
|
2023-06-26 23:06:53 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
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 {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-04 10:05:57 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
|
|
|
|
activity := create
|
|
|
|
return activity, nil
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|