-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from philippebeck/dev
Release 0.2.0
- Loading branch information
Showing
39 changed files
with
2,140 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ api/.env | |
api/node_modules/ | ||
dist/ | ||
node_modules/ | ||
public/img/users/ | ||
public/img/ |
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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
"use strict"; | ||
|
||
const formidable = require("formidable"); | ||
const fs = require("fs"); | ||
const nem = require("nemjs"); | ||
const accents = require("remove-accents"); | ||
|
||
const ArticleModel = require("../model/ArticleModel"); | ||
|
||
require("dotenv").config(); | ||
|
||
const form = formidable({ | ||
uploadDir: process.env.IMG_URL + "articles/", | ||
keepExtensions: true | ||
}); | ||
|
||
/** | ||
* GET IMAGE NAME | ||
* @param {string} name | ||
*/ | ||
exports.getImgName = (name) => { | ||
|
||
return accents.remove(name).replace(" ", "-").toLowerCase() + "-" + Date.now() + "." + process.env.IMG_EXT; | ||
} | ||
|
||
/** | ||
* GET ARTICLE | ||
* @param {string} name | ||
* @param {string} description | ||
* @param {string} image | ||
* @param {number} price | ||
* @returns | ||
*/ | ||
exports.getArticle = (name, description, image, price) => { | ||
|
||
return { | ||
name: name, | ||
description: description, | ||
image: image, | ||
price: price | ||
} | ||
} | ||
|
||
/** | ||
* LIST ARTICLES | ||
* @param {object} req | ||
* @param {object} res | ||
*/ | ||
exports.listArticles = (req, res) => { | ||
ArticleModel | ||
.find() | ||
.then((articles) => res.status(200).json(articles)) | ||
.catch((error) => res.status(400).json({ error })); | ||
}; | ||
|
||
//! ****************************** CRUD ****************************** | ||
|
||
/** | ||
* CREATE ARTICLE | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
*/ | ||
exports.createArticle = (req, res, next) => { | ||
form.parse(req, (err, fields, files) => { | ||
|
||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
|
||
let image = this.getImgName(fields.name); | ||
nem.createImage(files.image.newFilename, image); | ||
|
||
let article = new ArticleModel(this.getArticle(fields.name, fields.description, image, fields.price)); | ||
|
||
fs.unlink(process.env.IMG_URL + "articles/" + files.image.newFilename, () => { | ||
article | ||
.save() | ||
.then(() => res.status(201).json({ message: process.env.ARTICLE_CREATED })) | ||
.catch((error) => res.status(400).json({ error })); | ||
}); | ||
}) | ||
}; | ||
|
||
/** | ||
* UPDATE ARTICLE | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
*/ | ||
exports.updateArticle = (req, res, next) => { | ||
form.parse(req, (err, fields) => { | ||
|
||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
|
||
let image = fields.image; | ||
|
||
if (Object.keys(files).length !== 0) { | ||
image = this.getImgName(fields.name); | ||
nem.createImage(files.image.newFilename, image); | ||
|
||
ArticleModel | ||
.findOne({ _id: req.params.id }) | ||
.then((article) => | ||
fs.unlink(process.env.IMG_URL + "articles/" + article.image, () => { | ||
fs.unlink(process.env.IMG_URL + "articles/" + files.image.newFilename, () => { | ||
console.log("Image ok !"); | ||
}) | ||
}) | ||
) | ||
} | ||
|
||
let article = this.getArticle(fields.name, fields.description, image, fields.price); | ||
|
||
ArticleModel | ||
.updateOne({ _id: req.params.id }, { ...article, _id: req.params.id }) | ||
.then(() => res.status(200).json({ message: process.env.ARTICLE_UPDATED })) | ||
.catch((error) => res.status(400).json({ error })); | ||
}) | ||
}; | ||
|
||
/** | ||
* DELETE ARTICLE | ||
* @param {object} req | ||
* @param {object} res | ||
*/ | ||
exports.deleteArticle = (req, res) => { | ||
ArticleModel | ||
.findOne({ _id: req.params.id }) | ||
.then(article => { | ||
fs.unlink(process.env.IMG_URL + "articles/" + article.image, () => { | ||
ArticleModel | ||
.deleteOne({ _id: req.params.id }) | ||
.then(() => res.status(200).json({ message: process.env.ARTICLE_DELETED })) | ||
.catch((error) => res.status(400).json({ error })); | ||
}); | ||
}) | ||
.catch(error => res.status(500).json({ error })); | ||
}; |
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,79 @@ | ||
"use strict"; | ||
|
||
const formidable = require("formidable"); | ||
|
||
const CommentModel = require("../model/CommentModel"); | ||
|
||
require("dotenv").config(); | ||
|
||
const form = formidable(); | ||
|
||
/** | ||
* LIST COMMENTS | ||
* @param {object} req | ||
* @param {object} res | ||
*/ | ||
exports.listComments = (req, res) => { | ||
CommentModel | ||
.find() | ||
.then((comments) => res.status(200).json(comments)) | ||
.catch((error) => res.status(400).json({ error })); | ||
}; | ||
|
||
//! ****************************** CRUD ****************************** | ||
|
||
/** | ||
* CREATE COMMENT | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
*/ | ||
exports.createComment = (req, res, next) => { | ||
form.parse(req, (err, fields) => { | ||
|
||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
|
||
let comment = new CommentModel(fields); | ||
|
||
comment | ||
.save() | ||
.then(() => res.status(201).json({ message: process.env.COMMENT_CREATED })) | ||
.catch((error) => res.status(400).json({ error })); | ||
}) | ||
}; | ||
|
||
/** | ||
* UPDATE COMMENT | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {function} next | ||
*/ | ||
exports.updateComment = (req, res, next) => { | ||
form.parse(req, (err, fields) => { | ||
|
||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
|
||
CommentModel | ||
.updateOne({ _id: req.params.id }, { ...fields, _id: req.params.id }) | ||
.then(() => res.status(200).json({ message: process.env.COMMENT_UPDATED })) | ||
.catch((error) => res.status(400).json({ error })); | ||
}) | ||
}; | ||
|
||
/** | ||
* DELETE COMMENT | ||
* @param {object} req | ||
* @param {object} res | ||
*/ | ||
exports.deleteComment = (req, res) => { | ||
CommentModel | ||
.deleteOne({ _id: req.params.id }) | ||
.then(() => res.status(200).json({ message: process.env.COMMENT_DELETED })) | ||
.catch((error) => res.status(400).json({ error })) | ||
}; |
Oops, something went wrong.