-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cards.go
203 lines (174 loc) · 5.88 KB
/
cards.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
package main
import (
"encoding/json"
"github.com/adonese/noebs/ebs_fields"
"github.com/adonese/noebs/utils"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-redis/redis"
"github.com/sirupsen/logrus"
"net/http"
)
func GetCards(c *gin.Context) {
redisClient := getRedis()
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "unauthorized access", "code": "unauthorized_access"})
} else {
cards, err := redisClient.ZRange(username + ":cards", 0, -1).Result()
if err != nil {
// handle the error somehow
logrus.WithFields(logrus.Fields{
"error": "unable to get results from redis",
"message": err.Error(),
}).Info("unable to get results from redis")
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": "error in redis"})
}
// unmrshall cards and send them back to the user
// they should be a []Cards
var cb ebs_fields.CardsRedis
var cardBytes []ebs_fields.CardsRedis
var id = 1
for _, v := range cards {
json.Unmarshal([]byte(v), &cb)
cb.ID = id
cardBytes = append(cardBytes, cb)
id++
}
c.JSON(http.StatusOK, gin.H{"cards": cardBytes})
}
}
func AddCards(c *gin.Context) {
redisClient := getRedis()
var fields ebs_fields.CardsRedis
err := c.ShouldBindWith(&fields, binding.JSON)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "unmarshalling_error"})
} else {
buf, _ := json.Marshal(fields)
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "unauthorized access", "code": "unauthorized_access"})
} else {
z := &redis.Z{
Member:buf,
}
if fields.IsMain {
// refactor me, please!
redisClient.HSet(username, "main_card", buf)
redisClient.ZAdd(username+":cards", z)
} else {
redisClient.ZAdd(username+":cards", z)
}
c.JSON(http.StatusCreated, gin.H{"username": username, "cards": fields})
}
}
}
// EditCards a work in progress. This function needs to be reviewed and refactored
func EditCard(c *gin.Context) {
redisClient := getRedis()
var fields ebs_fields.CardsRedis
err := c.ShouldBindWith(&fields, binding.JSON)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "unmarshalling_error"})
} else {
buf, _ := json.Marshal(fields)
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "unauthorized_access", "code": "empty_card_id"})
}else if fields.ID <= 0{
c.JSON(http.StatusBadRequest, gin.H{"message": "card id not submitted", "code": "empty_card_id"})
} else {
//FIXME please
id := fields.ID
keys, _ := redisClient.ZRange(username+":cards", int64(id), int64(id)).Result()
// after getting the key, we are offloading it to the card instance
cards := utils.RedisHelper(keys)
//z := &redis.Z{
// Member:buf,
//}
if fields.IsMain {
// refactor me, please!
redisClient.HSet(username, "main_card", buf)
// get the old item using the ID
redisClient.ZRem(username+":cards", cards)
redisClient.ZAdd(username+":cards", z)
} else {
redisClient.ZRem(username+":cards", cards)
redisClient.ZAdd(username+":cards", z)
}
c.JSON(http.StatusNoContent, gin.H{"username": username, "cards": buf, "cards_old": cards})
}
}
}
// RemoveCard a work in progress. This function needs to be reviewed and refactored
func RemoveCard(c *gin.Context) {
redisClient := getRedis()
var fields ebs_fields.CardsRedis
err := c.ShouldBindWith(&fields, binding.JSON)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "unmarshalling_error"})
// there is no error in the request body
} else {
buf, _ := json.Marshal(fields)
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "unauthorized access", "code": "unauthorized_access"})
} else if fields.ID <= 0{
c.JSON(http.StatusBadRequest, gin.H{"message": "card id not provided", "code": "card_id_not_provided"})
if fields.IsMain {
redisClient.HDel(username+":cards", "main_card")
} else {
_, err := redisClient.ZRem(username+":cards", buf).Result()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "message": "unable_to_delete"})
}
}
c.JSON(http.StatusNoContent, gin.H{"username": username, "cards": buf})
}
}
}
func AddMobile(c *gin.Context) {
redisClient := getRedis()
var fields ebs_fields.MobileRedis
err := c.ShouldBindWith(&fields, binding.JSON)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "unmarshalling_error"})
} else {
buf, _ := json.Marshal(fields)
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "unauthorized access", "code": "unauthorized_access"})
} else {
if fields.IsMain {
redisClient.HSet(username, "main_mobile", buf)
redisClient.SAdd(username+":cards", buf)
} else {
redisClient.SAdd(username+":mobile_numbers", buf)
}
c.JSON(http.StatusCreated, gin.H{"username": username, "cards": string(buf)})
}
}
}
func GetMobile(c *gin.Context) {
redisClient := getRedis()
var fields ebs_fields.CardsRedis
err := c.ShouldBindWith(&fields, binding.JSON)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "unmarshalling_error"})
} else {
buf, _ := json.Marshal(fields)
username := c.GetString("username")
if username == "" {
c.JSON(http.StatusUnauthorized, gin.H{"message": "unauthorized access", "code": "unauthorized_access"})
} else {
if fields.IsMain {
redisClient.HSet(username, "main_mobile", buf)
redisClient.SAdd(username+":mobile_numbers", buf)
} else {
redisClient.SAdd(username+":mobile_numbers", buf)
}
c.JSON(http.StatusCreated, gin.H{"username": username, "mobile_numbers": string(buf)})
}
}
}