Skip to content

Commit

Permalink
Merge pull request #20 from toussam/product
Browse files Browse the repository at this point in the history
Product
  • Loading branch information
Justkant committed Oct 28, 2015
2 parents 9d64ea0 + cc2a70a commit 558f162
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/functions/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export users from './users';
export products from './products';
//export product from './product';
73 changes: 70 additions & 3 deletions api/functions/products.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export User from './User';
export Product from './Product';
//export Product from './Product';
export Email from './Email';
55 changes: 55 additions & 0 deletions src/containers/Product/Product.styl
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 558f162

Please sign in to comment.