summaryrefslogtreecommitdiff
path: root/internal/data/networks.sql.go
blob: 5e1e0dbd8181fc902fab6cb1ab07f777a6755526 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: networks.sql

package data

import (
	"context"

	"github.com/kyren223/eko/pkg/snowflake"
)

const createNetwork = `-- name: CreateNetwork :one
INSERT INTO networks (
  id, owner_id, name, is_public,
  icon, bg_hex_color, fg_hex_color
) VALUES (
  ?, ?, ?, ?, 
  ?, ?, ?
)
RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public
`

type CreateNetworkParams struct {
	ID         snowflake.ID
	OwnerID    snowflake.ID
	Name       string
	IsPublic   bool
	Icon       string
	BgHexColor string
	FgHexColor string
}

func (q *Queries) CreateNetwork(ctx context.Context, arg CreateNetworkParams) (Network, error) {
	row := q.db.QueryRowContext(ctx, createNetwork,
		arg.ID,
		arg.OwnerID,
		arg.Name,
		arg.IsPublic,
		arg.Icon,
		arg.BgHexColor,
		arg.FgHexColor,
	)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}

const deleteNetwork = `-- name: DeleteNetwork :exec
DELETE FROM networks WHERE id = ?
`

func (q *Queries) DeleteNetwork(ctx context.Context, id snowflake.ID) error {
	_, err := q.db.ExecContext(ctx, deleteNetwork, id)
	return err
}

const getNetworkBannedUsers = `-- name: GetNetworkBannedUsers :many
SELECT
  users.id, users.name, users.public_key, users.description, users.is_public_dm, users.is_deleted,
  users_networks.ban_reason
FROM users_networks
JOIN users ON users.id = users_networks.user_id
WHERE users_networks.network_id = ?
`

type GetNetworkBannedUsersRow struct {
	User      User
	BanReason *string
}

func (q *Queries) GetNetworkBannedUsers(ctx context.Context, networkID snowflake.ID) ([]GetNetworkBannedUsersRow, error) {
	rows, err := q.db.QueryContext(ctx, getNetworkBannedUsers, networkID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetNetworkBannedUsersRow
	for rows.Next() {
		var i GetNetworkBannedUsersRow
		if err := rows.Scan(
			&i.User.ID,
			&i.User.Name,
			&i.User.PublicKey,
			&i.User.Description,
			&i.User.IsPublicDM,
			&i.User.IsDeleted,
			&i.BanReason,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getNetworkById = `-- name: GetNetworkById :one
SELECT id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public FROM networks
WHERE id = ?
`

func (q *Queries) GetNetworkById(ctx context.Context, id snowflake.ID) (Network, error) {
	row := q.db.QueryRowContext(ctx, getNetworkById, id)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}

const getNetworkMembers = `-- name: GetNetworkMembers :many
SELECT
  users.id, users.name, users.public_key, users.description, users.is_public_dm, users.is_deleted,
  users_networks.joined_at,
  users_networks.is_admin,
  users_networks.is_muted
FROM users_networks
JOIN users ON users.id = users_networks.user_id
WHERE users_networks.network_id = ? AND is_member = true
`

type GetNetworkMembersRow struct {
	User     User
	JoinedAt string
	IsAdmin  bool
	IsMuted  bool
}

func (q *Queries) GetNetworkMembers(ctx context.Context, networkID snowflake.ID) ([]GetNetworkMembersRow, error) {
	rows, err := q.db.QueryContext(ctx, getNetworkMembers, networkID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetNetworkMembersRow
	for rows.Next() {
		var i GetNetworkMembersRow
		if err := rows.Scan(
			&i.User.ID,
			&i.User.Name,
			&i.User.PublicKey,
			&i.User.Description,
			&i.User.IsPublicDM,
			&i.User.IsDeleted,
			&i.JoinedAt,
			&i.IsAdmin,
			&i.IsMuted,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getPublicNetworks = `-- name: GetPublicNetworks :many
SELECT id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public FROM networks
WHERE is_public = true
`

func (q *Queries) GetPublicNetworks(ctx context.Context) ([]Network, error) {
	rows, err := q.db.QueryContext(ctx, getPublicNetworks)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Network
	for rows.Next() {
		var i Network
		if err := rows.Scan(
			&i.ID,
			&i.OwnerID,
			&i.Name,
			&i.Icon,
			&i.BgHexColor,
			&i.FgHexColor,
			&i.IsPublic,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const setNetworkIcon = `-- name: SetNetworkIcon :one
UPDATE networks SET
  icon = ?,
  bg_hex_color = ?,
  fg_hex_color = ?
WHERE id = ?
RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public
`

type SetNetworkIconParams struct {
	Icon       string
	BgHexColor string
	FgHexColor string
	ID         snowflake.ID
}

func (q *Queries) SetNetworkIcon(ctx context.Context, arg SetNetworkIconParams) (Network, error) {
	row := q.db.QueryRowContext(ctx, setNetworkIcon,
		arg.Icon,
		arg.BgHexColor,
		arg.FgHexColor,
		arg.ID,
	)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}

const setNetworkIsPublic = `-- name: SetNetworkIsPublic :one
UPDATE networks SET
  is_public = ?
WHERE id = ?
RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public
`

type SetNetworkIsPublicParams struct {
	IsPublic bool
	ID       snowflake.ID
}

func (q *Queries) SetNetworkIsPublic(ctx context.Context, arg SetNetworkIsPublicParams) (Network, error) {
	row := q.db.QueryRowContext(ctx, setNetworkIsPublic, arg.IsPublic, arg.ID)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}

const setNetworkName = `-- name: SetNetworkName :one
UPDATE networks SET
  name = ?
WHERE id = ?
RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public
`

type SetNetworkNameParams struct {
	Name string
	ID   snowflake.ID
}

func (q *Queries) SetNetworkName(ctx context.Context, arg SetNetworkNameParams) (Network, error) {
	row := q.db.QueryRowContext(ctx, setNetworkName, arg.Name, arg.ID)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}

const setNetworkUser = `-- name: SetNetworkUser :one
INSERT INTO users_networks (
  user_id, network_id,
  is_member, is_admin, is_muted,
  is_banned, ban_reason
) VALUES (
  ?, ?,
  ?, ?, ?,
  ?, ?
)
ON CONFLICT DO 
UPDATE SET
  is_member = ?, is_admin = ?, is_muted = ?,
  is_banned = ?, ban_reason = ?
WHERE user_id = ? AND network_id = ?
RETURNING user_id, network_id, joined_at, is_member, is_admin, is_muted, is_banned, ban_reason
`

type SetNetworkUserParams struct {
	UserID    snowflake.ID
	NetworkID snowflake.ID
	IsMember  bool
	IsAdmin   bool
	IsMuted   bool
	IsBanned  bool
	BanReason *string
}

func (q *Queries) SetNetworkUser(ctx context.Context, arg SetNetworkUserParams) (UsersNetwork, error) {
	row := q.db.QueryRowContext(ctx, setNetworkUser,
		arg.UserID,
		arg.NetworkID,
		arg.IsMember,
		arg.IsAdmin,
		arg.IsMuted,
		arg.IsBanned,
		arg.BanReason,
	)
	var i UsersNetwork
	err := row.Scan(
		&i.UserID,
		&i.NetworkID,
		&i.JoinedAt,
		&i.IsMember,
		&i.IsAdmin,
		&i.IsMuted,
		&i.IsBanned,
		&i.BanReason,
	)
	return i, err
}

const transferNetwork = `-- name: TransferNetwork :one
UPDATE networks SET
  owner_id = ?
WHERE id = ?
RETURNING id, owner_id, name, icon, bg_hex_color, fg_hex_color, is_public
`

type TransferNetworkParams struct {
	OwnerID snowflake.ID
	ID      snowflake.ID
}

func (q *Queries) TransferNetwork(ctx context.Context, arg TransferNetworkParams) (Network, error) {
	row := q.db.QueryRowContext(ctx, transferNetwork, arg.OwnerID, arg.ID)
	var i Network
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.Name,
		&i.Icon,
		&i.BgHexColor,
		&i.FgHexColor,
		&i.IsPublic,
	)
	return i, err
}