Skip to content

Commit

Permalink
updated articles controller, fix bug at comments controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucky-victory committed May 29, 2022
1 parent e725a43 commit 220b9b8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
12 changes: 6 additions & 6 deletions config/db.init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require('dotenv').config();
const connectDB=require("./db.config");
connectDB();
const Users= require("../models/users");
const Articles= require("../models/articles");
const Comments= require("../models/comments");
const Replies= require("../models/replies");
const ArticleTags = require('../models/article-tags');
const Tags = require('../models/tags');
const Users= require("../models/users.model");
const Articles= require("../models/articles.model");
const Comments= require("../models/comments.model");
const Replies= require("../models/replies.model");
const ArticleTags = require('../models/article-tags.model');
const Tags = require('../models/tags.model');

async function initializeDB(){

Expand Down
3 changes: 2 additions & 1 deletion constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const SORT_LISTS={
}

const ACCEPTABLE_SORT_NAMES=['latest','popular','short','long'];

const MAX_ARTICLE_TAGS=5;
module.exports={
IS_PROD,
MAX_ARTICLE_TAGS,
IS_DEV,
ARTICLES_SQL_QUERY,
ACCEPTABLE_SORT_NAMES,
Expand Down
8 changes: 5 additions & 3 deletions controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const Articles=require('../models/articles.model');
const Comments=require('../models/comments.model');
const asyncHandler=require('express-async-handler');
const {Nester,GenerateSlug,CalculateReadTime, StringToArray, NullOrUndefined, NotNullOrUndefined, isEmpty, AddPropsToObject, StringArrayToObjectArray,MergeArrays,GetIdOfDuplicateTags,RemoveDuplicateTags, GetLocalTime}=require("../helpers/utils");
const {Nester,GenerateSlug,CalculateReadTime, StringToArray, NullOrUndefined, NotNullOrUndefined, isEmpty, AddPropsToObject, StringArrayToObjectArray,MergeArrays,GetIdOfDuplicateTags,RemoveDuplicateTags, GetLocalTime, isLongerThan}=require("../helpers/utils");
const {Converter}=require("showdown");
const converter=new Converter();
const {encode,decode}=require("html-entities");
const { ARTICLES_SQL_QUERY,ACCEPTABLE_SORT_NAMES,SORT_LISTS} = require('../constants');
const { ARTICLES_SQL_QUERY,ACCEPTABLE_SORT_NAMES,SORT_LISTS, MAX_ARTICLE_TAGS} = require('../constants');
const ArticleTags = require('../models/article-tags.model');
const Tags = require('../models/tags.model');

Expand Down Expand Up @@ -109,8 +109,10 @@ const authorId=req.userId;
const totalWords= String(NotNullOrUndefined(title) + NotNullOrUndefined(content)) ||'';
const {readTime}=CalculateReadTime(totalWords)
const publishedAt=status=='published'? createdAt : null;
const newArticle= {createdAt,publishedAt,title,content,heroImage,slug,category,authorId,published,modifiedAt:createdAt,views:0,readTime,intro};
const newArticle= {createdAt,publishedAt,title,content,heroImage,slug,category,authorId,modifiedAt:createdAt,views:0,readTime,intro,status};

// If Tags are more than {MAX_ARTICLE TAGS}, remove the extras
tags=isLongerThan(tags,MAX_ARTICLE_TAGS) ? MAX_ARTICLE_TAGS : (tags)
// try getting tags from database to see if they exist
const tagsExist=await Tags.find({getAttributes:["id","text"],where:`text IN("${tags.join('","')}")`});
let remainingTags=tags;
Expand Down
4 changes: 2 additions & 2 deletions controllers/comments.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const addNewComment = async (req, res) => {
message: "No 'postId' provided"
});
const { userId } = req;
const { text, status = 'pending', postId } = req.body;
const { text, status = 'pending'} = req.body;
const createdAt = GetLocalTime();
await Comments.create({
text,
Expand All @@ -54,5 +54,5 @@ const addNewComment = async (req, res) => {


module.exports = {
getApprovedComments
getApprovedComments,addNewComment
}
37 changes: 37 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ RemoveDuplicateTags(prevTags, newTags) {
}
return [];
},
/**
* Merge two arrays
* @param {any[]} arr - array to be merged
* @param {any[]} arr2 - array to be merged
* @returns
*/
MergeArrays(arr=[], arr2=[]) {
const newArr = [];
newArr.push(...arr, ...arr2)
Expand All @@ -268,6 +274,37 @@ const currentHour=new Date().getHours();
const localDateTimeInMilliseconds=new Date().setHours(currentHour - timeZoneOffsetInHours);
const localTime=new Date(localDateTimeInMilliseconds).toISOString();
return localTime;
},
/**
*
* @param {*} val - the value to be transformed to an array
* @returns {any[]}
*/
toArray(val){
if(!Utils.NullOrUndefined(val) && !Array.isArray(val)){
val=[val]
return val
}

},
/**
*
* @param {any[]} arr - an array to be checked
* @param {number} length - the length to measure the array's length
* @returns {boolean}
*/
isLongerThan(arr,length){
if(!Array.isArray(arr)) return;
return arr.length > length;
},
/**
*
* @param {any[]} arr - the array to be shortened
* @param {number} size - the size to shorten the array to
*/
shortenArray(arr,size){
if(!Array.isArray(arr)) return;
return arr.splice(0)
}
}

Expand Down

0 comments on commit 220b9b8

Please sign in to comment.