Skip to content

Instantly share code, notes, and snippets.

@Hc747
Created August 16, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hc747/32f1e878ad1473ca7435328c22e319ce to your computer and use it in GitHub Desktop.
Save Hc747/32f1e878ad1473ca7435328c22e319ce to your computer and use it in GitHub Desktop.
const express = require('express');
const router = express.Router();
//TODO: put models into own package
class IdentifiableModel {
//TODO: proptypes or typescript
constructor(id) {
this.id = id;
}
}
class Category extends IdentifiableModel {
//TODO: prop types or typescript
constructor(id, name, description, products) {
super(id);
this.name = name;
this.description = description;
this.products = products;
}
}
const store = {
categories: [
new Category('663864f8-3e46-4192-b4cf-55f2918da53d', 'Shoes', 'See all available shoes.', []),
new Category('098233c7-c8b3-4b96-956c-8c6d538f8824', 'Pants', 'See all available pants.', []),
new Category('98c672b3-c30c-484c-8008-ff4a41055158', 'Dresses', 'See all available dresses.', []),
new Category('faee1ec0-ca29-49c0-a629-d1b8586d3761', 'Skirts', 'See all available skirts.', []),
new Category('42e04604-f3e7-4f71-9d58-1f6cfc153551', 'Shirts', 'See all available shirts.', []),
new Category('37fd652b-509d-4430-b730-974f308ccbe8', 'Socks', 'See all available socks.', []),
new Category('2a6d93c4-d920-4d3e-aaf5-4c42da992703', 'Suits', 'See all available suits.', []),
]
};
//return all categories
router.get('/categories', (req, res) => {
res.json({ categories: store.categories });
});
//return single category where category[:filter] === :value
router.get('/categories/:filter/:value', (req, res) => {
const category = store.categories.find(c => c[req.params.filter] === req.params.value);
res.json({ category: category || {} });
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment