/
bootstrap_version.go
71 lines (53 loc) · 1.93 KB
/
bootstrap_version.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
70
71
package postgres
import (
"context"
"strings"
"github.com/satisfactorymodding/smr-api/models"
"github.com/satisfactorymodding/smr-api/util"
)
func CreateBootstrapVersion(ctx context.Context, bootstrapVersion *BootstrapVersion) (*BootstrapVersion, error) {
bootstrapVersion.ID = util.GenerateUniqueID()
DBCtx(ctx).Create(&bootstrapVersion)
return bootstrapVersion, nil
}
func GetBootstrapVersionByID(ctx context.Context, bootstrapVersionID string) *BootstrapVersion {
var bootstrapVersion BootstrapVersion
DBCtx(ctx).Find(&bootstrapVersion, "id = ?", bootstrapVersionID)
if bootstrapVersion.ID == "" {
return nil
}
return &bootstrapVersion
}
func GetBootstrapVersions(ctx context.Context, filter *models.BootstrapVersionFilter) []BootstrapVersion {
var bootstrapVersions []BootstrapVersion
query := DBCtx(ctx)
if filter != nil {
query = query.Limit(*filter.Limit).
Offset(*filter.Offset).
Order(string(*filter.OrderBy) + " " + string(*filter.Order))
if filter.Search != nil && *filter.Search != "" {
query = query.Where("to_tsvector(name) @@ to_tsquery(?)", strings.Replace(*filter.Search, " ", " & ", -1))
}
}
query.Find(&bootstrapVersions)
return bootstrapVersions
}
func GetBootstrapVersionsByID(ctx context.Context, bootstrapVersionIds []string) []BootstrapVersion {
var bootstrapVersions []BootstrapVersion
DBCtx(ctx).Find(&bootstrapVersions, "id in (?)", bootstrapVersionIds)
if len(bootstrapVersionIds) != len(bootstrapVersions) {
return nil
}
return bootstrapVersions
}
func GetBootstrapVersionCount(ctx context.Context, filter *models.BootstrapVersionFilter) int64 {
var bootstrapVersionCount int64
query := DBCtx(ctx).Model(BootstrapVersion{})
if filter != nil {
if filter.Search != nil && *filter.Search != "" {
query = query.Where("to_tsvector(name) @@ to_tsquery(?)", strings.Replace(*filter.Search, " ", " & ", -1))
}
}
query.Count(&bootstrapVersionCount)
return bootstrapVersionCount
}