-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagStore.go
109 lines (86 loc) · 2.9 KB
/
tagStore.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package storage
import (
_ "embed"
"fmt"
"go.uber.org/zap"
"github.com/google/uuid"
"github.com/cmsgov/mint-app/pkg/models"
"github.com/cmsgov/mint-app/pkg/shared/utilityUUID"
"github.com/cmsgov/mint-app/pkg/sqlutils"
"github.com/cmsgov/mint-app/pkg/storage/genericmodel"
)
//go:embed SQL/tag/create.sql
var tagCreateSQL string
//go:embed SQL/tag/create_collection.sql
var tagCreateCollectionSQL string
//go:embed SQL/tag/get_by_table_field_and_content_id.sql
var tagGetByTableFieldAndContentIDSQL string
// TagCreate writes a new tage to the database
func (s *Store) TagCreate(
logger *zap.Logger,
tag *models.Tag,
) (*models.Tag, error) {
tag.ID = utilityUUID.ValueOrNewUUID(tag.ID)
stmt, err := s.db.PrepareNamed(tagCreateSQL)
if err != nil {
return nil, genericmodel.HandleModelCreationError(logger, err, tag)
}
defer stmt.Close()
retTag := &models.Tag{}
err = stmt.Get(retTag, tag)
if err != nil {
return nil, genericmodel.HandleModelCreationError(logger, err, retTag)
}
return retTag, nil
}
// TagCollectionCreate creates an array of tags in the database based on the tag context provided in the string
// the method is expected to be part of a larger transaction and does not handle committing or rolling back the transactions
func TagCollectionCreate(np sqlutils.NamedPreparer, _ *zap.Logger, tags []*models.Tag, createdBy uuid.UUID) ([]*models.Tag, error) {
retTags := []*models.Tag{}
stmt, sErr := np.PrepareNamed(tagCreateCollectionSQL)
if sErr != nil {
return nil, sErr
}
defer stmt.Close()
mapSlice := []map[string]interface{}{}
for _, tag := range tags {
tag.ID = utilityUUID.ValueOrNewUUID(tag.ID) //Note, there are helper functions to do most of this, but converting manually let's us set ID and created by here as well.
tag.CreatedBy = createdBy
tMap, err := models.StructToMap(*tag)
if err != nil {
return nil, fmt.Errorf(" issue creating tags: error: %w", err)
}
mapSlice = append(mapSlice, tMap)
}
jsonTag, err := models.MapArrayToJSONArray(mapSlice)
if err != nil {
return nil, fmt.Errorf(" error converting tagArray to json: %w", err)
}
arg := map[string]interface{}{
"paramTableJSON": jsonTag,
}
err = stmt.Select(&retTags, arg)
if err != nil {
return nil, err
}
return retTags, nil
}
// TagCollectionGetByContentIDAndField returns relevant tags for specific table and field
func (s *Store) TagCollectionGetByContentIDAndField(_ *zap.Logger, taggedTable string, taggedField string, taggedContentID uuid.UUID) ([]*models.Tag, error) {
var tags []*models.Tag
stmt, err := s.db.PrepareNamed(tagGetByTableFieldAndContentIDSQL)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"tagged_content_table": taggedTable,
"tagged_field": taggedField,
"tagged_content_id": taggedContentID,
}
err = stmt.Select(&tags, arg) // This returns more than one
if err != nil {
return nil, err
}
return tags, nil
}