Skip to content

Commit

Permalink
feature(share-articles): Rebase to current changes [Finishes #167313416]
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalyesubula committed Aug 29, 2019
2 parents 82142a8 + cca1648 commit 730d291
Show file tree
Hide file tree
Showing 44 changed files with 2,765 additions and 121 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ typings/

#ignore vscode settings file
.vscode/settings.json

#ignore DS_Store
.DS_Store

# ignore package-lock
package-lock.json

# End of https://www.gitignore.io/api/node

6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
language: node_js

node_js:
- "stable"
- 'stable'

cache:
directories:
- "node_modules"
- 'node_modules'

install:
- npm ci
- npm install

services:
- postgresql
Expand Down
6 changes: 3 additions & 3 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ module.exports = {
database: DB_NAME,
host: DB_HOST,
dialect: 'postgres',
port: DB_PORT
port: DB_PORT,
},
test: {
username: DB_USER,
password: DB_PASSWORD,
database: DB_NAME_TEST,
host: DB_HOST,
dialect: 'postgres',
port: DB_PORT
port: DB_PORT,
},
production: {
username: DB_USER,
Expand All @@ -35,6 +35,6 @@ module.exports = {
database: DB_NAME,
url: DATABASE_URL,
dialect: 'postgres',
port: DB_PORT
port: DB_PORT,
}
};
20 changes: 3 additions & 17 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,10 @@ class Articles {
*/
static async getAllArticles(req, res) {
const counter = await db.count();
let page = parseInt(req.query.page, 10);
if (isNaN(page) || page < 1) {
page = 1;
if (req.offset >= counter) {
req.offset = 0;
}
let limit = parseInt(req.query.limit, 10);
if (isNaN(limit)) {
limit = 10;
} else if (limit > 50) {
limit = 50;
} else if (limit < 1) {
limit = 1;
}
let offset = (page - 1) * limit;
if (offset >= counter) {
offset = 0;
}

const { searchQueries } = req;
const { searchQueries, offset, limit } = req;
const articles = await articleService.getAllArticles(offset, limit, searchQueries);
if (!articles) {
return res.status(200).json({ status: 200, message: 'There is no article.' });
Expand Down
143 changes: 143 additions & 0 deletions src/controllers/bookmarks.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* eslint-disable require-jsdoc */
import Util from '../helpers/util';
import dbService from '../services/data.service';
import models from '../models';

const {
checkItem, ensureItem, checkItems, deleteItem, updateItem
} = dbService;

const util = new Util();

const notFound = (msg) => {
util.setError(404, `${msg} not found`);
return util;
};

class BookMarkController {
static async createBookMark(req, res) {
const article = await checkItem(req.body.articleId, 'Article');
const name = req.body.name || `${article.title}-${new Date()}`;
const { articleId } = req.body;
const userId = req.auth.id;
const bookmark = await ensureItem({ articleId, name, userId });
return res.status(201).json({
message: `bookmark '${name}' created`, data: bookmark[0]
});
}

static async editBookMark(req, res) {
const oldName = req.params.name || req.data.name;
const name = req.body.name || req.data.newName;
const updated = updateItem({ name }, { name: oldName });
return res.status(200).json({
message: `bookmark ${oldName} updated to ${name}`,
data: updated[1]
});
}

static async createCollection(req, res) {
const { name, articleId, collection } = req.body;
const userId = req.auth.id;
const added = await ensureItem({
articleId, collection, name, userId
});
if (added[1]) {
return res.status(201).json({
message: `bookmark '${name}' added to collection '${collection}'`
});
}
res.status(200).json({
message: `bookmark '${name}' already in collection '${collection}'`
});
}

static async getCollection(req, res) {
const { collection } = req.params;
const Collection = await checkItems({ collection, userId: req.auth.id },
'BookMark', [models.Article]);
return res.status(200).json({ Collection });
}

static async getCollections(req, res) {
const all = await checkItems({ userId: req.auth.id }, 'BookMark', [models.Article]);
const collections = all.filter(x => x.collection);
if (collections.length) {
// reduce/map collections to collection: bookmark-count object array
return res.status(200).json({ collections });
}
notFound('collections').send(res);
}

static async updateCollection(req, res) {
const { collection } = req.params;
const updated = await updateItem({ collection }, {
collection: req.body.collection,
userId: req.auth.id
});
return res.status(200).json({
message: `collection '${collection}' updated to ${collection}`,
data: updated
});
}

// delete collection and content
static async deleteCollection(req, res) {
const { collection } = req.params;
await deleteItem({ collection });
return res.status(404).json({ message: `collection '${collection}' deleted` });
}

// delete from collection
static async unCollect(req, res) {
const { collection, name } = req.params;
const userId = req.auth.id;
await updateItem({ collection: '' }, { collection, name, userId });
return res.status(404).json({
message: `bookmark ${name} deleted from collection ${collection}`
});
}

static async getUserBookMarks(req, res) {
const bookmarks = await checkItems({ userId: req.auth.id });
return res.status(200).json({
message: `${bookmarks.length} bookmarks found`,
data: bookmarks
});
}

static async getUserBookMark(req, res) {
const bookmark = await checkItem({ userId: req.auth.id, name: req.params.name });
return res.status(200).json({
message: `bookmark '${bookmark.name}' found`,
data: bookmark
});
}

static async deleteUserBookMark(req, res) {
const bookmark = await checkItem({ userId: req.auth.id, name: req.params.name });
bookmark.destroy();
return res.status(404).json({
message: `bookmark '${req.params.name}' deleted`
});
}

static async deleteUserBookMarks(req, res) {
const reader = await checkItem(req.auth.id, 'user', ['articles']);
const deleted = await reader.removeArticles(reader.articles);
res.status(404).json({ message: `${deleted} bookmarks deleted` });
}

static async copyBookmark(req, res) {
const {
articleId, name, newName, userId
} = req.data;
const copy = await ensureItem({ articleId, name: newName, userId });
res.status(201).json({
message: `copy of bookmark ${name} created as ${newName}`,
data: copy
});
}
}

export default BookMarkController;
64 changes: 26 additions & 38 deletions src/controllers/comments.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import commentsService from '../services/comments.service';
import UserService from '../services/user.service';
import models from '../models';
import NotificationServices from '../services/notification.service';
import Util from '../helpers/util';

const util = new Util();

const { notifyUsersWhoFavorited } = NotificationServices;

Expand Down Expand Up @@ -39,11 +42,8 @@ class Comments {
};
const createdComment = await commentsService.addComment(comment);
await notifyUsersWhoFavorited(req, res, getArticle.id, req.params.slug);
return res.status(201).send({
status: 201,
message: 'Comment successfully created',
comment: createdComment
});
await util.setSuccess(201, 'Comment successfully created', createdComment);
return util.send(res);
} catch (error) {
return res.send({
message: error.message
Expand All @@ -67,24 +67,18 @@ class Comments {
const { id } = req.params;

if (!Number(id)) {
return res.status(400).send({
status: 400,
message: 'Please provide numeric value'
});
await util.setError(400, 'Please provide numeric value');
return util.send(res);
}
if (!(req.auth.id === commentAuthor)) {
return res.status(403).send({
status: 403,
message: 'comment is not yours'
});
await util.setError(403, 'comment is not yours');
return util.send(res);
}
try {
const CommentTODelete = await commentsService.deleteComment(id);
if (CommentTODelete) {
return res.status(200).send({
status: 200,
message: `Comment with id ${id} is successfully deleted`
});
await util.setSuccess(200, `Comment with id ${id} is successfully deleted`);
return util.send(res);
}

return res.status(404).send({
Expand All @@ -110,15 +104,11 @@ class Comments {
static async getComments(req, res) {
const comments = await CommentsDb.findAll();
if (!comments) {
return res.status(200).send({
message: 'No comments found'
});
await util.setError(200, 'No comments found');
return util.send(res);
}
return res.status(200).send({
status: 200,
message: 'All comments successfully retrieved',
comments
});
await util.setSuccess(200, 'All comments successfully retrieved', comments);
return util.send(res);
}

/**
Expand All @@ -134,23 +124,21 @@ class Comments {
const getComment = await CommentsDb.findOne({ where: { id: req.params.id } });
const gottenComent = getComment && getComment.get().id;
const { id } = req.params;
if (!id) {
return res.status(400).send({
status: 400,
message: 'Please provide valid numeric value'
});
}

if (!gottenComent) {
return res.status(200).json({ status: 200, message: 'That comment does not exist' });
await util.setSuccess(200, 'That comment does not exist');
return util.send(res);
}
if (!Number(id)) {
await util.setError(400, 'Please provide numeric value');
return util.send(res);
}

const { body } = req.body;
const updateComment = await commentsService.updateComment(req.params.id, { body });
return res.status(200).send({
status: 200,
message: 'Updation is successfully',
updateComment
});
const commentRevisions = getComment.dataValues.body;
const updateComment = await commentsService.updateComment(req.params.id, { body, commentRevisions });
await util.setSuccess(200, 'Update is successfully', updateComment);
return util.send(res);
}
}
export default Comments;
Loading

0 comments on commit 730d291

Please sign in to comment.