-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepository_project.go
69 lines (57 loc) · 2.1 KB
/
repository_project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package repository
import (
"context"
"github.com/gofrs/uuid"
. "github.com/networkteam/qrb"
"github.com/networkteam/qrb/builder"
"github.com/networkteam/qrb/fn"
"github.com/networkteam/qrb/qrbpgx"
"github.com/networkteam/construct/v2/constructpgx"
"github.com/networkteam/construct/v2/example/pgx/model"
)
type ProjectQueryOpts struct {
IncludeTodoCount bool
}
// projectBuildFindQuery creates a partial builder.SelectBuilder that
// - selects a single JSON result by using projectJson (which selects a TodoCount if opts.IncludeTodoCount is true)
// - from the projects table
func projectBuildFindQuery(opts ProjectQueryOpts) builder.SelectBuilder {
return SelectJson(projectJson(opts)).
From(project).
SelectBuilder
}
// FindProjectByID finds a single project by id
func FindProjectByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID, opts ProjectQueryOpts) (result model.Project, err error) {
q := projectBuildFindQuery(opts).
Where(project.ID.Eq(Arg(id)))
return constructpgx.ScanRow[model.Project](
qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx),
)
}
// FindAllProjects finds all projects sorted by title
func FindAllProjects(ctx context.Context, executor qrbpgx.Executor, opts ProjectQueryOpts) (result []model.Project, err error) {
q := projectBuildFindQuery(opts).
OrderBy(project.Title)
return constructpgx.CollectRows[model.Project](
qrbpgx.Build(q).WithExecutor(executor).Query(ctx),
)
}
// InsertProject inserts a new project from a ProjectChangeSet
func InsertProject(ctx context.Context, executor qrbpgx.Executor, changeSet ProjectChangeSet) error {
q := InsertInto(project).
SetMap(changeSet.toMap())
_, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)
return err
}
func projectJson(opts ProjectQueryOpts) builder.JsonBuildObjectBuilder {
// Use the generated default select (JSON object builder) and add another property for the aggregated count#
// (if opts.IncludeTodoCount is true).
return projectDefaultJson.
PropIf(
opts.IncludeTodoCount,
"TodoCount",
Select(fn.Count(N("*"))).
From(todo).
Where(todo.ProjectID.Eq(project.ID)),
)
}