-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.go
157 lines (126 loc) · 3.48 KB
/
mount.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
package retropg
import (
"context"
"fmt"
"time"
"github.com/kralamoure/retro"
"github.com/kralamoure/retro/retrotyp"
)
func (r *Db) CreateMount(ctx context.Context, mount retro.Mount) (id int, err error) {
query := "INSERT INTO retro.mounts (template_id, character_id, name, sex, xp, capacities, validity)" +
" VALUES ($1, $2, $3, $4, $5, $6, $7)" +
" RETURNING id;"
var characterId *int
if mount.CharacterId != 0 {
characterId = &mount.CharacterId
}
var validity *time.Time
if !mount.Validity.IsZero() {
validity = &mount.Validity
}
capacities := make([]int, len(mount.Capacities))
for i, v := range mount.Capacities {
capacities[i] = int(v)
}
err = dbError(
r.pool.QueryRow(ctx, query,
mount.TemplateId, characterId, mount.Name, mount.Sex, mount.XP, capacities, validity,
).Scan(&id),
)
return
}
func (r *Db) UpdateMount(ctx context.Context, mount retro.Mount) error {
query := "UPDATE retro.mounts" +
" SET template_id = $2, character_id = $3, name = $4, sex = $5, xp = $6, capacities = $7, validity = $8" +
" WHERE id = $1;"
var characterId *int
if mount.CharacterId != 0 {
characterId = &mount.CharacterId
}
var validity *time.Time
if !mount.Validity.IsZero() {
validity = &mount.Validity
}
capacities := make([]int, len(mount.Capacities))
for i, v := range mount.Capacities {
capacities[i] = int(v)
}
tag, err := r.pool.Exec(ctx, query, mount.Id, mount.TemplateId, characterId, mount.Name, mount.Sex, mount.XP,
capacities, validity)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return retro.ErrNotFound
}
return nil
}
func (r *Db) DeleteMount(ctx context.Context, id int) error {
query := "DELETE FROM retro.mounts" +
" WHERE id = $1;"
tag, err := r.pool.Exec(ctx, query, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return retro.ErrNotFound
}
return nil
}
func (r *Db) Mount(ctx context.Context, id int) (retro.Mount, error) {
var mount retro.Mount
mounts, err := r.mounts(ctx, "id = $1", id)
if err != nil {
return mount, err
}
if len(mounts) != 1 {
return mount, retro.ErrNotFound
}
for k := range mounts {
mount = mounts[k]
}
return mount, nil
}
func (r *Db) Mounts(ctx context.Context) (items map[int]retro.Mount, err error) {
return r.mounts(ctx, "")
}
func (r *Db) MountsByCharacterId(ctx context.Context, characterId int) (items map[int]retro.Mount, err error) {
return r.mounts(ctx, "character_id = $1", characterId)
}
func (r *Db) mounts(ctx context.Context, conditions string, args ...interface{}) (map[int]retro.Mount, error) {
query := "SELECT id, template_id, character_id, name, sex, xp, capacities, validity" +
" FROM retro.mounts"
if conditions != "" {
query += fmt.Sprintf(" WHERE %s", conditions)
}
query += ";"
rows, err := r.pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
mounts := make(map[int]retro.Mount)
for rows.Next() {
var mount retro.Mount
var characterId *int
var validity *time.Time
var capacities []int
err = rows.Scan(&mount.Id, &mount.TemplateId, &characterId, &mount.Name, &mount.Sex, &mount.XP,
&capacities, &validity)
if err != nil {
return nil, err
}
if characterId != nil {
mount.CharacterId = *characterId
}
if validity != nil {
mount.Validity = *validity
}
mount.Capacities = make([]retrotyp.MountCapacityId, len(capacities))
for i, v := range capacities {
mount.Capacities[i] = retrotyp.MountCapacityId(v)
}
mounts[mount.Id] = mount
}
return mounts, nil
}