feat(store): tasks, accounts, runs, dedup journal
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
|
||||
type Task struct {
|
||||
ID int64
|
||||
Name string
|
||||
SrcEndpointID int64
|
||||
DstEndpointID int64
|
||||
Status string
|
||||
FolderMapping map[string]string
|
||||
}
|
||||
|
||||
func (s *Store) CreateTask(ctx context.Context, t Task) (int64, error) {
|
||||
if t.FolderMapping == nil {
|
||||
t.FolderMapping = map[string]string{}
|
||||
}
|
||||
var id int64
|
||||
err := s.Pool.QueryRow(ctx,
|
||||
`INSERT INTO tasks (name, src_endpoint_id, dst_endpoint_id, folder_mapping)
|
||||
VALUES ($1,$2,$3,$4) RETURNING id`,
|
||||
t.Name, t.SrcEndpointID, t.DstEndpointID, t.FolderMapping).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *Store) GetTask(ctx context.Context, id int64) (Task, error) {
|
||||
var t Task
|
||||
err := s.Pool.QueryRow(ctx,
|
||||
`SELECT id, name, src_endpoint_id, dst_endpoint_id, status, folder_mapping
|
||||
FROM tasks WHERE id=$1`, id).
|
||||
Scan(&t.ID, &t.Name, &t.SrcEndpointID, &t.DstEndpointID, &t.Status, &t.FolderMapping)
|
||||
return t, err
|
||||
}
|
||||
|
||||
func (s *Store) ListTasks(ctx context.Context) ([]Task, error) {
|
||||
rows, err := s.Pool.Query(ctx,
|
||||
`SELECT id, name, src_endpoint_id, dst_endpoint_id, status, folder_mapping
|
||||
FROM tasks ORDER BY id DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Task
|
||||
for rows.Next() {
|
||||
var t Task
|
||||
if err := rows.Scan(&t.ID, &t.Name, &t.SrcEndpointID, &t.DstEndpointID, &t.Status, &t.FolderMapping); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, t)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) SetTaskStatus(ctx context.Context, id int64, status string) error {
|
||||
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET status=$2 WHERE id=$1`, id, status)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user