CreatePlan creates a new plan.
(ctx context.Context, plan *PlanMessage, creator string)
| 69 | |
| 70 | // CreatePlan creates a new plan. |
| 71 | func (s *Store) CreatePlan(ctx context.Context, plan *PlanMessage, creator string) (*PlanMessage, error) { |
| 72 | config, err := protojson.Marshal(plan.Config) |
| 73 | if err != nil { |
| 74 | return nil, errors.Wrap(err, "failed to marshal plan config") |
| 75 | } |
| 76 | |
| 77 | tx, err := s.GetDB().BeginTx(ctx, nil) |
| 78 | if err != nil { |
| 79 | return nil, errors.Wrap(err, "failed to begin tx") |
| 80 | } |
| 81 | defer tx.Rollback() |
| 82 | |
| 83 | nextID, err := nextProjectID(ctx, tx, "plan", plan.ProjectID) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | |
| 88 | q := qb.Q().Space(` |
| 89 | INSERT INTO plan ( |
| 90 | id, |
| 91 | creator, |
| 92 | project, |
| 93 | name, |
| 94 | description, |
| 95 | config |
| 96 | ) VALUES ( |
| 97 | ?, ?, ?, ?, ?, ? |
| 98 | ) RETURNING created_at, updated_at |
| 99 | `, nextID, creator, plan.ProjectID, plan.Name, plan.Description, config) |
| 100 | |
| 101 | query, args, err := q.ToSQL() |
| 102 | if err != nil { |
| 103 | return nil, errors.Wrap(err, "failed to build sql") |
| 104 | } |
| 105 | |
| 106 | if err := tx.QueryRowContext(ctx, query, args...).Scan(&plan.CreatedAt, &plan.UpdatedAt); err != nil { |
| 107 | return nil, errors.Wrap(err, "failed to insert plan") |
| 108 | } |
| 109 | |
| 110 | if err := tx.Commit(); err != nil { |
| 111 | return nil, errors.Wrap(err, "failed to commit tx") |
| 112 | } |
| 113 | |
| 114 | plan.UID = nextID |
| 115 | plan.Creator = creator |
| 116 | return plan, nil |
| 117 | } |
| 118 | |
| 119 | // GetPlan gets a plan. |
| 120 | func (s *Store) GetPlan(ctx context.Context, find *FindPlanMessage) (*PlanMessage, error) { |