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

Commit c60d2c3

Browse files
author
Luciano Nooijen
committed
Added category functionality
1 parent a321eca commit c60d2c3

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
[![NodeJS Version](https://img.shields.io/badge/Node%20Version-%3E%3D%20v8.0.0-green.svg)](https://img.shields.io/badge/Node%20Version-%3E%3D%20v8.0.0-green.svg)
99
[![GPLv3 license](https://img.shields.io/badge/License-GPLv3-blue.svg)](http://perso.crans.org/besson/LICENSE.html)
1010

11-
**NOTE: THIS PACKAGE IS STILL IN DEVELOPMENT! RELEASE OF v1.0.0 WILL BE AROUND DECEMBER 1st, 2018.**
12-
1311
Blog API developed by Bytecode Digital Agency as free (as in freedom) open source software. Built in NodeJS. Available as a standalone server, or as a NPM package
1412

1513
## Installation
@@ -58,7 +56,7 @@ Then you can use the imported functions as you wish, for example:
5856
const posts = await articles.list(blog);
5957
```
6058

61-
Just send the `blog` instance as the first argument and the rest of the arguments second.
59+
Just send the `blog` instance as the first argument and the rest of the arguments second. This is because this way the same logic can be applied to multiple blog instances within an application.
6260

6361
The available methods are:
6462

controllers/categories.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const listCategories = async knex => {
2+
const Categories = await knex.select('*').from('categories');
3+
return Categories;
4+
};
5+
6+
const getCategory = async (knex, id) => {
7+
const category = await knex
8+
.select('*')
9+
.from('categories')
10+
.where({ id });
11+
return category[0];
12+
};
13+
14+
const addCategory = async (knex, category) => {
15+
const newCategoryData = {
16+
name: category.name,
17+
slug: category.slug,
18+
};
19+
const returning = ['id', 'name', 'slug'];
20+
const newCategory = await knex('categories')
21+
.insert([newCategoryData])
22+
.returning(returning);
23+
return newCategory[0];
24+
};
25+
26+
const modifyCategory = async (knex, id, category) => {
27+
const { name, slug } = category;
28+
const newCategoryData = { name, slug };
29+
const returning = ['id', 'name', 'slug'];
30+
const oldCategoryData = getCategory(knex, id);
31+
const newCategory = Object.assign(
32+
{},
33+
{ ...oldCategoryData, ...newCategoryData },
34+
);
35+
const modifiedCategory = await knex('categories')
36+
.returning(returning)
37+
.where('id', '=', id)
38+
.update(newCategory);
39+
return modifiedCategory[0];
40+
};
41+
42+
const deleteCategory = async (knex, id) =>
43+
new Promise(resolve =>
44+
knex('categories')
45+
.returning(['id'])
46+
.where({ id })
47+
.delete()
48+
.then(data => resolve(data[0])),
49+
); // eslint-disable-line
50+
51+
module.exports = {
52+
listCategories,
53+
getCategory,
54+
addCategory,
55+
modifyCategory,
56+
deleteCategory,
57+
};

tests/controllers/categories.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { useTestDatabase } from '../config/index';
2+
import blog from '../config/blog';
3+
4+
const { authHelper } = require('../../helpers');
5+
6+
const {
7+
listCategories,
8+
getCategory,
9+
addCategory,
10+
modifyCategory,
11+
deleteCategory,
12+
} = require('../../controllers/categories');
13+
14+
useTestDatabase();
15+
16+
const newCategory = {
17+
name: 'The new category name',
18+
slug: 'the-new-slug',
19+
};
20+
21+
describe('Test if Categories CRUD operations are working correctly', () => {
22+
test('Listing all Categories should return rows', async () => {
23+
expect.assertions(1);
24+
const categories = await listCategories(blog);
25+
expect(categories.length).toBeGreaterThan(0);
26+
});
27+
28+
test('Fetching a single Category should return an Categorie', async () => {
29+
expect.assertions(3);
30+
const category = await getCategory(blog, 1);
31+
expect(category.id).toBe(1);
32+
expect(typeof category.name).toBe('string');
33+
expect(typeof category.slug).toBe('string');
34+
});
35+
36+
test('Adding a new Category should add a single row', async () => {
37+
expect.assertions(1);
38+
const categoriesBefore = await listCategories(blog);
39+
const categorieLengthBefore = categoriesBefore.length;
40+
await addCategory(blog, newCategory);
41+
const categoriesAfter = await listCategories(blog);
42+
const categorieLengthAfter = categoriesAfter.length;
43+
expect(categorieLengthAfter).toBe(categorieLengthBefore + 1);
44+
});
45+
46+
test('Adding a new Category should return the new Category', async () => {
47+
expect.assertions(3);
48+
const addedCategory = await addCategory(blog, newCategory);
49+
expect(typeof addedCategory.id).toBe('number');
50+
expect(addedCategory.name).toBe(newCategory.name);
51+
expect(addedCategory.slug).toBe(newCategory.slug);
52+
});
53+
54+
test('Updating an Category should return the modified data', async () => {
55+
expect.assertions(7);
56+
const originalCategory = await getCategory(blog, 1);
57+
expect(originalCategory.id).toBe(1);
58+
expect(originalCategory.name).not.toBe(newCategory.name);
59+
expect(originalCategory.slug).not.toBe(newCategory.slug);
60+
const modifiedCategory = await modifyCategory(blog, 1, newCategory);
61+
expect(modifiedCategory.id).toBeDefined();
62+
expect(typeof modifiedCategory.id).toBe('number');
63+
expect(modifiedCategory.name).toBe(newCategory.name);
64+
expect(modifiedCategory.slug).toBe(newCategory.slug);
65+
});
66+
67+
test('Deleting an Category should return undefined', async () => {
68+
expect.assertions(2);
69+
return deleteCategory(blog, 2)
70+
.then(data => expect(data.id).toBe(2))
71+
.then(async () =>
72+
expect(await getCategory(blog, 2)).toBeUndefined());
73+
});
74+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy