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