Skip to content

Commit

Permalink
Merge pull request #4 from philippebeck/dev
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
philippebeck authored Jan 28, 2023
2 parents b8dc797 + 09486ac commit af3b992
Show file tree
Hide file tree
Showing 39 changed files with 2,140 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ api/.env
api/node_modules/
dist/
node_modules/
public/img/users/
public/img/
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,34 @@ Yarn : `yarn add vesan`

## Content

Available API Files :
- **UserModel** *(Schema MongoDB)*
- **UserCtrl** *(Controller)*
- **UserRoute** *(Route)*
Available API Files (UNDER CONSTRUCTION) :
- **ArticleModel, ReviewModel, PostModel, CommentModel, UserModel** *(Schemas MongoDB)*
- **ArticleCtrl, ReviewCtrl, PostCtrl, CommentCtrl, UserCtrl** *(Controllers)*
- **ArticleRoute, ReviewRoute, PostRoute, CommentRoute, UserRoute** *(Routes)*

Available Views :
Available Views (UNDER CONSTRUCTION) :
- **HomeView** *(Home)*
- **ShopView** *(Shop)*
- **ArticleView** *(Article of Shop)*
- **BlogView** *(Blog)*
- **PostView** *(Post of Blog)*
- **ContactView** *(Contact)*
- **LoginView** *(Login & Forgot Password)*
- **AdminView** *(Admin with the 2 Components)*
- **ErrorView** *(Error)*

Available Components :
Available Components (UNDER CONSTRUCTION) :
- **CreateArticle** *(Create Article)*
- **ListArticles** *(Update & Delete Article)*
- **CreateReview** *(Create Review)*
- **ListReviews** *(Update & Delete Review)*
- **CreatePost** *(Create Post)*
- **ListPosts** *(Update & Delete Post)*
- **CreateComment** *(Create Comment)*
- **ListComments** *(Update & Delete Comment)*
- **CreateUser** *(Create User)*
- **ListUsers** *(Update & Delete User)*

---

## Documentation
Expand Down
34 changes: 33 additions & 1 deletion api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
AUTH_ID = "Invalid Id !"
AUTH_REQ = "Invalid Request !"

GENERATE_LENGTH = 18
GENERATE_NUMBERS = true
GENERATE_SYMBOLS = true
GENERATE_STRICT = true

IMG_URL = "../public/img/"
IMG_EXT = "webp"
IMG_WIDTH = 100
IMG_HEIGHT = 100
IMG_FIT = "cover"
IMG_POSITION = "top"

JWT = "your-json-web-token"
JWT_DURATION = "72h"

Expand All @@ -22,7 +34,15 @@ PASS_MIN = 8
USER_EMAIL = "Invalid email !"
USER_PASS = "Invalid password !"

# APP
# VESAN

ARTICLE_CREATED = "Article created !"
ARTICLE_DELETED = "Article deleted !"
ARTICLE_UPDATED = "Article updated !"

COMMENT_CREATED = "Comment created !"
COMMENT_DELETED = "Comment deleted !"
COMMENT_UPDATED = "Comment updated !"

DB = "your-db-username-password-and-host"

Expand All @@ -31,6 +51,18 @@ MONGO_SUCCESS = "Connection to MongoDB successful!"

PORT = 3000

POST_CREATED = "Post created !"
POST_DELETED = "Post deleted !"
POST_UPDATED = "Post updated !"

REVIEW_CREATED = "Review created !"
REVIEW_DELETED = "Review deleted !"
REVIEW_UPDATED = "Review updated !"

ROUTE_ARTICLE = "/api/articles"
ROUTE_COMMENT = "/api/comments"
ROUTE_POST = "/api/posts"
ROUTE_REVIEW = "/api/reviews"
ROUTE_USER = "/api/users"

USER_CREATED = "User created !"
Expand Down
12 changes: 11 additions & 1 deletion api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ const helmet = require("helmet");
const sanitize = require("express-mongo-sanitize");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const userRoute = require("./route/UserRoute");

const articleRoute = require("./route/ArticleRoute");
const reviewRoute = require("./route/ReviewRoute");
const postRoute = require("./route/PostRoute");
const commentRoute = require("./route/CommentRoute");
const userRoute = require("./route/UserRoute");

require("dotenv").config();

/**
* MONGODB
*/
mongoose
.set('strictQuery', true)
.connect(process.env.DB, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log(process.env.MONGO_SUCCESS))
.catch(() => console.log(process.env.MONGO_FAIL));
Expand All @@ -40,6 +46,10 @@ const limiter = rateLimit({
/**
* ROUTES
*/
app.use(process.env.ROUTE_ARTICLE, articleRoute, limiter);
app.use(process.env.ROUTE_REVIEW, reviewRoute, limiter);
app.use(process.env.ROUTE_POST, postRoute, limiter);
app.use(process.env.ROUTE_COMMENT, commentRoute, limiter);
app.use(process.env.ROUTE_USER, userRoute, limiter);

module.exports = app;
143 changes: 143 additions & 0 deletions api/controller/ArticleCtrl.js
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 }));
};
79 changes: 79 additions & 0 deletions api/controller/CommentCtrl.js
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 }))
};
Loading

0 comments on commit af3b992

Please sign in to comment.