~evanj/cms

ref: 515c5fd9b9df1ff4652d7ce3e5b2f7df58ee5b85 cms/internal/s/db/hook.go -rw-r--r-- 3.0 KiB
515c5fd9Evan M Jones Feat(content search): Pagination for content search complete. Fix up 1 year, 2 months 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
package db

import (
	"database/sql"
	"strconv"

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

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 {
	id, url, spaceID string
}

func (db *DB) HookNew(space space.Space, url string) (hook.Hook, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

	res, err := t.Exec(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.QueryRow(query, id).Scan(&hook.id, &hook.url, &hook.spaceID); err != nil {
		return nil, err
	}

	return &hook, t.Commit()
}

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

	return &hook, nil
}

func (db *DB) HookGet(space space.Space, id string) (hook.Hook, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

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

	return h, t.Commit()
}

func (db *DB) HookDelete(s space.Space, h hook.Hook) error {
	t, err := db.Begin()
	if err != nil {
		return err
	}
	defer t.Rollback()

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

	return t.Commit()
}

func (db *DB) hooksPerSpace(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 := db.Query(q, space.ID(), before, perPage+1)
	if err != nil {
		return nil, err
	}

	for i := 0; rows.Next(); i++ {
		if i == perPage {
			hasMore = true
			break
		}

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

		ct, err := db.hookGet(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(space space.Space, before int) (hook.HookList, error) {
	t, err := db.Begin()
	if err != nil {
		return nil, err
	}
	defer t.Rollback()

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

	return list, t.Commit()
}

// Interface impl.

func (h *Hook) ID() string  { return h.id }
func (h *Hook) URL() string { return h.url }

// HOOK LIST STRUCT / INTERFACE

type HookList struct {
	HookList     []hook.Hook
	HookListMore bool
	HookListLast 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) Last() int         { return hl.HookListLast }