-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
93 lines (81 loc) · 1.59 KB
/
pool.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
package pool
import (
"context"
"github.com/kycklingar/PBooru/DataManager/db"
"github.com/kycklingar/PBooru/DataManager/user"
)
type ID int
type Pool struct {
ID ID
User user.User
Title string
Description string
}
func (p Pool) AddPost(ctx context.Context, postID int) error {
_, err := db.Context.ExecContext(
ctx,
`INSERT INTO pool_mappings(pool_id, post_id, position)
VALUES(
$1, $2,
COALESCE(
(SELECT MAX(position) + 1
FROM pool_mappings
WHERE pool_id = $1),
0
)
)`,
p.ID,
postID,
)
return err
}
func (p Pool) RemovePost(ctx context.Context, postID int) error {
_, err := db.Context.ExecContext(
ctx,
`DELETE FROM pool_mappings
WHERE pool_id = $1
AND post_id = $2`,
p.ID,
postID,
)
return err
}
func (p *Pool) scan(scan db.Scanner) error {
return scan(
&p.ID,
&p.User,
&p.Title,
&p.Description,
)
}
func Create(ctx context.Context, userID user.ID, title, description string) error {
_, err := db.Context.ExecContext(
ctx,
`INSERT INTO user_pools(user_id, title, description)
VALUES($1, $2, $3)`,
userID,
title,
description,
)
return err
}
func OfUser(ctx context.Context, id user.ID) (Pools, error) {
var pools Pools
return pools, db.QueryRowsContext(
ctx, db.Context,
`SELECT id, user_id, title, description
FROM user_pools
WHERE user_id = $1`,
id,
)(pools.scan)
}
func FromID(ctx context.Context, id ID) (Pool, error) {
var pool Pool
return pool, pool.scan(db.Context.QueryRowContext(
ctx,
`SELECT id, user_id, title, description
FROM user_pools
WHERE id = $1`,
id,
).Scan)
}