-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_item.go
136 lines (109 loc) · 3.17 KB
/
character_item.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
package retropg
import (
"context"
"fmt"
"strings"
"github.com/kralamoure/retro"
"github.com/kralamoure/retro/retrotyp"
)
func (r *Db) CreateCharacterItem(ctx context.Context, item retro.CharacterItem) (id int, err error) {
query := "INSERT INTO retro.characters_items (template_id, quantity, effects, position, character_id)" +
" VALUES ($1, $2, $3, $4, $5)" +
" RETURNING id;"
effects := retro.EncodeItemEffects(item.Effects)
var position *retrotyp.CharacterItemPosition
if item.Position != -1 {
position = &item.Position
}
err = dbError(
r.pool.QueryRow(ctx, query,
item.TemplateId, item.Quantity, strings.Join(effects, ","), position, item.CharacterId,
).Scan(&id),
)
return
}
func (r *Db) UpdateCharacterItem(ctx context.Context, item retro.CharacterItem) error {
query := "UPDATE retro.characters_items" +
" SET template_id = $2, quantity = $3, effects = $4, position = $5, character_id = $6" +
" WHERE id = $1;"
effects := retro.EncodeItemEffects(item.Effects)
var position *retrotyp.CharacterItemPosition
if item.Position != -1 {
position = &item.Position
}
tag, err := r.pool.Exec(ctx, query, item.Id,
item.TemplateId, item.Quantity, strings.Join(effects, ","), position, item.CharacterId)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return retro.ErrNotFound
}
return nil
}
func (r *Db) DeleteCharacterItem(ctx context.Context, id int) error {
query := "DELETE FROM retro.characters_items" +
" 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) CharacterItemsByCharacterId(ctx context.Context, characterId int) (items map[int]retro.CharacterItem, err error) {
return r.characterItems(ctx, "character_id = $1", characterId)
}
func (r *Db) CharacterItem(ctx context.Context, id int) (retro.CharacterItem, error) {
var item retro.CharacterItem
items, err := r.characterItems(ctx, "id = $1", id)
if err != nil {
return item, err
}
if len(items) != 1 {
return item, retro.ErrNotFound
}
for k := range items {
item = items[k]
}
return item, nil
}
func (r *Db) characterItems(ctx context.Context, conditions string, args ...interface{}) (map[int]retro.CharacterItem, error) {
query := "SELECT id, template_id, quantity, effects, position, character_id" +
" FROM retro.characters_items"
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()
items := make(map[int]retro.CharacterItem)
for rows.Next() {
var item retro.CharacterItem
var effectsStr string
var position *retrotyp.CharacterItemPosition
err = rows.Scan(&item.Id, &item.TemplateId, &item.Quantity, &effectsStr, &position, &item.CharacterId)
if err != nil {
return nil, err
}
if effectsStr != "" {
effects, err := retro.DecodeItemEffects(strings.Split(effectsStr, ","))
if err != nil {
return nil, err
}
item.Effects = effects
}
if position == nil {
item.Position = -1
} else {
item.Position = *position
}
items[item.Id] = item
}
return items, nil
}