diff --git a/api/functions/index.js b/api/functions/index.js index 43f1094..8132540 100644 --- a/api/functions/index.js +++ b/api/functions/index.js @@ -1,2 +1,3 @@ export users from './users'; export products from './products'; +//export product from './product'; diff --git a/api/functions/products.js b/api/functions/products.js index ec94ede..9f21f48 100644 --- a/api/functions/products.js +++ b/api/functions/products.js @@ -1,23 +1,90 @@ import { Product } from '../models'; function getProducts(req, res) { - + res.json([{ + title: 'Title', + description: 'Nike shoes', + imageUrl: 'product.jpg', + price: '125$' + }]); } function getProduct(req, res) { - + /* pourquoi product pop en orange ? */ + res.json(req.product.getPublic()); } function addProduct(req, res) { + const product = new Product({ + title: req.body.title, + description: req.body.description, + imageUrl: req.body.imageUrl, + price: req.body.price + }); + + product.save().then(() => { + res.json(product); + }, (error) => { + console.error(error); + res.status(500).json({msg: 'Contact an administrator', err: error}); + }); } function updateProduct(req, res) { + const product = {}; + const promises = []; + + if (req.body.title && req.body.title != req.product.title) { + promises.push(new Promise((resolve, reject) => { + product.title = req.body.title; + resolve(); + }, (error) => { + console.error(error); + res.status(500).json({msg: 'Contact an administrator', err: error}); + reject(); + })); + } + if (req.body.price && req.body.price !== req.product.price) { + promises.push(new Promise((resolve) => { + product.price = req.body.price; + resolve(); + }, (error) => { + console.error(error); + res.status(500).json({msg: 'Contact an administrator', err: error}); + reject(); + })); + } + + if (req.body.description && req.body.description !== req.product.description) { + promises.push(new Promise((resolve) => { + product.description = req.body.description; + resolve(); + }, (error) => { + console.error(error); + res.status(500).json({msg: 'Contact an administrator', err: error}); + reject(); + })); + } + + Promise.all(promises).then(() => { + req.product.merge(product).save().then((result) => { + res.json(req.product.getPublic()); + }, (error) => { + console.error(error); + res.status(400).json({msg: 'Something went wrong', err: error}); + }); + }); } function deleteProduct(req, res) { - + req.product.delete().then(() => { + res.json({msg: 'Account deleted'}); + }, (error) => { + console.error(error); + res.status(500).json({msg: 'Contact an administrator', err: error}); + }); } function search(req, res) { diff --git a/api/models/index.js b/api/models/index.js index 6ab6ea6..c542341 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,3 +1,3 @@ export User from './User'; -export Product from './Product'; +//export Product from './Product'; export Email from './Email'; diff --git a/src/containers/Product/Product.styl b/src/containers/Product/Product.styl new file mode 100644 index 0000000..bf6c847 --- /dev/null +++ b/src/containers/Product/Product.styl @@ -0,0 +1,55 @@ +.productContainer { + padding: 20px; + display: flex; + flex-direction: column; + overflow: auto; +} + +.productContainer img { + padding: 20px; + max-width: 200px; + cursor: pointer; +} + +.mainInformations { + display: flex; + padding: 20px 0; + + &:not(:last-child) { + border-bottom: 1px solid #E7E7EC; + } +} + +.mainInformationsLeft { + display: flex; + flex-direction: column; + flex-basis: 100%; + padding: 0 20px; +} + +.mainInformationsLeft h3 { + color: #3585b5; + font-weight: 300; + margin: 0; + margin-bottom: 10px; +} + +.mainInformationsRight { + display: flex; + flex-direction: column; + flex-basis: 70%; + padding-left: 10%; +} + +.mainInformationsRight p { + font-size: 12px; + margin-bottom: 10px; + color: #999; +} + +.additionalInformations { + padding: 20px; + font-weight: 300; + font-size: 12px; + line-height: 24px; +}