-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
143 lines (139 loc) · 4.61 KB
/
models.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
const db = require('./db');
module.exports.list = (page, count, callback) => {
let start = ((Number(page) - 1) * Number(count)) + 1;
let end = start + Number(count);
db.query(`SELECT * FROM products WHERE id >= ${start} AND id < ${end}`)
.then(res => {
callback(res.rows);
})
.catch(err => console.error(err));
};
module.exports.info = (product_id, callback) => {
let result = {};
db.query(`SELECT * FROM products WHERE id = ${product_id}`)
.then(res => {
result = res.rows[0];
return db.query(`SELECT feature, value FROM features WHERE product_id = ${product_id}`)
})
.then(res => {
result.features = [];
res.rows.forEach((row) => {
let feature = {
feature: row.feature,
value: row.value
};
result.features.push(feature);
});
callback(result);
})
.catch(err => console.error(err));
};
module.exports.styles = (product_id, callback) => {
let result = { product_id, results: [] };
// db.query(`SELECT * FROM styles WHERE product_id = ${product_id}`)
// .then(res => {
// let promises = res.rows.map(row => {
// let style = {
// style_id: row.id,
// name: row.name,
// original_price: row.default_price,
// sale_price: row.sale_price,
// 'default?': row.default_style,
// photos: [],
// skus: {}
// };
// return db.query(`SELECT thumbnail_url, url FROM photos WHERE style_id = ${row.id}`)
// .then(res => {
// res.rows.forEach(photoRow => {
// style.photos.push({
// thumbnail_url: photoRow.thumbnail_url,
// url: photoRow.url
// });
// })
// return db.query(`SELECT size, quantity FROM skus WHERE style_id = ${row.id}`)
// })
// .then(res => {
// res.rows.forEach(skuRow => {
// style.skus[skuRow.size] = skuRow.quantity;
// });
// return style;
// })
// })
// return Promise.all(promises);
// })
// .then(res => {
// result.results = res;
// console.log('how is this fast wtf???');
// callback(result);
// })
// .catch(err => console.error(err));
db.query(`SELECT styles.*, skus.size, skus.quantity, photos.thumbnail_url, photos.url FROM styles LEFT JOIN skus ON skus.style_id = styles.id LEFT JOIN photos ON photos.style_id = styles.id WHERE styles.product_id = ${product_id};`)
.then(res => {
let style = {
style_id: undefined,
name: undefined,
original_price: undefined,
sale_price: undefined,
'default?': undefined,
photos: [],
skus: {}
};
let urls = [];
let thumbs = [];
for (let i = 0; i < res.rows.length; ++i) {
if (res.rows[i].id !== style.style_id && style.style_id !== undefined) {
for (let j = 0; j < thumbs.length; ++j) {
style.photos.push({
thumbnail_url: thumbs[j],
url: urls[j]
});
}
result.results.push(style);
style = {
style_id: undefined,
name: undefined,
original_price: undefined,
sale_price: undefined,
'default?': undefined,
photos: [],
skus: {}
};
urls, thumbs = [];
}
if (style.style_id === undefined) {
style.style_id = res.rows[i].id;
style.name = res.rows[i].name;
style.original_price = res.rows[i].default_price;
style.sale_price = res.rows[i].sale_price;
style['default?'] = res.rows[i].default_style;
}
style.skus[res.rows[i].size] = res.rows[i].quantity;
if (!thumbs.includes(res.rows[i].thumbnail_url)) {
thumbs.push(res.rows[i].thumbnail_url);
}
if (!urls.includes(res.rows[i].url)) {
urls.push(res.rows[i].url);
}
if (i === res.rows.length - 1) {
for (let j = 0; j < thumbs.length; ++j) {
style.photos.push({
thumbnail_url: thumbs[j],
url: urls[j]
});
}
result.results.push(style);
}
}
callback(result);
})
.catch(err => console.error(err));
};
module.exports.related = (product_id, callback) => {
let result = [];
db.query(`SELECT * FROM related WHERE current_id = ${product_id}`)
.then(res => {
res.rows.forEach(row => result.push(row.related_id))
callback(result);
})
.catch(err => console.error(err));
};