From 434a1655ddd1a004d44becdea095ffec464cd4c6 Mon Sep 17 00:00:00 2001 From: Luciano Nooijen Date: Tue, 26 Feb 2019 09:50:59 +0100 Subject: [PATCH] Added SEO tags to article table Release V1.0.0-alpha.3 --- controllers/articles.js | 6 +++++ .../migrations/20190226092504_seo_tags.js | 26 +++++++++++++++++++ database/seeds/test-data.js | 6 +++++ tests/controllers/articles.test.ts | 8 +++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 database/migrations/20190226092504_seo_tags.js diff --git a/controllers/articles.js b/controllers/articles.js index 1fc9b21..22a258b 100644 --- a/controllers/articles.js +++ b/controllers/articles.js @@ -9,6 +9,9 @@ const fieldsBase = [ 'articles.subtitle', 'articles.slug', 'articles.posted_on', + 'articles.seo_title', + 'articles.seo_description', + 'articles.seo_tags', 'article_content.image_url AS article_image_url', 'article_content.summary', 'authors.name AS author_name', @@ -154,6 +157,9 @@ const addArticle = async (knex, article) => { slug: article.slug, author: article.author, category: article.author, + seo_title: article.seo_title, + seo_description: article.seo_description, + seo_tags: article.seo_tags, }; const addedArticleData = await addToArticlesTable(knex, articleData); const addedArticleId = addedArticleData.id; diff --git a/database/migrations/20190226092504_seo_tags.js b/database/migrations/20190226092504_seo_tags.js new file mode 100644 index 0000000..6a69f6e --- /dev/null +++ b/database/migrations/20190226092504_seo_tags.js @@ -0,0 +1,26 @@ +/* eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 10 }] */ +/* eslint-disable prettier/prettier, max-len, arrow-body-style */ + +const addSeoTags = table => { + table.string('seo_title').notNullable(); + table.string('seo_description').notNullable(); + table.string('seo_tags').notNullable(); +}; + +const removeSeoTags = table => { + table.dropColumn('seo_title'); + table.dropColumn('seo_description'); + table.dropColumn('seo_tags'); +}; + +module.exports.up = (knex, Promise) => { + return Promise.all([ + knex.schema.table('articles', table => addSeoTags(table)), + ]); +}; + +module.exports.down = (knex, Promise) => { + return Promise.all([ + knex.schema.table('articles', table => removeSeoTags(table)), + ]); +}; diff --git a/database/seeds/test-data.js b/database/seeds/test-data.js index 6ae1a6e..9c4b8f5 100644 --- a/database/seeds/test-data.js +++ b/database/seeds/test-data.js @@ -96,6 +96,9 @@ const insertArticles = knex => subtitle: 'Subtitle Article 1', slug: 'article_one', category: 1, + seo_title: 'Title1', + seo_description: 'Description1', + seo_tags: 'Tags1', }, { author: 1, @@ -103,6 +106,9 @@ const insertArticles = knex => subtitle: 'Subtitle Article 2', slug: 'article_two', category: 1, + seo_title: 'Title2', + seo_description: 'Description2', + seo_tags: 'Tags2', }, ]), ); // eslint-disable-line diff --git a/tests/controllers/articles.test.ts b/tests/controllers/articles.test.ts index e513186..2412d8f 100644 --- a/tests/controllers/articles.test.ts +++ b/tests/controllers/articles.test.ts @@ -26,6 +26,9 @@ const newArticle = { html_content: 'the content', author: 2, category: 1, + seo_title: 'Titletest', + seo_description: 'Descriptiontest', + seo_tags: 'Tagstest', related_articles: [1, 2], }; @@ -83,7 +86,7 @@ describe('Articles Controller', () => { }); test('getArticle should return an article with content', async () => { - expect.assertions(14); + expect.assertions(17); const article = await getArticle(blog, 1); expect(typeof article.id).toBe('number'); expect(typeof article.title).toBe('string'); @@ -99,6 +102,9 @@ describe('Articles Controller', () => { expect(typeof article.category_name).toBe('string'); expect(typeof article.category_slug).toBe('string'); expect(typeof article.reading_time).toBe('number'); + expect(typeof article.seo_title).toBe('string'); + expect(typeof article.seo_description).toBe('string'); + expect(typeof article.seo_tags).toBe('string'); }); test('calculateReadingTime should calculate reading time', async () => {