Skip to content

Commit

Permalink
add order api
Browse files Browse the repository at this point in the history
  • Loading branch information
koretskiyav committed Dec 8, 2020
1 parent 352129d commit 114a2ef
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions simple_api/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ router.get('/users', (req, res, next) => {
reply(res, users);
});

const min = (m) => `you ordered for $${m}, but the min order amount is $50`;
const max = (m) => `you ordered for $${m}, but the max order amount is $200`;

router.post('/order', function (req, res, next) {
try {
const total = req.body
.map((it) => products.find((p) => p.id === it.id).price * it.amount)
.reduce((acc, next) => acc + next, 0);

if (total < 50) return reply(res, min(total), 3000, 400);
if (total > 200) return reply(res, max(total), 3000, 400);
return reply(res, 'ok', 3000);
} catch {
return reply(res, 'wrong data', 1000, 400);
}
});

module.exports = router;

0 comments on commit 114a2ef

Please sign in to comment.