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

Commit e155de5

Browse files
author
Luciano Nooijen
committed
Work in progress on auth CRUD
1 parent 89a6dda commit e155de5

File tree

3 files changed

+110
-51
lines changed

3 files changed

+110
-51
lines changed

.vscode/launch.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@
88
"type": "node",
99
"name": "Launch Jest Tests",
1010
"request": "launch",
11+
"env": {"NODE_ENV": "test"},
1112
"args": [
12-
"--runInBand"
13+
"--runInBand",
14+
"-i"
1315
],
1416
"cwd": "${workspaceFolder}",
1517
"console": "integratedTerminal",
1618
"internalConsoleOptions": "neverOpen",
17-
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
18-
},
19-
{
20-
"type": "node",
21-
"request": "launch",
22-
"name": "Launch Program",
23-
"program": "${workspaceFolder}/server.js"
19+
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
20+
"runtimeExecutable": "/usr/local/bin/node"
2421
},
2522
]
2623
}

controllers/articles.js

+65-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint max-len: ["error", { "code": 100 }] */
2+
/* eslint-disable prettier/prettier */
3+
14
const { knex } = require('../helpers');
25

36
const listArticles = async () => {
@@ -18,7 +21,7 @@ const listArticles = async () => {
1821
];
1922
const articles = await knex
2023
.select(fields)
21-
.from('articles') // eslint-disable-next-line
24+
.from('articles')
2225
.join('article_content', 'article_content.article_id', '=', 'articles.id')
2326
.join('categories', 'categories.id', '=', 'articles.category')
2427
.join('authors', 'authors.id', '=', 'articles.author')
@@ -27,14 +30,69 @@ const listArticles = async () => {
2730
return articles;
2831
};
2932

30-
const getRelatedArticles = async id => {};
33+
const getRelatedArticles = async id => {
34+
const fields = [
35+
'articles.id',
36+
'articles.title',
37+
'articles.subtitle',
38+
'articles.slug',
39+
'articles.posted_on',
40+
'article_content.image_url AS article_image_url',
41+
'article_content.summary',
42+
'authors.name AS author_name',
43+
'authors.role AS author_role',
44+
'authors.image_url AS author_image_url',
45+
'categories.name AS category_name',
46+
'categories.slug AS category_slug',
47+
];
48+
const relatedArticles = await knex
49+
.select(fields)
50+
.from('articles')
51+
.join('article_content', 'article_content.article_id', '=', 'articles.id')
52+
.join('categories', 'categories.id', '=', 'articles.category')
53+
.join('authors', 'authors.id', '=', 'articles.author')
54+
.join('related_articles', 'related_articles.related_article_id', '=', 'articles.id')
55+
.where('related_articles.article_id', '=', id)
56+
.andWhere('articles.hidden', '=', false)
57+
.andWhere('articles.posted_on', '<=', knex.raw('now()'));
58+
return relatedArticles;
59+
};
60+
61+
const addRelatedArticles = async (id, article) => {
62+
const relatedArticles = await getRelatedArticles(id);
63+
const articleWithRelatedArticles = {
64+
...article,
65+
related_articles: relatedArticles,
66+
};
67+
return articleWithRelatedArticles;
68+
};
3169

3270
const getArticle = async id => {
33-
const user = await knex
34-
.select('*')
35-
.from('users')
36-
.where({ id });
37-
return user[0];
71+
const fields = [
72+
'articles.id',
73+
'articles.title',
74+
'articles.subtitle',
75+
'articles.slug',
76+
'articles.posted_on',
77+
'article_content.image_url AS article_image_url',
78+
'article_content.summary',
79+
'article_content.html_content',
80+
'authors.name AS author_name',
81+
'authors.role AS author_role',
82+
'authors.image_url AS author_image_url',
83+
'categories.name AS category_name',
84+
'categories.slug AS category_slug',
85+
];
86+
const articles = await knex
87+
.select(fields)
88+
.from('articles') // eslint-disable-next-line
89+
.join('article_content', 'article_content.article_id', '=', 'articles.id')
90+
.join('categories', 'categories.id', '=', 'articles.category')
91+
.join('authors', 'authors.id', '=', 'articles.author')
92+
.where('articles.id', '=', id);
93+
const articleBase = articles[0];
94+
const article = await addRelatedArticles(id, articleBase);
95+
return article;
3896
};
3997

4098
const addArticle = async user => {

tests/controllers/articles.test.ts

+40-36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const newArticle = {
1717
slug: 'new-title',
1818
image_url: 'http://placekitten.com/400/400',
1919
summary: 'the summary',
20+
html_content: 'the content',
21+
author: 1,
22+
category: 1,
23+
related_articles: [1, 2],
2024
};
2125

2226
describe('Articles Controller', () => {
@@ -38,49 +42,49 @@ describe('Articles Controller', () => {
3842
// expect(articles.length).toBeGreaterThan(0);
3943
// });
4044

41-
// test('getArticle should return an article with content', async () => {
42-
// expect.assertions(13);
43-
// const article = await getArticle(1);
44-
// expect(typeof article.id).toBe('number');
45-
// expect(typeof article.title).toBe('string');
46-
// expect(typeof article.subtitle).toBe('string');
47-
// expect(typeof article.slug).toBe('string');
48-
// expect(typeof article.posted_on).toBe('string');
49-
// expect(typeof article.article_image_url).toBe('string');
50-
// expect(typeof article.summary).toBe('string');
51-
// expect(typeof article.html_content).toBe('string');
52-
// expect(typeof article.author_name).toBe('string');
53-
// expect(typeof article.author_role).toBe('string');
54-
// expect(typeof article.author_image_url).toBe('string');
55-
// expect(typeof article.category_name).toBe('string');
56-
// expect(typeof article.category_slug).toBe('string');
57-
// expect(typeof article.relatedArticles).toBe('array');
58-
// });
45+
test('getArticle should return an article with content', async () => {
46+
expect.assertions(14);
47+
const article = await getArticle(1);
48+
expect(typeof article.id).toBe('number');
49+
expect(typeof article.title).toBe('string');
50+
expect(typeof article.subtitle).toBe('string');
51+
expect(typeof article.slug).toBe('string');
52+
expect(article.posted_on).toBeInstanceOf(Date);
53+
expect(typeof article.article_image_url).toBe('string');
54+
expect(typeof article.summary).toBe('string');
55+
expect(typeof article.html_content).toBe('string');
56+
expect(typeof article.author_name).toBe('string');
57+
expect(typeof article.author_role).toBe('string');
58+
expect(typeof article.author_image_url).toBe('string');
59+
expect(typeof article.category_name).toBe('string');
60+
expect(typeof article.category_slug).toBe('string');
61+
expect(typeof article.related_articles).toBe('array');
62+
});
5963

6064
// test('calculateReadingTime should calculate reading time', async () => {
6165
// });
6266

6367
// test('getArticle should list reading time', async () => {
6468
// });
6569

66-
// test('getRelatedArticles should return a summary array', async () => {
67-
// expect.assertions(13);
68-
// const relatedArticles = await getRelatedArticles(1);
69-
// const article = relatedArticles[0];
70-
// expect(typeof relatedArticles).toBe('ar ray');
71-
// expect(typeof article.id).toBe('number');
72-
// expect(typeof article.title).toBe('string');
73-
// expect(typeof article.subtitle).toBe('string');
74-
// expect(typeof article.slug).toBe('string');
75-
// expect(typeof article.posted_on).toBe('string');
76-
// expect(typeof article.article_image_url).toBe('string');
77-
// expect(typeof article.summary).toBe('string');
78-
// expect(typeof article.author_name).toBe('string');
79-
// expect(typeof article.author_role).toBe('string');
80-
// expect(typeof article.author_image_url).toBe('string');
81-
// expect(typeof article.category_name).toBe('string');
82-
// expect(typeof article.category_slug).toBe('string');
83-
// });
70+
test('getRelatedArticles should return a summary array', async () => {
71+
expect.assertions(13);
72+
const relatedArticles = await getRelatedArticles(1);
73+
const article = relatedArticles[0];
74+
expect(typeof relatedArticles).toBe('array');
75+
expect(typeof article.id).toBe('number');
76+
expect(typeof article.title).toBe('string');
77+
expect(typeof article.subtitle).toBe('string');
78+
expect(typeof article.slug).toBe('string');
79+
expect(typeof article.posted_on).toBe('string');
80+
expect(typeof article.article_image_url).toBe('string');
81+
expect(typeof article.summary).toBe('string');
82+
expect(typeof article.author_name).toBe('string');
83+
expect(typeof article.author_role).toBe('string');
84+
expect(typeof article.author_image_url).toBe('string');
85+
expect(typeof article.category_name).toBe('string');
86+
expect(typeof article.category_slug).toBe('string');
87+
});
8488

8589
// test('addArticle should add an article correctly', async () => {
8690
// });

0 commit comments

Comments
 (0)