/
storage.go
115 lines (94 loc) · 2.86 KB
/
storage.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
110
111
112
113
114
115
package profile
import (
"github.com/joaosoft/dbr"
)
type StoragePostgres struct {
config *ProfileConfig
db *dbr.Dbr
}
func NewStoragePostgres(config *ProfileConfig) (*StoragePostgres, error) {
dbr, err := dbr.New(dbr.WithConfiguration(config.Dbr))
if err != nil {
return nil, err
}
return &StoragePostgres{
config: config,
db: dbr,
}, nil
}
func (storage *StoragePostgres) GetSections() (SectionList, error) {
sections := make(SectionList, 0)
_, err := storage.db.
Select([]interface{}{"s.id_section", "s.key", "s.name", "s.description"}...).
From(dbr.As(profileTableSection, "s")).
Where("s.active").
Load(§ions)
if err != nil {
return nil, err
}
return sections, nil
}
func (storage *StoragePostgres) GetSectionsContents() (SectionsContentsList, error) {
sectionsContents := make(SectionsContentsList, 0)
_, err := storage.db.
Select("s.id_section", "s.key", "s.name", "s.description",
dbr.OnNull(
storage.db.Select(dbr.ArrayToJson(dbr.ArrayAgg(dbr.RowToJson("t")))).
From(
dbr.As(storage.db.Select("c.*", dbr.As("ct.key", "type")).
From(dbr.As(profileTableContent, "c")).
Join(dbr.As(profileTableContentType, "ct"), "ct.id_content_type = c.fk_content_type").
Where("c.fk_section = s.id_section").
Where("c.active").
OrderAsc("c.position"), "t")),
"[]", "contents")).
From(dbr.As(profileTableSection, "s")).
Where("s.active").
OrderAsc("s.position").
Load(§ionsContents)
if err != nil {
return nil, err
}
return sectionsContents, nil
}
func (storage *StoragePostgres) GetSection(sectionKey string) (*Section, error) {
section := Section{}
count, err := storage.db.
Select([]interface{}{"s.id_section", "s.key", "s.name", "s.description"}...).
From(dbr.As(profileTableSection, "s")).
Where("s.key = ?", sectionKey).
Where("s.active").
OrderAsc("s.position").
Load(§ion)
if err != nil {
return nil, err
}
if count == 0 {
return nil, nil
}
return §ion, nil
}
func (storage *StoragePostgres) GetSectionContents(sectionKey string) (*SectionContents, error) {
sectionContents := SectionContents{}
_, err := storage.db.
Select([]interface{}{"s.id_section", "s.key", "s.name", "s.description",
dbr.OnNull(
storage.db.Select(dbr.ArrayToJson(dbr.ArrayAgg(dbr.RowToJson("t")))).
From(
dbr.As(storage.db.Select("c.*", dbr.As("ct.key", "type")).
From(dbr.As(profileTableContent, "c")).
Join(dbr.As(profileTableContentType, "ct"), "ct.id_content_type = c.fk_content_type").
Where("c.fk_section = s.id_section").
Where("c.active").
OrderAsc("c.position"), "t")),
"[]", "contents")}...).
From(dbr.As(profileTableSection, "s")).
Where("s.active").
Where("s.key = ?", sectionKey).
OrderAsc("s.position").
Load(§ionContents)
if err != nil {
return nil, err
}
return §ionContents, nil
}