-
Notifications
You must be signed in to change notification settings - Fork 36
/
notices.go
193 lines (157 loc) · 4.28 KB
/
notices.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package sqlite
import (
"context"
"database/sql"
"fmt"
"github.com/friendsofgo/errors"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb/sqlite/models"
)
// compiler assertion to ensure the struct fullfills the interface
var _ roomdb.PinnedNoticesService = (*PinnedNotices)(nil)
type PinnedNotices struct {
db *sql.DB
}
// List returns a sortable map of all the pinned notices
func (pn PinnedNotices) List(ctx context.Context) (roomdb.PinnedNotices, error) {
// get all the pins and eager-load the related notices
lst, err := models.Pins(qm.Load("Notices")).All(ctx, pn.db)
if err != nil {
return nil, err
}
// prepare a map for them
var pinnedMap = make(roomdb.PinnedNotices, len(lst))
for _, entry := range lst {
// skip a pin if it has no entries
noticeCount := len(entry.R.Notices)
if noticeCount == 0 {
continue
}
name := roomdb.PinnedNoticeName(entry.Name)
// get the exisint map entry or create a new slice
notices, has := pinnedMap[name]
if !has {
notices = make([]roomdb.Notice, 0, noticeCount)
}
// add all the related notice to the slice
for _, n := range entry.R.Notices {
relatedNotice := roomdb.Notice{
ID: n.ID,
Title: n.Title,
Content: n.Content,
Language: n.Language,
}
notices = append(notices, relatedNotice)
}
// update the map
pinnedMap[name] = notices
}
return pinnedMap, nil
}
func (pn PinnedNotices) Get(ctx context.Context, name roomdb.PinnedNoticeName, lang string) (*roomdb.Notice, error) {
p, err := models.Pins(
qm.Where("name = ?", name),
qm.Load("Notices", qm.Where("language = ?", lang)),
).One(ctx, pn.db)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, roomdb.ErrNotFound
}
return nil, err
}
if n := len(p.R.Notices); n != 1 {
return nil, fmt.Errorf("pinnedNotice: expected 1 notice but got %d", n)
}
modelNotice := p.R.Notices[0]
return &roomdb.Notice{
ID: modelNotice.ID,
Title: modelNotice.Title,
Content: modelNotice.Content,
Language: modelNotice.Language,
}, nil
}
func (pn PinnedNotices) Set(ctx context.Context, name roomdb.PinnedNoticeName, noticeID int64) error {
if !name.Valid() {
return fmt.Errorf("pinned notice: invalid notice name: %s", name)
}
n, err := models.FindNotice(ctx, pn.db, noticeID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return roomdb.ErrNotFound
}
return err
}
p, err := models.Pins(qm.Where("name = ?", name)).One(ctx, pn.db)
if err != nil {
return err
}
err = p.AddNotices(ctx, pn.db, false, n)
if err != nil {
return err
}
return nil
}
// compiler assertion to ensure the struct fullfills the interface
var _ roomdb.NoticesService = (*Notices)(nil)
type Notices struct {
db *sql.DB
}
func (n Notices) GetByID(ctx context.Context, id int64) (roomdb.Notice, error) {
var notice roomdb.Notice
dbEntry, err := models.FindNotice(ctx, n.db, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return notice, roomdb.ErrNotFound
}
return notice, err
}
// convert models type to admindb type
notice.ID = dbEntry.ID
notice.Title = dbEntry.Title
notice.Language = dbEntry.Language
notice.Content = dbEntry.Content
return notice, nil
}
func (n Notices) RemoveID(ctx context.Context, id int64) error {
dbEntry, err := models.FindNotice(ctx, n.db, id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return roomdb.ErrNotFound
}
return err
}
_, err = dbEntry.Delete(ctx, n.db)
if err != nil {
return err
}
return nil
}
func (n Notices) Save(ctx context.Context, p *roomdb.Notice) error {
if p.ID == 0 {
var newEntry models.Notice
newEntry.Title = p.Title
newEntry.Content = p.Content
newEntry.Language = p.Language
err := newEntry.Insert(ctx, n.db, boil.Whitelist("title", "content", "language"))
if err != nil {
return err
}
p.ID = newEntry.ID
return nil
}
var existing models.Notice
existing.ID = p.ID
existing.Title = p.Title
existing.Content = p.Content
existing.Language = p.Language
_, err := existing.Update(ctx, n.db, boil.Whitelist("title", "content", "language"))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return roomdb.ErrNotFound
}
return err
}
return nil
}