Skip to content

Commit

Permalink
Merge d0108c5 into 9828602
Browse files Browse the repository at this point in the history
  • Loading branch information
minega25 committed Oct 17, 2019
2 parents 9828602 + d0108c5 commit 7807488
Show file tree
Hide file tree
Showing 34 changed files with 766 additions and 252 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.com/andela/codepirates-ah-backend.svg?branch=develop)](https://travis-ci.com/andela/codepirates-ah-backend)
[![Coverage Status](https://coveralls.io/repos/github/andela/codepirates-ah-backend/badge.svg?branch=develop)](https://coveralls.io/github/andela/codepirates-ah-backend?branch=develop)
[![Reviewed by Hound CI](https://img.shields.io/badge/Reviewed%20by-Hound%20CI-blue.svg)](https://houndci.com)
[![Maintainability](https://api.codeclimate.com/v1/badges/a1f59061d27322fc6f1f/maintainability)](https://codeclimate.com/github/andela/codepirates-ah-backend/maintainability)

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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"method-override": "^2.3.10",
"moment": "^2.24.0",
"multer": "^1.4.2",
"nyc": "^14.1.1",
"open": "^6.4.0",
Expand Down
4 changes: 3 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import winston from 'winston';
import express from 'express';
import bodyParser from 'body-parser';
import logging from './helpers/logging';
import routes from './routes/index';
import './config/cloudinary.config';
import { mock } from './middlewares/validators/socialLogin-mock';


const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
app.use(mock);
Expand Down
181 changes: 125 additions & 56 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dotenv/config';
import slug from 'slug';
import _ from 'lodash';
import moment from 'moment';
import uniqid from 'uniqid';
import models from '../models';
import Userservice from '../services/user.service';
Expand All @@ -23,18 +24,19 @@ const db = models.Article;
*/
class Articles {
/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Articles
*/
static async createArticles(req, res) {
const userId = req.auth.id;
const findUser = await Userservice.getOneUser(userId);
const images = await cloudinaryHelper.generateCloudinaryUrl(req.files);
// const images = await cloudinaryHelper.generateCloudinaryUrl(req.files);
const { images } = req.body;

if (findUser) {
const { title } = req.body;
Expand Down Expand Up @@ -67,14 +69,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} articles
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} articles
* @memberof Articles
*/
static async getAllArticles(req, res) {
const counter = await db.count();
if (req.offset >= counter) {
Expand All @@ -85,14 +87,47 @@ class Articles {
if (!articles) {
return res.status(200).json({ status: 200, message: 'There is no article.' });
}
const allArticles = _.map(articles, _.partialRight(_.pick, ['slug', 'title', 'description', 'body', 'taglist', 'favorited', 'favoritedcount', 'flagged', 'images', 'views']));
const allArticles = _.map(
articles,
_.partialRight(_.pick, [
'authorId',
'slug',
'title',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views',
'createdAt',
])
);

allArticles.map((article) => {
const readTime = Helper.calculateReadTime(article.body);
article.readtime = readTime;
return true;
});
await Promise.all(allArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
console.log(error);
}
}));
const popularArticles = allArticles.slice(0);
popularArticles.sort((a, b) => b.views - a.views);
const mostPopular = popularArticles.slice(0, 9);

if (req.query.popular) {
util.setSuccess(200, 'The most popular articles on authors haven', mostPopular);
return util.send(res);
}
return res.status(200).json({
status: 200,
message: 'List of all articles',
Expand All @@ -102,14 +137,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} article
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} article
* @memberof Articles
*/
static async getOneArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand All @@ -120,7 +155,19 @@ class Articles {
message: 'That article does not exist!'
});
}
const article = _.pick(findArticle, ['slug', 'title', 'description', 'body', 'taglist', 'favorited', 'favoritedcount', 'flagged', 'images', 'views']);

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) {
Expand All @@ -129,6 +176,28 @@ class Articles {
const item = 'article';
await statsService.createStat({ description, item, readerId }, 'Stats');
}
let viewObject = {
slug: findArticle.slug,
title: findArticle.title,
description: findArticle.description,
body: findArticle.body,
flagged: findArticle.flagged,
favorited: findArticle.favorited,
favoritedcount: findArticle.favoritedcount,
images: findArticle.images,
views: 1
};
if (findArticle) {
viewObject = { ...viewObject, views: findArticle.views + 1 };
await db.update(viewObject, {
where: {
id: findArticle.id
},
returing: true
});
} else {
await db.create(viewObject);
}
return res.status(200).json({
status: 200,
message: 'Article successfully retrieved',
Expand All @@ -137,14 +206,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} message
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} message
* @memberof Articles
*/
static async deleteArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand All @@ -171,14 +240,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} updated article details
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} updated article details
* @memberof Articles
*/
static async UpdateArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand Down Expand Up @@ -207,14 +276,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
static async shareArticle(req, res) {
const article = await db.findOne({
where: { slug: req.params.slug }
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/likes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class LikesController {
return util.send(res);
}
} catch (error) {
util.setError(400, 'server error contact admin');
util.setError(500, 'server error contact admin');
return util.send(res);
}
}
Expand All @@ -174,7 +174,7 @@ class LikesController {
return util.send(res);
}
} catch (error) {
util.setError(400, 'server error contact admin');
util.setError(500, 'server error contact admin');
return util.send(res);
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/controllers/search.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import _ from 'lodash';
import moment from 'moment';
import ArticleModel from '../services/article.service';
import UserModel from '../services/user.service';
import TagModel from '../services/tag.service';
import Util from '../helpers/util';
import Helper from '../helpers/helper';

const util = new Util();
/**
*
*
* @class Report
*/
class Search {
/**
*
*
* @static
* @param {*} req
* @param {*} res
* @return {Json} return json object
* @memberof Search
*/
static async processSearchQuery(req, res) {
const { q, offset, limit } = req.query;
const searchQuery = JSON.parse(q);
const articles = await ArticleModel.searchArticle(searchQuery.keyword, offset, limit);
const foundArticles = _.map(
articles,
_.partialRight(_.pick, [
'authorId',
'slug',
'title',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views',
'createdAt',
])
);

await Promise.all(foundArticles.map(async (article) => {
try {
const userDetails = await UserModel.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
console.log(error);
}
}));
const users = await UserModel.searchUser(searchQuery.user, offset, limit);
const foundUsers = _.map(
users,
_.partialRight(_.pick, [
'username',
'image',
'bio',
])
);
const tags = await TagModel.searchTag('Tag', searchQuery.tag, offset, limit);
const foundTags = _.map(
tags,
_.partialRight(_.pick, [
'name',
])
);
util.setSuccess(200, 'Search successful', { foundArticles, foundUsers, foundTags });
return util.send(res);
}
}

export default Search;
Loading

0 comments on commit 7807488

Please sign in to comment.