-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Justkant/product-enhancements
Product enhancements
- Loading branch information
Showing
30 changed files
with
664 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,80 @@ | ||
import { Product } from '../models'; | ||
import { shuffle } from '../utils/functions'; | ||
|
||
function getProducts(req, res) { | ||
res.json([{ | ||
title: 'Title', | ||
description: 'Nike shoes', | ||
imageUrl: 'product.jpg', | ||
price: '125$' | ||
}]); | ||
Product.orderBy('-createdAt').run().then((result) => { | ||
res.json(result); | ||
}); | ||
} | ||
|
||
function getProduct(req, res) { | ||
/* pourquoi product pop en orange ? */ | ||
res.json(req.product.getPublic()); | ||
Product.get(req.params.id).run().then((product) => { | ||
res.json(product); | ||
}, (error) => { | ||
res.status(404).json({msg: 'Product not found'}); | ||
}); | ||
} | ||
|
||
function addProduct(req, res) { | ||
|
||
const product = new Product({ | ||
title: req.body.title, | ||
description: req.body.description, | ||
imageUrl: req.body.imageUrl, | ||
price: req.body.price | ||
price: 0 | ||
}); | ||
|
||
product.save().then(() => { | ||
res.json(product); | ||
}, (error) => { | ||
console.error(error); | ||
res.status(500).json({msg: 'Contact an administrator', err: error}); | ||
}); | ||
}, (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()); | ||
Product.get(req.params.id).run().then((product) => { | ||
product.merge(req.body).save().then((result) => { | ||
res.json(product); | ||
}, (error) => { | ||
console.error(error); | ||
res.status(400).json({msg: 'Something went wrong', err: error}); | ||
}); | ||
}, (error) => { | ||
res.status(404).json({msg: 'Product not found'}); | ||
}); | ||
} | ||
|
||
function deleteProduct(req, res) { | ||
req.product.delete().then(() => { | ||
res.json({msg: 'Account deleted'}); | ||
Product.get(req.params.id).run().then((product) => { | ||
product.delete().then(() => { | ||
res.json({msg: 'Product deleted'}); | ||
}, (error) => { | ||
console.error(error); | ||
res.status(500).json({msg: 'Contact an administrator', err: error}); | ||
}); | ||
}, (error) => { | ||
console.error(error); | ||
res.status(500).json({msg: 'Contact an administrator', err: error}); | ||
res.status(404).json({msg: 'Product not found'}); | ||
}); | ||
} | ||
|
||
function search(req, res) { | ||
|
||
} | ||
|
||
function getMarket(req, res) { | ||
Product.run().then((result) => { | ||
res.json(shuffle(result)); | ||
}); | ||
} | ||
|
||
const products = { | ||
getProducts: getProducts, | ||
getProduct: getProduct, | ||
addProduct: addProduct, | ||
updateProduct: updateProduct, | ||
deleteProduct: deleteProduct, | ||
search: search | ||
search: search, | ||
getMarket: getMarket | ||
}; | ||
|
||
export default products; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,10 +79,41 @@ function logout(req, res) { | |
res.json(null); | ||
} | ||
|
||
/** | ||
* @api {get} /users Request All Users | ||
* @apiName GetUsers | ||
* @apiGroup User | ||
*/ | ||
function getUsers(req, res) { | ||
res.json([{username: 'kant'}]); | ||
User.orderBy('-createdAt').run().then((result) => { | ||
res.json(result); | ||
}); | ||
} | ||
|
||
/** | ||
* @api {get} /users/:id Request User Information | ||
* @apiName GetUser | ||
* @apiGroup User | ||
* | ||
* @apiParam {Number} id Users unique ID. | ||
* | ||
* @apiSuccess {String} username The users name. | ||
* @apiSuccess {String} email The users email. | ||
* @apiSuccess {String} token The users token. | ||
* @apiSuccess {String} pictureUrl The users picture url. | ||
* @apiSuccess {Boolean} admin The users right. | ||
* @apiSuccess {Date} createdAt The users creation date. | ||
* | ||
* @apiSuccessExample Example data on success: | ||
* { | ||
* username: 'Kant', | ||
* email: '[email protected]', | ||
* token: 'IOEJVofz@fohinsmov24azd5niermogunqeprofinzqoe8297', | ||
* pictureUrl: 'uploads/picture-94305067460.png', | ||
* admin: true, | ||
* createdAt: Wed Oct 21 2015 14:33:53 GMT+00:00 | ||
* } | ||
*/ | ||
function getUser(req, res) { | ||
res.json(req.user.getPublic()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export function shuffle(array) { | ||
var m = array.length, t, i; | ||
|
||
// While there remain elements to shuffle… | ||
while (m) { | ||
|
||
// Pick a remaining element… | ||
i = Math.floor(Math.random() * m--); | ||
|
||
// And swap it with the current element. | ||
t = array[m]; | ||
array[m] = array[i]; | ||
array[i] = t; | ||
} | ||
|
||
return array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "WhatAShop API", | ||
"version": "0.1.1", | ||
"description": "API description for WhatAShop" | ||
} |
Oops, something went wrong.