-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
144 lines (126 loc) · 3.82 KB
/
api.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
var bodyparser = require('body-parser');
var express = require('express');
var status = require('http-status');
var _ = require('underscore');
module.exports = function(wagner) {
var api = express.Router();
api.use(bodyparser.json());
api.put('/me/cart', wagner.invoke(function(User) {
return function(req, res) {
try {
var cart = req.body.data.cart;
} catch(e) {
return res.
status(status.BAD_REQUEST).
json({ error: 'No cart specified!' });
}
req.user.data.cart = cart;
req.user.save(function(error, user) {
if (error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
return res.json({ user: user });
});
};
}));
api.get('/me', function(req, res) {
if (!req.user) {
return res.
status(status.UNAUTHORIZED).
json({ error: 'Not logged in' });
}
req.user.populate({ path: 'data.cart.product', model: 'Product' },
handleOne.bind(null, 'user', res));
});
api.post('/checkout', wagner.invoke(function(User, Stripe) {
return function(req, res) {
if (!req.user) {
return res.
status(status.UNAUTHORIZED).
json({ error: 'Not logged in' });
}
// Populate the products in the user's cart
req.user.populate({ path: 'data.cart.product', model: 'Product' }, function(error, user) {
// Sum up the total price in USD
var totalCostUSD = 0;
_.each(user.data.cart, function(item) {
totalCostUSD += item.product.internal.approximatePriceUSD *
item.quantity;
});
// And create a charge in Stripe corresponding to the price
Stripe.charges.create(
{
// Stripe wants price in cents, so multiply by 100 and round up
amount: Math.ceil(totalCostUSD * 100),
currency: 'usd',
source: req.body.stripeToken,
description: 'Example charge'
},
function(err, charge) {
if (err && err.type === 'StripeCardError') {
return res.
status(status.BAD_REQUEST).
json({ error: err.toString() });
}
if (err) {
console.log(err);
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: err.toString() });
}
req.user.data.cart = [];
req.user.save(function() {
// Ignore any errors - if we failed to empty the user's
// cart, that's not necessarily a failure
// If successful, return the charge id
return res.json({ id: charge.id });
});
});
});
};
}));
api.get('/product/text/:query', wagner.invoke(function(Product){
return function(req, res){
Product.
find(
{ $text: { $search : req.params.query } },
{ $score: { $meta : 'textScore' } }).
sort({ score: { $meta : 'textScore' } }).
limit(10).
exec(handleMany.bind(null, 'products', res));
}
}));
return api;
};
function handleOne(property, res, error, result) {
if (error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
if (!result) {
return res.
status(status.NOT_FOUND).
json({ error: 'Not found' });
}
var json = {};
json[property] = result;
res.json(json);
}
function handleMany(property, res, error, result) {
if (error) {
return res.
status(status.INTERNAL_SERVER_ERROR).
json({ error: error.toString() });
}
if (!result) {
return res.
status(status.NOT_FOUND).
json({ error: 'Not found' });
}
var json = {};
json[property] = result;
res.json(json);
}