From 8ab9f09287f7b67dfe17fd8749093139dca3bebf Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 20 Nov 2018 08:39:25 +1100 Subject: [PATCH] Built basic blog backend. --- private/app/App.js | 36 ++++++++-- private/blog/Articles.js | 70 +++++++++++++++++++ private/cache/CacheStore.js | 6 +- private/database/DatabaseConnection.js | 3 +- private/database/DatabaseInterface.js | 35 ++++++++++ private/database/queries/blog/addArticle.sql | 5 ++ .../queries/blog/getArticleByHandle.sql | 1 + .../database/queries/blog/getArticleById.sql | 1 + .../queries/blog/getArticlesByPage.sql | 11 +++ .../queries/blog/getArticlesCount.sql | 1 + .../create/CreateBlogArticlesTable.sql | 9 +++ .../queries/create/CreateFormatsTable.sql | 5 -- .../queries/create/CreateGamesTable.sql | 4 -- .../queries/create/CreateSeasonsTable.sql | 7 -- .../create/CreateTeamUsersSeasonsTable.sql | 7 -- .../queries/create/CreateTeamsTable.sql | 7 -- .../queries/create/CreateUsersTable.sql | 6 -- private/database/queries/format/AddFormat.sql | 1 - .../queries/format/GetFormatsByGame.sql | 1 - private/database/queries/game/AddGame.sql | 1 - .../database/queries/game/GetGameByName.sql | 1 - .../database/queries/season/GetSeasonById.sql | 1 - .../queries/season/GetSeasonForDate.sql | 5 -- private/database/queries/team/AddTeam.sql | 5 -- .../queries/team/AddTeamUserSeason.sql | 5 -- private/database/queries/team/GetTeamById.sql | 1 - .../database/queries/team/GetTeamByName.sql | 1 - .../queries/team/GetTeamBySeasonAndUser.sql | 10 --- .../queries/team/GetTeamUsersBySeason.sql | 1 - .../queries/team/GetTeamsBySeason.sql | 11 --- private/database/queries/user/AddUser.sql | 9 --- .../queries/user/GetUserByDiscordId.sql | 1 - private/database/queries/user/GetUserById.sql | 1 - .../api/methods/blog/GetBlogArticles.js | 50 +++++++++++++ 34 files changed, 221 insertions(+), 98 deletions(-) create mode 100644 private/blog/Articles.js create mode 100644 private/database/DatabaseInterface.js create mode 100644 private/database/queries/blog/addArticle.sql create mode 100644 private/database/queries/blog/getArticleByHandle.sql create mode 100644 private/database/queries/blog/getArticleById.sql create mode 100644 private/database/queries/blog/getArticlesByPage.sql create mode 100644 private/database/queries/blog/getArticlesCount.sql create mode 100644 private/database/queries/create/CreateBlogArticlesTable.sql delete mode 100644 private/database/queries/create/CreateFormatsTable.sql delete mode 100644 private/database/queries/create/CreateGamesTable.sql delete mode 100644 private/database/queries/create/CreateSeasonsTable.sql delete mode 100644 private/database/queries/create/CreateTeamUsersSeasonsTable.sql delete mode 100644 private/database/queries/create/CreateTeamsTable.sql delete mode 100644 private/database/queries/create/CreateUsersTable.sql delete mode 100644 private/database/queries/format/AddFormat.sql delete mode 100644 private/database/queries/format/GetFormatsByGame.sql delete mode 100644 private/database/queries/game/AddGame.sql delete mode 100644 private/database/queries/game/GetGameByName.sql delete mode 100644 private/database/queries/season/GetSeasonById.sql delete mode 100644 private/database/queries/season/GetSeasonForDate.sql delete mode 100644 private/database/queries/team/AddTeam.sql delete mode 100644 private/database/queries/team/AddTeamUserSeason.sql delete mode 100644 private/database/queries/team/GetTeamById.sql delete mode 100644 private/database/queries/team/GetTeamByName.sql delete mode 100644 private/database/queries/team/GetTeamBySeasonAndUser.sql delete mode 100644 private/database/queries/team/GetTeamUsersBySeason.sql delete mode 100644 private/database/queries/team/GetTeamsBySeason.sql delete mode 100644 private/database/queries/user/AddUser.sql delete mode 100644 private/database/queries/user/GetUserByDiscordId.sql delete mode 100644 private/database/queries/user/GetUserById.sql create mode 100644 private/server/api/methods/blog/GetBlogArticles.js diff --git a/private/app/App.js b/private/app/App.js index 51f33ac..fb4150a 100644 --- a/private/app/App.js +++ b/private/app/App.js @@ -27,24 +27,28 @@ const DatabaseConnection = require('./../database/DatabaseConnection'), Server = require('./../server/Server'), Email = require('./../email/Email'), - CacheStore = require('./../cache/CacheStore') + CacheStore = require('./../cache/CacheStore'), + + Articles = require('./../blog/Articles') ; class App { constructor() { this.config = new Configuration(this); this.database = new DatabaseConnection(this); + + this.articles = new Articles(this); + this.server = new Server(this); this.email = new Email(this); - //this.store = new CacheStore(this); } getConfig() { return this.config; } - //getCacheStore() {return this.store;} - getDiscord() { return this.discord; } getDatabase() { return this.database; } - getPalaise() { return this.palaise; } getEmail() {return this.email;} + getServer() {return this.server;} + + getArticles() {return this.articles;} async init() { this.log('Starting App...'); @@ -90,6 +94,28 @@ class App { } this.log('App ready'); + console.log(await this.articles.getArticlesByPage(2, 20)); + } + + // Common Functions // + createHandle(str) { + //Creates a human handle for the supplied string, this won't take any kind + //of existing checks into account, be sure to append a numeric value to the + //end of this string such as app.createHandle("test")+"-2"; + str = str.toLowerCase(); + ['"', "'", "\\", "(", ")", "[", "]"].forEach(e => str = str.replace(e, "")); + + str = str.replace(/\W+/g, "-"); + + if (str.charAt(str.length - 1) == "-") { + str = str.replace(/-+\z/, ""); + } + + if (str.charAt(0) == "-") { + str = str.replace(/\A-+/, ""); + } + + return str; } // Logging Functions // diff --git a/private/blog/Articles.js b/private/blog/Articles.js new file mode 100644 index 0000000..c997ec2 --- /dev/null +++ b/private/blog/Articles.js @@ -0,0 +1,70 @@ +// Copyright (c) 2018 Dominic Masters +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +const DatabaseInterface = require('./../database/DatabaseInterface'); + +module.exports = class Articles extends DatabaseInterface { + constructor(app) { + super(app); + } + + async getArticlesCount() { + return (await this.store.getFromDatabase( + `getArticlesCount`, 'getArticlesCount', {}, 'one' + )).count; + } + + async getArticleById(id) { + return await this.store.getFromDatabase( + `getArticleById_${id}`, `getArticleById`, {id}, 'one' + ); + } + + async getArticleByHandle(handle) { + return await this.store.getFromDatabase( + `getArticleByHandle_${handle}`, `getArticleByHandle`, {handle}, 'one' + ); + } + + async getArticlesByPage(page, perPage) { + if(!page) page = 1; + if(!perPage) perPage = 10; + + page = Math.max(0, page - 1) * perPage; + + return await this.store.getFromDatabase( + `getArticlesByPage_${page}_${perPage}`, `getArticlesByPage`, + { count:perPage, offset:page }, 'any' + ); + } + + + async addArticle(handle, title, image, shortDescription, description, date) { + if(!date) date = new Date(); + let article = await this.getDatabase().one('addArticle', { + handle, title, image, shortDescription, description, date + }); + this.store.flush();//In future support my wildcard syntax to make this no longer necessary. + return article; + } +} diff --git a/private/cache/CacheStore.js b/private/cache/CacheStore.js index 5b23211..fa2e737 100644 --- a/private/cache/CacheStore.js +++ b/private/cache/CacheStore.js @@ -54,13 +54,17 @@ class CacheStore { this.store.del(keys); } + flush() { + this.store.flushAll(); + } + //Database related stores async getFromDatabase(key, query, params, method) { if(typeof params === typeof undefined) params = {}; if(typeof method === typeof undefined) method = "any"; return await this.get(key, async () => { - return await this.database[method](query, params); + return await this.getDatabase()[method](query, params); }); } } diff --git a/private/database/DatabaseConnection.js b/private/database/DatabaseConnection.js index bc9b4ea..e38beea 100644 --- a/private/database/DatabaseConnection.js +++ b/private/database/DatabaseConnection.js @@ -68,12 +68,13 @@ class DatabaseConnection { let keys = Object.keys(queries); for(let i = 0; i < keys.length; i++) { let k = keys[i]; - if(!k.startsWith("Create")) return; + if(!k.toLowerCase().startsWith("create")) continue; await this.none(k); }; } getQuery(name) { + if(!this.queries[name]) throw new Error("No Query by that name exists"); return this.queries[name]; } diff --git a/private/database/DatabaseInterface.js b/private/database/DatabaseInterface.js new file mode 100644 index 0000000..e96ee15 --- /dev/null +++ b/private/database/DatabaseInterface.js @@ -0,0 +1,35 @@ +// Copyright (c) 2018 Dominic Masters +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +const CacheStore = require('./../cache/CacheStore'); + +module.exports = class DatabaseInterface { + constructor(app) { + this.app = app; + this.store = new CacheStore(app); + } + + getApp() {return this.app;} + getDatabase() {return this.app.getDatabase();} + getStore() {return this.store;} +} diff --git a/private/database/queries/blog/addArticle.sql b/private/database/queries/blog/addArticle.sql new file mode 100644 index 0000000..b64734c --- /dev/null +++ b/private/database/queries/blog/addArticle.sql @@ -0,0 +1,5 @@ +INSERT INTO "BlogArticles" ( + "handle", "image", "shortDescription", "description", "date" +) VALUES ( + ${handle}, ${image}, ${shortDescription}, ${description}, ${date} +) RETURNING *; diff --git a/private/database/queries/blog/getArticleByHandle.sql b/private/database/queries/blog/getArticleByHandle.sql new file mode 100644 index 0000000..f1ed300 --- /dev/null +++ b/private/database/queries/blog/getArticleByHandle.sql @@ -0,0 +1 @@ +SELECT * FROM "BlogArticles" WHERE "handle"=${handle}; diff --git a/private/database/queries/blog/getArticleById.sql b/private/database/queries/blog/getArticleById.sql new file mode 100644 index 0000000..d1b1d57 --- /dev/null +++ b/private/database/queries/blog/getArticleById.sql @@ -0,0 +1 @@ +SELECT * FROM "BlogArticles" WHERE "id" = ${id}; diff --git a/private/database/queries/blog/getArticlesByPage.sql b/private/database/queries/blog/getArticlesByPage.sql new file mode 100644 index 0000000..cd6c095 --- /dev/null +++ b/private/database/queries/blog/getArticlesByPage.sql @@ -0,0 +1,11 @@ +SELECT + * +FROM + "BlogArticles" +ORDER BY + "date" DESC +LIMIT + ${count} +OFFSET + ${offset} +; diff --git a/private/database/queries/blog/getArticlesCount.sql b/private/database/queries/blog/getArticlesCount.sql new file mode 100644 index 0000000..ac0e81f --- /dev/null +++ b/private/database/queries/blog/getArticlesCount.sql @@ -0,0 +1 @@ +SELECT COUNT(*) FROM "BlogArticles"; diff --git a/private/database/queries/create/CreateBlogArticlesTable.sql b/private/database/queries/create/CreateBlogArticlesTable.sql new file mode 100644 index 0000000..2682a25 --- /dev/null +++ b/private/database/queries/create/CreateBlogArticlesTable.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS "BlogArticles" ( + "id" BIGSERIAL NOT NULL PRIMARY KEY, + "handle" TEXT NOT NULL UNIQUE, + "title" TEXT NOT NULL, + "image" TEXT NOT NULL, + "shortDescription" TEXT NULL, + "description" TEXT NOT NULL, + "date" TIMESTAMP NOT NULL +); diff --git a/private/database/queries/create/CreateFormatsTable.sql b/private/database/queries/create/CreateFormatsTable.sql deleted file mode 100644 index b20b63a..0000000 --- a/private/database/queries/create/CreateFormatsTable.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE IF NOT EXISTS "Formats" ( - "id" BIGSERIAL NOT NULL PRIMARY KEY, - "name" TEXT NOT NULL, - "gameId" BIGSERIAL NOT NULL -); diff --git a/private/database/queries/create/CreateGamesTable.sql b/private/database/queries/create/CreateGamesTable.sql deleted file mode 100644 index c7a7d1b..0000000 --- a/private/database/queries/create/CreateGamesTable.sql +++ /dev/null @@ -1,4 +0,0 @@ -CREATE TABLE IF NOT EXISTS "Games" ( - "id" BIGSERIAL NOT NULL PRIMARY KEY, - "name" TEXT NOT NULL UNIQUE -); diff --git a/private/database/queries/create/CreateSeasonsTable.sql b/private/database/queries/create/CreateSeasonsTable.sql deleted file mode 100644 index f145a2c..0000000 --- a/private/database/queries/create/CreateSeasonsTable.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS "Seasons" ( - "id" BIGSERIAL NOT NULL PRIMARY KEY, - "name" TEXT NOT NULL, - "formatId" BIGSERIAL NOT NULL, - "startDate" TIMESTAMP NOT NULL, - "endDate" TIMESTAMP NOT NULL -); diff --git a/private/database/queries/create/CreateTeamUsersSeasonsTable.sql b/private/database/queries/create/CreateTeamUsersSeasonsTable.sql deleted file mode 100644 index d4e020f..0000000 --- a/private/database/queries/create/CreateTeamUsersSeasonsTable.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS "TeamUsersSeasons" ( - "teamId" BIGSERIAL NOT NULL, - "userId" BIGSERIAL NOT NULL, - "seasonId" BIGSERIAL NOT NULL, - "registered" TIMESTAMP NOT NULL, - PRIMARY KEY("teamId", "userId", "seasonId") -); diff --git a/private/database/queries/create/CreateTeamsTable.sql b/private/database/queries/create/CreateTeamsTable.sql deleted file mode 100644 index de63a4a..0000000 --- a/private/database/queries/create/CreateTeamsTable.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS "Teams" ( - "id" BIGSERIAL NOT NULL PRIMARY KEY, - "name" varchar(32) NOT NULL UNIQUE, - "motto" text NULL, - "image" text NULL, - "registered" TIMESTAMP NOT NULL -); diff --git a/private/database/queries/create/CreateUsersTable.sql b/private/database/queries/create/CreateUsersTable.sql deleted file mode 100644 index 7d950b2..0000000 --- a/private/database/queries/create/CreateUsersTable.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE IF NOT EXISTS "Users" ( - "id" BIGSERIAL NOT NULL PRIMARY KEY, - "discordId" text NULL UNIQUE, - "steamId" text NULL UNIQUE, - "email" text NULL UNIQUE -); diff --git a/private/database/queries/format/AddFormat.sql b/private/database/queries/format/AddFormat.sql deleted file mode 100644 index 56a4b0b..0000000 --- a/private/database/queries/format/AddFormat.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO "Formats" ("name", "gameId") VALUES (${name}, ${gameId}) RETURNING *; diff --git a/private/database/queries/format/GetFormatsByGame.sql b/private/database/queries/format/GetFormatsByGame.sql deleted file mode 100644 index a7a8f52..0000000 --- a/private/database/queries/format/GetFormatsByGame.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Formats" WHERE "gameId"=${gameId}; diff --git a/private/database/queries/game/AddGame.sql b/private/database/queries/game/AddGame.sql deleted file mode 100644 index 942d60d..0000000 --- a/private/database/queries/game/AddGame.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO "Games" (name) VALUES (${name}) RETURNING *; diff --git a/private/database/queries/game/GetGameByName.sql b/private/database/queries/game/GetGameByName.sql deleted file mode 100644 index 0ac18b1..0000000 --- a/private/database/queries/game/GetGameByName.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Games" WHERE LOWER("name") = LOWER(${name}) LIMIT 1; diff --git a/private/database/queries/season/GetSeasonById.sql b/private/database/queries/season/GetSeasonById.sql deleted file mode 100644 index be98c37..0000000 --- a/private/database/queries/season/GetSeasonById.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Season" WHERE id = ${id} LIMIT 1; diff --git a/private/database/queries/season/GetSeasonForDate.sql b/private/database/queries/season/GetSeasonForDate.sql deleted file mode 100644 index 9da30a5..0000000 --- a/private/database/queries/season/GetSeasonForDate.sql +++ /dev/null @@ -1,5 +0,0 @@ -SELECT * FROM "Seasons" -WHERE - "startDate" <= ${date} AND - "endDate" >= ${date} -LIMIT 1; diff --git a/private/database/queries/team/AddTeam.sql b/private/database/queries/team/AddTeam.sql deleted file mode 100644 index 752b91c..0000000 --- a/private/database/queries/team/AddTeam.sql +++ /dev/null @@ -1,5 +0,0 @@ -INSERT INTO "Teams" ( - "name", "motto", "image", "registered" -) VALUES ( - ${name}, ${motto}, ${image}, ${registered} -) RETURNING *; diff --git a/private/database/queries/team/AddTeamUserSeason.sql b/private/database/queries/team/AddTeamUserSeason.sql deleted file mode 100644 index 4e57fe6..0000000 --- a/private/database/queries/team/AddTeamUserSeason.sql +++ /dev/null @@ -1,5 +0,0 @@ -INSERT INTO "TeamUsersSeasons" ( - "teamId", "userId", "seasonId", "registered" -) VALUES ( - ${teamId}, ${userId}, ${seasonId}, ${registered} -) RETURNING *; diff --git a/private/database/queries/team/GetTeamById.sql b/private/database/queries/team/GetTeamById.sql deleted file mode 100644 index ce14847..0000000 --- a/private/database/queries/team/GetTeamById.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Teams" WHERE "id"=${id} LIMIT 1; diff --git a/private/database/queries/team/GetTeamByName.sql b/private/database/queries/team/GetTeamByName.sql deleted file mode 100644 index d8087ea..0000000 --- a/private/database/queries/team/GetTeamByName.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Teams" WHERE LOWER("name") = LOWER(${name}) LIMIT 1; diff --git a/private/database/queries/team/GetTeamBySeasonAndUser.sql b/private/database/queries/team/GetTeamBySeasonAndUser.sql deleted file mode 100644 index 73fdce5..0000000 --- a/private/database/queries/team/GetTeamBySeasonAndUser.sql +++ /dev/null @@ -1,10 +0,0 @@ -SELECT - * -FROM - "Teams" -INNER JOIN - "TeamUsersSeasons" ON "TeamUsersSeasons"."teamId" = "Teams"."id" -WHERE - "TeamUsersSeasons"."userId" = ${userId} AND - "TeamUsersSeasons"."seasonId" = ${seasonId} -LIMIT 1; diff --git a/private/database/queries/team/GetTeamUsersBySeason.sql b/private/database/queries/team/GetTeamUsersBySeason.sql deleted file mode 100644 index 0aebb44..0000000 --- a/private/database/queries/team/GetTeamUsersBySeason.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "TeamUsersSeasons" WHERE "teamId" = ${teamId}; diff --git a/private/database/queries/team/GetTeamsBySeason.sql b/private/database/queries/team/GetTeamsBySeason.sql deleted file mode 100644 index 8043e6c..0000000 --- a/private/database/queries/team/GetTeamsBySeason.sql +++ /dev/null @@ -1,11 +0,0 @@ -SELECT - "Teams".* -FROM - "Teams" -INNER JOIN - "TeamUsersSeasons" ON "TeamUsersSeasons"."teamId" = "Teams"."id" -WHERE - "TeamUsersSeasons"."seasonId" = ${seasonId} -GROUP BY - "Teams"."id" -; diff --git a/private/database/queries/user/AddUser.sql b/private/database/queries/user/AddUser.sql deleted file mode 100644 index 6e3041a..0000000 --- a/private/database/queries/user/AddUser.sql +++ /dev/null @@ -1,9 +0,0 @@ -INSERT INTO "Users" ( - "discordId", - "steamId", - "email" -) VALUES ( - ${discordId}, - ${steamId}, - ${email} -) RETURNING *; diff --git a/private/database/queries/user/GetUserByDiscordId.sql b/private/database/queries/user/GetUserByDiscordId.sql deleted file mode 100644 index c704ad2..0000000 --- a/private/database/queries/user/GetUserByDiscordId.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Users" WHERE "discordId" = ${discordId} LIMIT 1; diff --git a/private/database/queries/user/GetUserById.sql b/private/database/queries/user/GetUserById.sql deleted file mode 100644 index df0afa1..0000000 --- a/private/database/queries/user/GetUserById.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM "Users" WHERE "id"=${id} LIMIT 1; diff --git a/private/server/api/methods/blog/GetBlogArticles.js b/private/server/api/methods/blog/GetBlogArticles.js new file mode 100644 index 0000000..947c0df --- /dev/null +++ b/private/server/api/methods/blog/GetBlogArticles.js @@ -0,0 +1,50 @@ +// Copyright (c) 2018 Dominic Masters +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +const + APIHandler = require('./../../APIHandler'), + sanitizeHtml = require('sanitize-html') +; + +const ERRORS = { + tooMany: "Cannot return this many" +}; + +module.exports = class GetBlogArticles extends APIHandler { + constructor(api) { + super(api, ['GET'], '/blog'); + } + + async handle(request) { + let page, perPage; + if(request.hasInteger("page")) page = request.getInteger("page"); + if(request.hasInteger("perPage")) perPage = request.getInteger("perPage"); + + perPage = Math.min(Math.max(perPage, 0), 20); + + return { + ok: true, + data: await request.getApp().getArticles().getArticlesByPage(page, perPage) + }; + } +}