-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.go
211 lines (190 loc) · 7.36 KB
/
character.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
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
// DO NOT EDIT. Code generated by entitygen. For modifications use "make gen-model".
package model
import (
"context"
"errors"
webapi "github.com/michaljurecko/ch-demo/internal/pkg/common/dataverse/webapi"
"net/http"
)
// Character - This table contains records of Dungeons and Dragons characters.
type Character struct {
// original field stores snapshot of the entity state for the Update operation, see TrackChanges method.
original *Character
ID string `json:"cr568_characterid,omitempty"`
Name string `json:"cr568_charactername"`
Level int `json:"cr568_level"`
Strength int `json:"cr568_strength"`
Dexterity int `json:"cr568_dexterity"`
Intelligence int `json:"cr568_intelligence"`
Charisma int `json:"cr568_charisma"`
Class webapi.Lookup[Class] `json:"_cr568_classid_value"`
Race webapi.Lookup[Race] `json:"_cr568_raceid_value"`
Player webapi.Lookup[Player] `json:"_cr568_player_value"`
}
// TrackChanges internally stores entity actual state to track changes for the Update operation.
func (e *Character) TrackChanges() {
clone := *e
e.original = &clone
}
// resetChanges after the Update operation.
func (e *Character) resetChanges() {
e.original = nil
}
type CharacterRepository struct {
client *webapi.Client
}
func newCharacterRepository(client *webapi.Client) *CharacterRepository {
return &CharacterRepository{client: client}
}
// Create entity. After successful operation, the new primary ID will be set to the original entity.
func (r *CharacterRepository) Create(entity *Character) *webapi.APIRequest[Character] {
payload, err := r.createPayload(entity)
if err != nil {
return webapi.NewAPIRequestError[Character](entity, err)
}
path := "cr568_characters"
result := entity
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodPost, path, payload)
httpReq.Header("Prefer", "return=representation")
httpReq.ExpectStatus(http.StatusCreated)
return webapi.NewAPIRequest(result, httpReq)
}
// Update entity. A diff of modifications is generated and saved via API.
// Before making changes, it is necessary to call the TrackChanges entity method.
func (r *CharacterRepository) Update(entity *Character) *webapi.APIRequest[Character] {
if entity.original == nil {
err := errors.New("changes are not tracked: use \"TrackChanges\" entity method to track changes and allow \"Update\" operation")
return webapi.NewAPIRequestError[Character](entity, err)
}
payload, err := r.updatePayload(entity.original, entity)
if err != nil {
return webapi.NewAPIRequestError[Character](entity, err)
}
id := entity.ID
path := "cr568_characters" + "(" + webapi.ID(id) + ")"
result := entity
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodPatch, path, payload)
httpReq.Header("If-Match", "*") // prevent create if entity not exists
httpReq.Header("Prefer", "return=representation")
httpReq.OnSuccess(func(ctx context.Context, c *Character) error {
c.resetChanges()
return nil
})
return webapi.NewAPIRequest(result, httpReq)
}
// Delete entity by the ID.
func (r *CharacterRepository) Delete(id string) *webapi.APIRequest[webapi.NoResult] {
path := "cr568_characters" + "(" + webapi.ID(id) + ")"
httpReq := webapi.NewHTTPRequest(&webapi.NoResult{}, r.client, http.MethodDelete, path, nil)
httpReq.ExpectStatus(http.StatusNoContent)
return webapi.NewAPIRequest(&webapi.NoResult{}, httpReq)
}
func (r *CharacterRepository) All() *webapi.APIRequest[webapi.Collection[Character]] {
path := "cr568_characters"
result := &webapi.Collection[Character]{}
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodGet, path, nil)
return webapi.NewAPIRequest(result, httpReq)
}
func (r *CharacterRepository) ByID(id string) *webapi.APIRequest[Character] {
path := "cr568_characters" + "(" + webapi.ID(id) + ")"
result := &Character{}
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodGet, path, nil)
return webapi.NewAPIRequest(result, httpReq)
}
func (r *CharacterRepository) ByClass(id string) *webapi.APIRequest[webapi.Collection[Character]] {
path := "cr568_characters"
result := &webapi.Collection[Character]{}
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodGet, path, nil)
httpReq.Filter("cr568_classid eq '" + webapi.ID(id) + "'")
return webapi.NewAPIRequest(result, httpReq)
}
func (r *CharacterRepository) ByRace(id string) *webapi.APIRequest[webapi.Collection[Character]] {
path := "cr568_characters"
result := &webapi.Collection[Character]{}
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodGet, path, nil)
httpReq.Filter("cr568_raceid eq '" + webapi.ID(id) + "'")
return webapi.NewAPIRequest(result, httpReq)
}
func (r *CharacterRepository) ByPlayer(id string) *webapi.APIRequest[webapi.Collection[Character]] {
path := "cr568_characters"
result := &webapi.Collection[Character]{}
httpReq := webapi.NewHTTPRequest(result, r.client, http.MethodGet, path, nil)
httpReq.Filter("cr568_player eq '" + webapi.ID(id) + "'")
return webapi.NewAPIRequest(result, httpReq)
}
// createPayload method generates payload for the Create operation.
func (r *CharacterRepository) createPayload(entity *Character) (map[string]any, error) {
payload := map[string]any{}
payload["cr568_charactername"] = entity.Name
payload["cr568_level"] = entity.Level
payload["cr568_strength"] = entity.Strength
payload["cr568_dexterity"] = entity.Dexterity
payload["cr568_intelligence"] = entity.Intelligence
payload["cr568_charisma"] = entity.Charisma
{
idOrNil, err := lookupFullIDOrNil(entity.Class)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
{
idOrNil, err := lookupFullIDOrNil(entity.Race)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
{
idOrNil, err := lookupFullIDOrNil(entity.Player)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
return payload, nil
}
// updatePayload method generates diff of original and modified entity state for the Update operation.
func (r *CharacterRepository) updatePayload(original, modified *Character) (map[string]any, error) {
payload := map[string]any{}
if original.Name != modified.Name {
payload["cr568_charactername"] = modified.Name
}
if original.Level != modified.Level {
payload["cr568_level"] = modified.Level
}
if original.Strength != modified.Strength {
payload["cr568_strength"] = modified.Strength
}
if original.Dexterity != modified.Dexterity {
payload["cr568_dexterity"] = modified.Dexterity
}
if original.Intelligence != modified.Intelligence {
payload["cr568_intelligence"] = modified.Intelligence
}
if original.Charisma != modified.Charisma {
payload["cr568_charisma"] = modified.Charisma
}
if original.Class.ID() != modified.Class.ID() {
idOrNil, err := lookupFullIDOrNil(modified.Class)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
if original.Race.ID() != modified.Race.ID() {
idOrNil, err := lookupFullIDOrNil(modified.Race)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
if original.Player.ID() != modified.Player.ID() {
idOrNil, err := lookupFullIDOrNil(modified.Player)
if err != nil {
return nil, err
}
payload["[email protected]"] = idOrNil
}
return payload, nil
}