~evanj/cms

ref: ed17a2199584ba94d1ca44e9002b52c48b2c02fa cms/internal/s/db/hook.go -rw-r--r-- 4.6 KiB
ed17a219Evan J Feat(context): Thread context throughout data layer. TODO: Update 1 year, 20 days ago
                                                                                
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package db

import (
	"context"
	"database/sql"
	"strconv"

	"git.sr.ht/~evanj/cms/internal/m/hook"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/user"
)

const (
	insert = `
		INSERT INTO cms_hooks (SPACE_ID, URL) 
		VALUES (?, ?)
	`

	query = `
		SELECT ID, URL, SPACE_ID
		FROM cms_hooks
		WHERE ID = ?
	`

	delete = `
		DELETE FROM cms_hooks
		WHERE ID = ?
	`
)

type Hook struct {
	HookID  string
	HookURL string
	spaceID string
}

func (db *DB) HookNew(ctx context.Context, u user.User, space space.Space, url string) (hook.Hook, error) {
	t, err := db.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	res, err := t.ExecContext(ctx, insert, space.ID(), url)
	if err != nil {
		return nil, err
	}

	id, err := res.LastInsertId()
	if err != nil {
		return nil, err
	}

	var hook Hook
	if err := t.QueryRowContext(ctx, query, id).Scan(&hook.HookID, &hook.HookURL, &hook.spaceID); err != nil {
		return nil, err
	}

	return &hook, t.Commit()
}

func (db *DB) hookGet(ctx context.Context, t *sql.Tx, space space.Space, id string) (hook.Hook, error) {
	var hook Hook
	if err := t.QueryRowContext(ctx, query, id).Scan(&hook.HookID, &hook.HookURL, &hook.spaceID); err != nil {
		return nil, err
	}

	return &hook, nil
}

func (db *DB) HookGet(ctx context.Context, u user.User, space space.Space, id string) (hook.Hook, error) {
	t, err := db.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	h, err := db.hookGet(ctx, t, space, id)
	if err != nil {
		return nil, err
	}

	return h, t.Commit()
}

func (db *DB) HookDelete(ctx context.Context, u user.User, s space.Space, h hook.Hook) error {
	t, err := db.BeginTx(ctx, nil)
	if err != nil {
		return err
	}
	defer t.Rollback()

	if _, err := t.ExecContext(ctx, delete, h.ID()); err != nil {
		return err
	}

	return t.Commit()
}

func (db *DB) hooksPerSpace(ctx context.Context, t *sql.Tx, space space.Space, before int) (hook.HookList, error) {
	var (
		r       []hook.Hook
		id      int
		hasMore bool
	)

	before = beformat(before)

	q := `
		SELECT ID FROM cms_hooks
		WHERE SPACE_ID = ? AND ID < ?
		ORDER BY ID DESC LIMIT ?
	 `

	rows, err := t.QueryContext(ctx, q, space.ID(), before, perPage+1)
	if err != nil {
		return nil, err
	}

	var ids []int
	for i := 0; rows.Next(); i++ {
		if i == perPage {
			hasMore = true
			break
		}

		if err := rows.Scan(&id); err != nil {
			return nil, err
		}

		ids = append(ids, id)
	}

	if err := rows.Close(); err != nil {
		return nil, err
	}

	for _, id := range ids {
		ct, err := db.hookGet(ctx, t, space, strconv.Itoa(id))
		if err != nil {
			return nil, err
		}

		r = append(r, ct)
	}

	return newHookList(r, hasMore, id), nil
}

func (db *DB) HooksPerSpace(ctx context.Context, u user.User, space space.Space, before int) (hook.HookList, error) {
	t, err := db.BeginTx(ctx, nil)
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	list, err := db.hooksPerSpace(ctx, t, space, before)
	if err != nil {
		return nil, err
	}

	return list, t.Commit()
}

// Interface impl.

func (h *Hook) ID() string  { return h.HookID }
func (h *Hook) URL() string { return h.HookURL }

// HOOK ITER STRUCT / INTERFACE

type hookIter struct {
	db    *DB
	ctx   context.Context
	t     *sql.Tx
	space space.Space

	// For pump
	list hook.HookList
	err  error
}

func (db *DB) HookIter(ctx context.Context, t *sql.Tx, u user.User, space space.Space) *hookIter {
	iter := &hookIter{db, ctx, t, space, newHookList(nil, false, 0), nil}
	iter.pump()
	return iter
}

func (iter *hookIter) pump() {
	list, err := iter.db.hooksPerSpace(iter.ctx, iter.t, iter.space, iter.list.Before())
	iter.list = list
	iter.err = err
}

func (iter *hookIter) Next() bool {
	if iter.err != nil {
		return true // Error is picked up with Scan call.
	}
	return len(iter.list.List()) > 0
}

func (iter *hookIter) Scan() (hook.Hook, error) {
	var (
		first hook.Hook
		err   error
	)

	if iter.err != nil {
		return first, iter.err
	}

	list := iter.list.List()
	first, rest := list[0], list[1:]

	iter.list = newHookList(rest, iter.list.More(), iter.list.Before())
	if err != nil {
		return nil, err
	}

	if len(iter.list.List()) < 1 {
		iter.pump()
	}

	return first, nil
}

// HOOK LIST STRUCT / INTERFACE

type HookList struct {
	HookList       []hook.Hook
	HookListMore   bool
	HookListBefore int
}

func newHookList(list []hook.Hook, hasMore bool, last int) *HookList {
	return &HookList{list, hasMore, last}
}

func (hl *HookList) List() []hook.Hook { return hl.HookList }
func (hl *HookList) More() bool        { return hl.HookListMore }
func (hl *HookList) Before() int       { return hl.HookListBefore }