Skip to content

Commit c1ebd84

Browse files
committed
Add create control
Signed-off-by: gearnode <bryan@frimin.fr>
1 parent ec75411 commit c1ebd84

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

pkg/probo/coredata/control.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ LIMIT 1;
117117
return nil
118118
}
119119

120+
func (c Control) Insert(
121+
ctx context.Context,
122+
conn pg.Conn,
123+
) error {
124+
q := `
125+
INSERT INTO
126+
controls (
127+
id,
128+
control_id,
129+
name,
130+
description,
131+
content_ref,
132+
state,
133+
created_at,
134+
updated_at
135+
)
136+
VALUES (
137+
@control_id,
138+
@framework_id,
139+
@name,
140+
@description,
141+
@content_ref,
142+
@state,
143+
@created_at,
144+
@updated_at
145+
);
146+
`
147+
148+
args := pgx.NamedArgs{
149+
"control_id": c.ID,
150+
"framework_id": c.FrameworkID,
151+
"name": c.Name,
152+
"description": c.Description,
153+
"content_ref": c.ContentRef,
154+
"created_at": c.CreatedAt,
155+
"updated_at": c.UpdatedAt,
156+
}
157+
_, err := conn.Exec(ctx, q, args)
158+
return err
159+
}
160+
120161
func (c *Controls) LoadByFrameworkID(
121162
ctx context.Context,
122163
conn pg.Conn,

pkg/probo/create_control.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9+
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11+
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12+
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13+
// PERFORMANCE OF THIS SOFTWARE.
14+
15+
package probo
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"time"
21+
22+
"github.com/getprobo/probo/pkg/gid"
23+
"github.com/getprobo/probo/pkg/probo/coredata"
24+
"go.gearno.de/kit/pg"
25+
)
26+
27+
type (
28+
CreateControlRequest struct {
29+
FrameworkID gid.GID
30+
Name string
31+
Description string
32+
ContentRef string
33+
}
34+
)
35+
36+
func (s Service) CreateControl(
37+
ctx context.Context,
38+
req CreateControlRequest,
39+
) (*coredata.Control, error) {
40+
now := time.Now()
41+
controlID, err := gid.NewGID(coredata.ControlEntityType)
42+
if err != nil {
43+
return nil, fmt.Errorf("cannot create global id: %w", err)
44+
}
45+
46+
framework := &coredata.Framework{}
47+
control := &coredata.Control{
48+
ID: controlID,
49+
FrameworkID: req.FrameworkID,
50+
Name: req.Name,
51+
Description: req.Description,
52+
ContentRef: req.ContentRef,
53+
CreatedAt: now,
54+
UpdatedAt: now,
55+
}
56+
57+
err = s.pg.WithTx(
58+
ctx,
59+
func(conn pg.Conn) error {
60+
if err := framework.LoadByID(ctx, conn, s.scope, req.FrameworkID); err != nil {
61+
return fmt.Errorf("cannot load framework %q: %w", req.FrameworkID, err)
62+
}
63+
64+
if err := control.Insert(ctx, conn); err != nil {
65+
return fmt.Errorf("cannot insert control: %w", err)
66+
}
67+
68+
return nil
69+
},
70+
)
71+
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
return control, nil
77+
}

0 commit comments

Comments
 (0)