-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
270 lines (238 loc) · 7.11 KB
/
index.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const express = require('express');
const bodyParser = require('body-parser');
const Multer = require('multer');
const cors = require('cors')
const Web3 = require('web3');
// const image = require('./public/images')
const {
addProduct,
buyProduct,
getProduct,
getAccounts,
unlockAccount,
getAllProducts,
checkRemainToken,
balance,
freeProduct
} = require('./blockchain');
const app = express();
const appPort = 3001;
app.use(cors())
app.use(express.json())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// let token;
// createContractInstance('Token').then(instance => {
// token = instance;
// });
// const checkRemainToken = async (address) => {
// const balance = await token.balanceOf.call(address)
// return { balance };
// };
// const transferToken = async (address1, address2, value) => {
// const receipt = await token.transfer.send(address1, address2, value, { from: address1, gas: 1000000 })
// return receipt;
// };
// IMAGE UPLOADER SESSING
const upload = Multer({ dest: 'public/images/', limits: { files: 1 } });
const MAX_IMAGE_SIZE_BYTES = 10485760;
// app.get('/', (request, response, next) => {
// response.send('Hello! Welcome to Simple E-commerce Application!');
// });
app.listen(appPort, () => console.log(`Ecommerce server is running! it is listening on port ${appPort}...`));
// ADD PRODUCT
app.post('/products/add', upload.single('productImageInput'), async (request, response) => {
try {
const { productNameInput, productPriceInput, productQtyInput, productSeller, accountPassword, productImageInput } = request.body;
// const { file } = request;
const file = {
mimetype: 'image/png',
filename: '123123.png',
path: './image/12.png'
}
if (!['image/jpeg', 'image/png', 'image/gif'].includes(file.mimetype)) {
fs.unlinkSync(file.path);
return response.json({
success: false,
error: 'Please upload a file as jpeg, png, or gif.',
});
}
if (file.size > MAX_IMAGE_SIZE_BYTES) {
fs.unlinkSync(file.path);
return response.json({
success: false,
error: 'Please upload smaller file. (10MB)',
});
}
if (!productNameInput || !productPriceInput || !productQtyInput || !productSeller) {
return response.json({
success: false,
error: 'Please fill the form.',
});
}
if (!Number.isInteger(Number(productPriceInput))) {
return response.json({
success: false,
error: 'Plese enter product price in number',
});
}
if (!Number.isInteger(Number(productQtyInput))) {
return response.json({
success: false,
error: 'Plese enter product quantity in number',
});
}
const productImagePath = productImageInput
const unlocked = await unlockAccount(productSeller, '', 600);
if (!unlocked) {
return response.json({
success: false,
error: 'Please type correct account password.',
});
}
const dataResult = await addProduct(productNameInput, productPriceInput, productQtyInput, productImagePath, productSeller);
return response.json({
success: true,
data: { pid: dataResult.pid, transactionReceipt: dataResult.receipt },
error: null,
});
} catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
// BUY PRODUCT
app.post('/products/buy', async (request, response) => {
try {
const { pid, buyer, password, to } = request.body;
if (!pid) {
return response.json({
success: false,
error: 'Please select product ID.',
});
}
if (!buyer) {
return response.json({
success: false,
error: 'Please select address to be buyer.',
});
}
const unlocked = await unlockAccount(buyer, password);
if (!unlocked) {
return response.json({
success: false,
error: 'Please type correct account password.',
});
}
console.log('=======')
await buyProduct(pid, buyer, to);
return response.json({
success: true,
error: null,
});
} catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
app.post('/products/free', async (request, response) => {
try {
const { to } = request.body;
console.log('=======')
await freeProduct( to);
return response.json({
success: true,
error: null,
});
} catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
app.get('/balance/:wallet', async (request, response) => {
try {
let bla = await balance(request.params.wallet);
return response.json(bla)
}
catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
// GET PRODUCT
app.get('/products/:pid', async (request, response) => {
try {
const { pid } = request.params;
const result = await getProduct(pid);
return response.json({
success: true,
data: result,
error: null,
});
}
catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
app.get('/', async (request, response) => {
try {
coin = await checkRemainToken('0x197b6caFAf8507eF27926027b292343b7D8f76b8')
coin1 = await checkRemainToken('0xFa9e1d7225abf4c255E149301c8CafC8EFA80891')
console.log(coin.balance)
console.log(coin1.balance)
console.log(Web3.utils.BN(coin.balance).toString())
console.log(Web3.utils.BN(coin1.balance).toString())
const result = await getAllProducts();
return response.json({
success: true,
data: result,
});
} catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
app.get('/accounts', async (request, response) => {
try {
const result = await getAccounts();
return response.json({
success: true,
data: result,
});
} catch (error) {
return response.json({
success: false,
error: error.message,
});
}
});
app.get('/getAllProducts', async (request, response, next) => {
const accounts = await getAccounts();
const products = await Promise.all((await getAllProducts()).map(pid => getProduct(pid)));
for( product of products) {
product.price = Web3.utils.BN(product.price).toString()
product.quantity = Web3.utils.BN(product.quantity).toString()
}
return response.json({ products });
});
app.get('/xx',async (request, response) => {
console.log(123)
const accounts = await getProduct(1583475447358);
return response.json({ accounts });
});
// app.post('/products/buy', (request, response) => {
// response.send('Buy a product');
// });