Skip to content

Commit

Permalink
Merge 130d5be into b5eac2c
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnickii committed Aug 30, 2019
2 parents b5eac2c + 130d5be commit c5f4ab7
Show file tree
Hide file tree
Showing 41 changed files with 1,227 additions and 328 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ typings/
#ignore DS_Store
.DS_Store

#ignore DS_Store
.DS_Store

# ignore package-lock
package-lock.json

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Reviewed by Hound CI](https://img.shields.io/badge/Reviewed%20by-Hound%20CI-blue.svg)](https://houndci.com)

Authors Haven - A Social platform for the creative at heart.
=======

## Vision

Expand Down
106 changes: 53 additions & 53 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
{
"name": "codepirates-ah-backend",
"scripts": {},
"env": {
"ADMIN_EMAIL": {
"required": true
},
"ADMIN_PASSWORD": {
"required": true
},
"API_VERSION": {
"required": true
},
"DATABASE_URL": {
"required": true
},
"DB_HOST": {
"required": true
},
"DB_NAME": {
"required": true
},
"DB_PASSWORD": {
"required": true
},
"DB_PORT": {
"required": true
},
"DB_USER": {
"required": true
},
"SECRET": {
"required": true
},
"STATE": {
"required": true
},
"USER_PASSWORD": {
"required": true
}
},
"formation": {
"web": {
"quantity": 1
}
},
"addons": [],
"buildpacks": [
{
"url": "heroku/nodejs"
}
],
"stack": "heroku-18"
}
"name": "codepirates-ah-backend",
"scripts": {},
"env": {
"ADMIN_EMAIL": {
"required": true
},
"ADMIN_PASSWORD": {
"required": true
},
"API_VERSION": {
"required": true
},
"DATABASE_URL": {
"required": true
},
"DB_HOST": {
"required": true
},
"DB_NAME": {
"required": true
},
"DB_PASSWORD": {
"required": true
},
"DB_PORT": {
"required": true
},
"DB_USER": {
"required": true
},
"SECRET": {
"required": true
},
"STATE": {
"required": true
},
"USER_PASSWORD": {
"required": true
}
},
"formation": {
"web": {
"quantity": 1
}
},
"addons": [],
"buildpacks": [
{
"url": "heroku/nodejs"
}
],
"stack": "heroku-18"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"undoseeds": "babel-node node_modules/.bin/sequelize db:seed:undo:all",
"migration": "babel-node node_modules/.bin/sequelize db:migrate",
"undomigration": "babel-node node_modules/.bin/sequelize db:migrate:undo:all",
"runmigrations": "npm run undoseeds && npm run migration && npm run seeds",
"runmigrations": " npm run undomigration && npm run migration && npm run seeds",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "export NODE_ENV=test && npm run undomigration && npm run migration && npm run seeds && nyc --reporter=html --reporter=text mocha ./test --exit --require @babel/register",
"dev": "nodemon --exec babel-node ./src/app.js"
Expand Down
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import routes from './routes/index';
import './config/cloudinary.config';
import { mock } from './middlewares/validators/socialLogin-mock';


const app = express();

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
Expand Down
14 changes: 10 additions & 4 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import NotificationServices from '../services/notification.service';
import cloudinaryHelper from '../helpers/cloudinaryHelper';
import OpenUrlHelper from '../helpers/share.article.helper';
import Util from '../helpers/util';

import statsService from '../services/db.service';

const { notifyViaEmailAndPush } = NotificationServices;
const util = new Util();
Expand Down Expand Up @@ -123,6 +123,12 @@ class Articles {
const article = _.pick(findArticle, ['slug', 'title', 'description', 'body', 'taglist', 'favorited', 'favoritedcount', 'flagged', 'images', 'views']);
const readTime = Helper.calculateReadTime(article.body);
article.readtime = readTime;
if (req.auth) {
const { description } = article;
const readerId = req.auth.id;
const item = 'article';
await statsService.createStat({ description, item, readerId }, 'Stats');
}
return res.status(200).json({
status: 200,
message: 'Article successfully retrieved',
Expand Down Expand Up @@ -223,15 +229,15 @@ class Articles {
switch (req.params.channel) {
case 'facebook':
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
case 'twitter':
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
case 'mail':
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
default:
break;
Expand Down
105 changes: 105 additions & 0 deletions src/controllers/comments.controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import commentsService from '../services/comments.service';
import UserService from '../services/user.service';
import models from '../models';
import Helper from '../helpers/helper';
import NotificationServices from '../services/notification.service';
import Util from '../helpers/util';
import StatsService from '../services/db.service';


const util = new Util();

Expand Down Expand Up @@ -107,6 +110,9 @@ class Comments {
await util.setError(200, 'No comments found');
return util.send(res);
}
const readerId = req.auth.id;
const item = 'comment';
await StatsService.createStat({ readerId, item, slug: 'all comments' }, 'Stats');
await util.setSuccess(200, 'All comments successfully retrieved', comments);
return util.send(res);
}
Expand Down Expand Up @@ -140,5 +146,104 @@ class Comments {
await util.setSuccess(200, 'Update is successfully', updateComment);
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} return json object
* @memberof Comments
*/
static async likeComment(req, res) {
const { id } = req.params;
const { username } = req.auth;
const comment = await commentsService.findOne(id);

if (!comment) {
util.setError(404, `Comment with id: ${id} does not exist.`);
return util.send(res);
}
let { likesCount, likeInfo } = comment;
const userHasLikedBefore = likeInfo.search(username);
if (userHasLikedBefore >= 0) {
util.setError(400, 'You liked this comment already');
return util.send(res);
}

likesCount += 1;
likeInfo = `${username}, ${likeInfo}`;
await commentsService.updateComment(id, { likesCount, likeInfo });

const formattedLikeInfo = Helper.formatLikeInfo(likeInfo);
util.setSuccess(201, { likesCount, formattedLikeInfo });
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} return json object
* @memberof Comments
*/
static async updateLikeComment(req, res) {
const { id } = req.params;
const { username } = req.auth;
const comment = await commentsService.findOne(id);
if (!comment) {
util.setError(404, `Comment with id: ${id} does not exist.`);
return util.send(res);
}

let { likesCount, likeInfo } = comment;
const userHasLikedBefore = likeInfo.search(username);

if (userHasLikedBefore === -1) {
util.setError(400, 'You did not like this comment before');
return util.send(res);
}

likesCount -= 1;
likeInfo = likeInfo.replace(`${username}, `, '');
await commentsService.updateComment(id, { likesCount, likeInfo });

util.setSuccess(200, 'You unliked this comment successfully');
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} return json object
* @memberof Comments
*/
static async getLikesComments(req, res) {
const { id } = req.params;
const { username } = req.auth;
const comment = await commentsService.findOne(id);

if (!comment) {
util.setError(404, `Comment with id: ${id} does not exist.`);
return util.send(res);
}
const { likesCount, likeInfo } = comment;

const userHasLikedBefore = likeInfo.search(username);

if (userHasLikedBefore === -1) {
util.setSuccess(200, 'Likes successfully retrieved', { data: { likesCount, likeInfo } });
return util.send(res);
}
const formattedLikeInfo = Helper.formatLikeInfo(likeInfo.replace(`${username}, `, ''));
util.setSuccess(200, { data: { likesCount, formattedLikeInfo } });
return util.send(res);
}
}
export default Comments;
Loading

0 comments on commit c5f4ab7

Please sign in to comment.