-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.go
255 lines (173 loc) · 5.83 KB
/
product.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"crypto/sha1"
"fmt"
"log"
)
type Product struct {
id string
credentials_id string
store_id string
product_code string
category string
name string
description string
price string
currency string
image_name string
}
func (product *Product) CreateNewProduct(dbConnection *DBConnection) bool {
sha1Hash := sha1.New()
sha1Hash.Write([]byte(product.credentials_id + " " + product.name))
sha1HashString := sha1Hash.Sum(nil)
productID := fmt.Sprintf("%x", sha1HashString)
if err := dbConnection.db.Ping(); err != nil {
log.Fatal(err)
return false
}
query := "INSERT INTO products(id, credentials_id, stores_id, product_code, category, name, description, price, currency, deleted, date_created, date_modified) " +
"VALUES('" + productID + "','" + product.credentials_id + "','" + product.store_id + "','" + product.product_code + "', " + product.category + ", '" + product.name + "', '" + product.description + "', '" +
product.price + "', '" + product.currency + "', 0, NOW(), NOW())"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
sha1Hash.Write([]byte(product.credentials_id + " " + product.image_name))
sha1HashString = sha1Hash.Sum(nil)
productImageID := fmt.Sprintf("%x", sha1HashString)
query = "INSERT INTO products_gallery(id, credentials_id, stores_id, products_id, image_name, main_photo, deleted, date_created, date_modified) " +
"VALUES('" + productImageID + "','" + product.credentials_id + "','" + product.store_id + "','" + product.id + "','" + product.image_name + "','YES',0,NOW(), NOW())"
_, err = dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) UpdateExistingProduct(dbConnection *DBConnection) bool {
query := "UPDATE products SET product_code='" + product.product_code + "', category=" + product.category + ", name='" + product.name + "', description='" + product.description + "', price='" + product.price + "', currency='" + product.currency + "' " +
"WHERE id='" + product.id + "'"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) CreateNewProductPhoto(dbConnection *DBConnection) bool {
sha1Hash := sha1.New()
sha1Hash.Write([]byte(product.credentials_id + " " + product.name + " " + product.image_name))
sha1HashString := sha1Hash.Sum(nil)
productImageID := fmt.Sprintf("%x", sha1HashString)
if err := dbConnection.db.Ping(); err != nil {
log.Fatal(err)
return false
}
query := "INSERT INTO products_gallery(id, credentials_id, stores_id, products_id, image_name, main_photo, deleted, date_created, date_modified) " +
"VALUES('" + productImageID + "','" + product.credentials_id + "','" + product.store_id + "','" + product.id + "','" + product.image_name + "','NO',0,NOW(), NOW())"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) DeleteProductPhoto(dbConnection *DBConnection, photoID string) bool {
query := "UPDATE products_gallery SET deleted=1, date_modified=NOW() WHERE id='" + photoID + "'"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) DeleteExistingProduct(dbConnection *DBConnection) bool {
query := "UPDATE products SET deleted=1 WHERE id='" + product.id + "'"
_, err := dbConnection.db.Exec(query)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func (product *Product) ListAllProductsByStore(dbConnection *DBConnection) []*Product {
query := "SELECT p.id, p.credentials_id, p.stores_id, p.product_code, p.category, p.name, p.description, p.price, p.currency, pg.image_name FROM products p LEFT JOIN products_gallery pg " +
"ON p.stores_id='" + product.store_id + "' AND p.deleted=0 AND p.id=pg.products_id AND pg.main_photo='YES' AND p.deleted=0"
rows, err := dbConnection.db.Query(query)
if err != nil {
log.Fatal(err)
return nil
}
defer rows.Close()
products := make([]*Product, 0)
for rows.Next() {
newProduct := new(Product)
err := rows.Scan(&newProduct.id, &newProduct.credentials_id, &newProduct.store_id, &newProduct.category, &newProduct.name, &newProduct.description, &newProduct.price, &newProduct.currency, &newProduct.image_name)
if err != nil {
log.Fatal(err)
return nil
}
products = append(products, newProduct)
}
return products
}
func (product *Product) SetLikeToProduct(dbConnection *DBConnection) bool {
client, err := dbConnection.cache.Get()
if err != nil {
log.Fatal(err)
return false
}
defer dbConnection.cache.Put(client)
r := client.Cmd("select", 8)
if r.Err != nil {
fmt.Println("Error selecting DB")
log.Fatal(r.Err)
return false
}
r = client.Cmd("sadd", "Product:Likes:"+product.id, product.credentials_id)
if r.Err != nil {
log.Fatal(r.Err)
return false
}
return true
}
func (product *Product) RemoveLikeFromProduct(dbConnection *DBConnection) bool {
client, err := dbConnection.cache.Get()
if err != nil {
log.Fatal(err)
return false
}
defer dbConnection.cache.Put(client)
r := client.Cmd("select", 8)
if r.Err != nil {
log.Fatal(r.Err)
return false
}
r = client.Cmd("srem", "Product:Likes:"+product.id, product.credentials_id)
if r.Err != nil {
log.Fatal(r.Err)
return false
}
return true
}
func (product *Product) ReadProductsLikes(dbConnection *DBConnection) []string {
client, err := dbConnection.cache.Get()
if err != nil {
log.Fatal(err)
return nil
}
defer dbConnection.cache.Put(client)
r := client.Cmd("select", 8)
if r.Err != nil {
log.Fatal(r.Err)
return nil
}
r = client.Cmd("smembers", "Product:Likes:"+product.id)
if r.Err != nil {
log.Fatal(r.Err)
return nil
}
vals, _ := r.List()
return vals
}