Skip to content

Commit

Permalink
Merge pull request #15 from philippebeck/dev
Browse files Browse the repository at this point in the history
Release 0.2.6-alpha
  • Loading branch information
philippebeck authored Dec 19, 2023
2 parents 08978e0 + df4edc7 commit c3bae36
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Available API Files :
## Documentation

Available Documentation :
- [**API**](https://github.com/philippebeck/nens/tree/master/swagger.yaml)
- [**API**](https://github.com/philippebeck/nens/blob/main/swagger.yaml)

Available Readme :
- [**NemJS**](https://github.com/philippebeck/nemjs)
23 changes: 16 additions & 7 deletions controller/ArticleCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ exports.createArticle = (req, res, next) => {
const { image } = files;

const IMG = nem.getName(name) + "." + IMG_EXT;
if (image && image.newFilename) this.setImage(image.newFilename, IMG);

this.checkArticleData(name, text, alt, cat, res);

Article.findAll()
.then((articles) => {
for (const article of articles) this.checkArticleUnique(name, text, article, res);
if (image && image.newFilename) this.setImage(image.newFilename, IMG);
for (const article of articles) {
this.checkArticleUnique(name, text, article, res);
}

const article = { ...fields, image: IMG };

Expand Down Expand Up @@ -165,25 +167,32 @@ exports.createArticle = (req, res, next) => {
*/
exports.updateArticle = (req, res, next) => {
const { ARTICLE_UPDATED, ARTICLE_NOT_UPDATED } = process.env;
const ID = parseInt(req.params.id, 10);

form.parse(req, (err, fields, files) => {
if (err) { next(err); return }

const { name, text, alt, cat } = fields;
const { image } = files;

const ID = parseInt(req.params.id, 10);
const IMG = nem.getName(name) + "." + IMG_EXT;

this.checkArticleData(name, text, alt, cat, res);

Article.findAll()
.then((articles) => {
let img;

if (image && image.newFilename) {
img = nem.getName(name) + "." + IMG_EXT;
this.setImage(image.newFilename, img);

} else {
img = articles.find(article => article.id === ID)?.image;
}

articles.filter(article => article.id !== ID).forEach(article =>
this.checkArticleUnique(name, text, article, res));

if (image && image.newFilename) this.setImage(image.newFilename, IMG);
const article = { ...fields, image: IMG };
const article = { ...fields, image: img };

Article.update(article, { where: { id: ID }})
.then(() => {
Expand Down
47 changes: 25 additions & 22 deletions controller/ImageCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,28 @@ exports.createImage = (req, res, next) => {
Gallery.findOne({ where: { id: galleryId }})
.then((gallery) => {

const IMG = `${nem.getName(gallery.name)}-${index}.${IMG_EXT}`;
if (image && image.newFilename) this.setImage(image.newFilename, IMG);

Image.findAll({ where: { galleryId: galleryId }})
.then((images) => {
let index = images.length + 1;
if (index < 10) index = `0${index}`;

const NAME = `${nem.getName(gallery.name)}-${index}.${IMG_EXT}`;
const img = { ...fields, name: NAME };

if (image && image.newFilename) this.setImage(image.newFilename, NAME);

Image.create(img)
.then(() => {
if (image && image.newFilename) {
fs.unlink(GALLERIES_IMG + image.newFilename, () => {
res.status(201).json({ message: IMAGE_CREATED })
})
}
})
.catch(() => res.status(400).json({ message: IMAGE_NOT_CREATED }));
})
.catch(() => res.status(404).json({ message: IMAGES_NOT_FOUND }));
.then((images) => {

let index = images.length + 1;
if (index < 10) index = `0${index}`;

const img = { ...fields, name: IMG };

Image.create(img)
.then(() => {
if (image && image.newFilename) {
fs.unlink(GALLERIES_IMG + image.newFilename, () => {
res.status(201).json({ message: IMAGE_CREATED })
})
}
})
.catch(() => res.status(400).json({ message: IMAGE_NOT_CREATED }));
})
.catch(() => res.status(404).json({ message: IMAGES_NOT_FOUND }));
})
.catch(() => res.status(404).json({ message: GALLERY_NOT_FOUND }));
})
Expand All @@ -138,11 +139,13 @@ exports.updateImage = (req, res, next) => {

const { name, description } = fields;
const { image } = files;
const img = { ...fields };

this.checkImageData(description, res);
if (image && image.newFilename) this.setImage(image.newFilename, name);

this.checkImageData(description, res);

const img = { ...fields };

Image.update(img, { where: { id: ID }})
.then(() => {
if (image && image.newFilename) {
Expand Down
21 changes: 14 additions & 7 deletions controller/ProductCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ exports.createProduct = (req, res, next) => {
const { alt, cat, description, name, price } = fields;
const { image } = files;

const IMG = nem.getName(fields.name) + "." + IMG_EXT;
if (image && image.newFilename) this.setImage(image.newFilename, IMG);

this.checkProductData(name, description, alt, price, cat, res);

Product.findAll()
Expand All @@ -135,11 +138,8 @@ exports.createProduct = (req, res, next) => {
this.checkProductUnique(name, description, product, res)
}

const IMG = nem.getName(fields.name) + "." + IMG_EXT;
const product = { ...fields, image: IMG };

if (image && image.newFilename) this.setImage(image.newFilename, IMG);

Product.create(product)
.then(() => {
if (image && image.newFilename) {
Expand Down Expand Up @@ -177,13 +177,20 @@ exports.updateProduct = (req, res, next) => {

Product.findAll()
.then((products) => {
let img;

if (image && image.newFilename) {
img = nem.getName(name) + "." + IMG_EXT;
this.setImage(image.newFilename, img);

} else {
img = products.find(product => product.id === ID)?.image;
}

products.filter(product => product.id !== ID).forEach(product =>
this.checkProductUnique(name, description, product, res));

const IMG = nem.getName(name) + "." + IMG_EXT;
const product = { ...fields, image: IMG };

if (image && image.newFilename) this.setImage(image.newFilename, IMG);
const product = { ...fields, image: img };

Product.update(product, { where: { id: ID }})
.then(() => {
Expand Down
27 changes: 17 additions & 10 deletions controller/UserCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ exports.createUser = (req, res, next) => {
const { image } = files;

const IMG = nem.getName(name) + "." + IMG_EXT;
if (image && image.newFilename) this.setImage(image.newFilename, IMG);

this.checkUserData(name, email, role, res);
this.checkUserPass(pass, res);

User.findAll()
.then((users) => {
for (const user of users) this.checkUserUnique(name, email, user, res);
if (image && image.newFilename) this.setImage(image.newFilename, IMG);
for (const user of users) {
this.checkUserUnique(name, email, user, res);
}

bcrypt.hash(pass, 10)
.then((hash) => {
Expand Down Expand Up @@ -218,35 +220,40 @@ exports.readUser = (req, res) => {
*/
exports.updateUser = (req, res, next) => {
const { USER_NOT_UPDATED, USER_UPDATED } = process.env;
const ID = parseInt(req.params.id, 10);

form.parse(req, (err, fields, files) => {
if (err) { next(err); return }

const { name, email, role, pass } = fields;
const { image } = files;

const ID = parseInt(req.params.id, 10);
const IMG = nem.getName(name) + "." + IMG_EXT;

let user;
this.checkUserData(name, email, role, res);

User.findAll()
.then((users) => {
let user, img;

if (image && image.newFilename) {
img = nem.getName(name) + "." + IMG_EXT;
this.setImage(image.newFilename, img);

} else {
img = users.find(user => user.id === ID)?.image;
}

users.filter(user => user.id !== ID).forEach(user =>
this.checkUserUnique(name, email, user, res));

if (image && image.newFilename) this.setImage(image.newFilename, IMG);

if (pass) {
this.checkUserPass(pass, res);

bcrypt.hash(pass, 10)
.then((hash) => { user = { ...fields, image: IMG, pass: hash }})
.then((hash) => { user = { ...fields, image: img, pass: hash } })
.catch(() => res.status(400).json({ message: USER_NOT_PASS }));

} else {
user = { ...fields, image: IMG }
user = { ...fields, image: img };
}

User.update(user, { where: { id: ID }})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nens",
"version": "0.2.5-alpha",
"version": "0.2.6-alpha",
"description": "API with Node, Express, NemJS & SQL",
"keywords": [
"api",
Expand Down

0 comments on commit c3bae36

Please sign in to comment.