Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 38bb719

Browse files
author
Luciano Nooijen
committed
Added article deletion
1 parent ef80bfd commit 38bb719

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

controllers/articles.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ const getRelatedArticles = async id => {
7777
return articlesWithReadingTime;
7878
};
7979

80-
const getRelatedArticlesToArticleObject = async (id, article) => {
80+
const addRelatedArticlesToArticleObject = async (id, article) => {
8181
const relatedArticles = await getRelatedArticles(id);
82+
if (relatedArticles.length === 0) {
83+
return article;
84+
}
8285
const articleWithRelatedArticles = {
8386
...article,
8487
related_articles: relatedArticles,
@@ -100,7 +103,7 @@ const getArticle = async id => {
100103
.where('articles.id', '=', id);
101104
const articlesWithReadingTime = addReadingTimeToArticles(articles);
102105
const articleBase = articlesWithReadingTime[0];
103-
const article = await getRelatedArticlesToArticleObject(id, articleBase);
106+
const article = await addRelatedArticlesToArticleObject(id, articleBase);
104107
return article;
105108
};
106109

@@ -167,29 +170,34 @@ const addArticle = async article => {
167170
return createdArticle;
168171
};
169172

170-
const modifyArticles = async (id, article) => {
173+
const modifyArticle = async (id, article) => {
171174

172175
};
173176

174-
const deleteArticle = async id =>
175-
new Promise(resolve =>
176-
knex('users')
177-
.returning(['id'])
178-
.where({ id })
179-
.delete()
180-
.then(data => resolve(data[0])),
181-
); // eslint-disable-line
177+
const deleteArticle = async id => {
178+
await knex('related_articles')
179+
.where({ article_id: id })
180+
.orWhere({ related_article_id: id })
181+
.delete();
182+
await knex('article_content')
183+
.where({ article_id: id })
184+
.delete();
185+
await knex('articles')
186+
.where({ id })
187+
.delete();
188+
return { id };
189+
};
182190

183191
module.exports = {
184192
listArticles,
185193
getRelatedArticles,
186-
getRelatedArticlesToArticleObject,
194+
addRelatedArticlesToArticleObject,
187195
calculateReadingTime,
188196
addReadingTimeToArticles,
189197
addToRelatedArticlesTable,
190198
getArticle,
191199
generateRelatedArticles,
192200
addArticle,
193-
modifyArticles,
201+
modifyArticle,
194202
deleteArticle,
195203
};

helpers/auth-helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const checkPasswordHash = async (plainTextPassword, hashedPassword) => {
3030
const calculateDates = issuedAtParam => {
3131
const date = new Date();
3232
const issuedAt = issuedAtParam || date.setDate(date.getDate());
33-
const issuedAtDate = new Date(issuedAt);
33+
const issuedAtDate = new Date(issuedAt); // eslint-disable-next-line
3434
const expiryDate = issuedAtDate.setDate(issuedAtDate.getDate() + jwtExpiresInDays);
3535
const dates = {
3636
iat: issuedAt,

tests/controllers/articles.test.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { useTestDatabase } from '../config/index';
33
const {
44
listArticles,
55
getRelatedArticles,
6-
getRelatedArticlesToArticleObject,
6+
addRelatedArticlesToArticleObject,
77
calculateReadingTime,
88
addReadingTimeToArticles,
99
getArticle,
1010
generateRelatedArticles,
1111
addToRelatedArticlesTable,
1212
addArticle,
13-
modifyArticles,
13+
modifyArticle,
1414
deleteArticle,
1515
} = require('../../controllers/articles');
1616

@@ -196,12 +196,17 @@ describe('Articles Controller', () => {
196196
expect(typeof addedArticle.reading_time).toBe('number');
197197
});
198198

199-
// test('addArticle should work with custom posted_on', async () => {
199+
// test('addArticle should work with custom posted_on', async () => { TODO:
200200
// });
201201

202202
// test('modifyArticle should modify an article correctly', async () => {
203-
// });
204203

205-
// test('deleteArticle should delete an article', async () => {
206204
// });
205+
206+
test('deleteArticle should delete an article', async () => {
207+
expect.assertions(2);
208+
return deleteArticle(1)
209+
.then(data => expect(data.id).toBe(1))
210+
.then(async () => expect(await getArticle(1)).toBeUndefined());
211+
});
207212
});

0 commit comments

Comments
 (0)