Built basic blog backend.
This commit is contained in:
		| @@ -27,24 +27,28 @@ const | |||||||
|   DatabaseConnection = require('./../database/DatabaseConnection'), |   DatabaseConnection = require('./../database/DatabaseConnection'), | ||||||
|   Server = require('./../server/Server'), |   Server = require('./../server/Server'), | ||||||
|   Email = require('./../email/Email'), |   Email = require('./../email/Email'), | ||||||
|   CacheStore = require('./../cache/CacheStore') |   CacheStore = require('./../cache/CacheStore'), | ||||||
|  |  | ||||||
|  |   Articles = require('./../blog/Articles') | ||||||
| ; | ; | ||||||
|  |  | ||||||
| class App { | class App { | ||||||
|   constructor() { |   constructor() { | ||||||
|     this.config = new Configuration(this); |     this.config = new Configuration(this); | ||||||
|     this.database = new DatabaseConnection(this); |     this.database = new DatabaseConnection(this); | ||||||
|  |  | ||||||
|  |     this.articles = new Articles(this); | ||||||
|  |  | ||||||
|     this.server = new Server(this); |     this.server = new Server(this); | ||||||
|     this.email = new Email(this); |     this.email = new Email(this); | ||||||
|     //this.store = new CacheStore(this); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   getConfig() { return this.config; } |   getConfig() { return this.config; } | ||||||
|   //getCacheStore() {return this.store;} |  | ||||||
|   getDiscord() { return this.discord; } |  | ||||||
|   getDatabase() { return this.database; } |   getDatabase() { return this.database; } | ||||||
|   getPalaise() { return this.palaise; } |  | ||||||
|   getEmail() {return this.email;} |   getEmail() {return this.email;} | ||||||
|  |   getServer() {return this.server;} | ||||||
|  |  | ||||||
|  |   getArticles() {return this.articles;} | ||||||
|  |  | ||||||
|   async init() { |   async init() { | ||||||
|     this.log('Starting App...'); |     this.log('Starting App...'); | ||||||
| @@ -90,6 +94,28 @@ class App { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     this.log('App ready'); |     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 // |   // Logging Functions // | ||||||
|   | |||||||
							
								
								
									
										70
									
								
								private/blog/Articles.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								private/blog/Articles.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										6
									
								
								private/cache/CacheStore.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								private/cache/CacheStore.js
									
									
									
									
										vendored
									
									
								
							| @@ -54,13 +54,17 @@ class CacheStore { | |||||||
|     this.store.del(keys); |     this.store.del(keys); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   flush() { | ||||||
|  |     this.store.flushAll(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   //Database related stores |   //Database related stores | ||||||
|   async getFromDatabase(key, query, params, method) { |   async getFromDatabase(key, query, params, method) { | ||||||
|     if(typeof params === typeof undefined) params = {}; |     if(typeof params === typeof undefined) params = {}; | ||||||
|     if(typeof method === typeof undefined) method = "any"; |     if(typeof method === typeof undefined) method = "any"; | ||||||
|  |  | ||||||
|     return await this.get(key, async () => { |     return await this.get(key, async () => { | ||||||
|       return await this.database[method](query, params); |       return await this.getDatabase()[method](query, params); | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -68,12 +68,13 @@ class DatabaseConnection { | |||||||
|     let keys = Object.keys(queries); |     let keys = Object.keys(queries); | ||||||
|     for(let i = 0; i < keys.length; i++) { |     for(let i = 0; i < keys.length; i++) { | ||||||
|       let k = keys[i]; |       let k = keys[i]; | ||||||
|       if(!k.startsWith("Create")) return; |       if(!k.toLowerCase().startsWith("create")) continue; | ||||||
|       await this.none(k); |       await this.none(k); | ||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   getQuery(name) { |   getQuery(name) { | ||||||
|  |     if(!this.queries[name]) throw new Error("No Query by that name exists"); | ||||||
|     return this.queries[name]; |     return this.queries[name]; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								private/database/DatabaseInterface.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								private/database/DatabaseInterface.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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;} | ||||||
|  | } | ||||||
							
								
								
									
										5
									
								
								private/database/queries/blog/addArticle.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								private/database/queries/blog/addArticle.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | INSERT INTO "BlogArticles" ( | ||||||
|  |   "handle", "image", "shortDescription", "description", "date" | ||||||
|  | ) VALUES ( | ||||||
|  |   ${handle}, ${image}, ${shortDescription}, ${description}, ${date} | ||||||
|  | ) RETURNING *; | ||||||
							
								
								
									
										1
									
								
								private/database/queries/blog/getArticleByHandle.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								private/database/queries/blog/getArticleByHandle.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | SELECT * FROM "BlogArticles" WHERE "handle"=${handle}; | ||||||
							
								
								
									
										1
									
								
								private/database/queries/blog/getArticleById.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								private/database/queries/blog/getArticleById.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | SELECT * FROM "BlogArticles" WHERE "id" = ${id}; | ||||||
							
								
								
									
										11
									
								
								private/database/queries/blog/getArticlesByPage.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								private/database/queries/blog/getArticlesByPage.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | SELECT | ||||||
|  |   * | ||||||
|  | FROM | ||||||
|  |   "BlogArticles" | ||||||
|  | ORDER BY | ||||||
|  |   "date" DESC | ||||||
|  | LIMIT | ||||||
|  |   ${count} | ||||||
|  | OFFSET | ||||||
|  |   ${offset} | ||||||
|  | ; | ||||||
							
								
								
									
										1
									
								
								private/database/queries/blog/getArticlesCount.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								private/database/queries/blog/getArticlesCount.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | SELECT COUNT(*) FROM "BlogArticles"; | ||||||
| @@ -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 | ||||||
|  | ); | ||||||
| @@ -1,5 +0,0 @@ | |||||||
| CREATE TABLE IF NOT EXISTS "Formats" ( |  | ||||||
|   "id" BIGSERIAL NOT NULL PRIMARY KEY, |  | ||||||
|   "name" TEXT NOT NULL, |  | ||||||
|   "gameId" BIGSERIAL NOT NULL |  | ||||||
| ); |  | ||||||
| @@ -1,4 +0,0 @@ | |||||||
| CREATE TABLE IF NOT EXISTS "Games" ( |  | ||||||
|   "id" BIGSERIAL NOT NULL PRIMARY KEY, |  | ||||||
|   "name" TEXT NOT NULL UNIQUE |  | ||||||
| ); |  | ||||||
| @@ -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 |  | ||||||
| ); |  | ||||||
| @@ -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") |  | ||||||
| ); |  | ||||||
| @@ -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 |  | ||||||
| ); |  | ||||||
| @@ -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 |  | ||||||
| ); |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| INSERT INTO "Formats" ("name", "gameId") VALUES (${name}, ${gameId}) RETURNING *; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Formats" WHERE "gameId"=${gameId}; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| INSERT INTO "Games" (name) VALUES (${name}) RETURNING *; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Games" WHERE LOWER("name") = LOWER(${name}) LIMIT 1; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Season" WHERE id = ${id} LIMIT 1; |  | ||||||
| @@ -1,5 +0,0 @@ | |||||||
| SELECT * FROM "Seasons" |  | ||||||
| WHERE |  | ||||||
|   "startDate" <= ${date} AND |  | ||||||
|   "endDate" >= ${date} |  | ||||||
| LIMIT 1; |  | ||||||
| @@ -1,5 +0,0 @@ | |||||||
| INSERT INTO "Teams" ( |  | ||||||
|   "name", "motto", "image", "registered" |  | ||||||
| ) VALUES ( |  | ||||||
|   ${name}, ${motto}, ${image}, ${registered} |  | ||||||
| ) RETURNING *; |  | ||||||
| @@ -1,5 +0,0 @@ | |||||||
| INSERT INTO "TeamUsersSeasons" ( |  | ||||||
|   "teamId", "userId", "seasonId", "registered" |  | ||||||
| ) VALUES ( |  | ||||||
|   ${teamId}, ${userId}, ${seasonId}, ${registered} |  | ||||||
| ) RETURNING *; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Teams" WHERE "id"=${id} LIMIT 1; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Teams" WHERE LOWER("name") = LOWER(${name}) LIMIT 1; |  | ||||||
| @@ -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; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "TeamUsersSeasons" WHERE "teamId" = ${teamId}; |  | ||||||
| @@ -1,11 +0,0 @@ | |||||||
| SELECT |  | ||||||
|   "Teams".* |  | ||||||
| FROM |  | ||||||
|   "Teams" |  | ||||||
| INNER JOIN |  | ||||||
|   "TeamUsersSeasons" ON "TeamUsersSeasons"."teamId" = "Teams"."id" |  | ||||||
| WHERE |  | ||||||
|   "TeamUsersSeasons"."seasonId" = ${seasonId} |  | ||||||
| GROUP BY |  | ||||||
|   "Teams"."id" |  | ||||||
| ; |  | ||||||
| @@ -1,9 +0,0 @@ | |||||||
| INSERT INTO "Users" ( |  | ||||||
|   "discordId", |  | ||||||
|   "steamId", |  | ||||||
|   "email" |  | ||||||
| ) VALUES ( |  | ||||||
|   ${discordId}, |  | ||||||
|   ${steamId}, |  | ||||||
|   ${email} |  | ||||||
| ) RETURNING *; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Users" WHERE "discordId" = ${discordId} LIMIT 1; |  | ||||||
| @@ -1 +0,0 @@ | |||||||
| SELECT * FROM "Users" WHERE "id"=${id} LIMIT 1; |  | ||||||
							
								
								
									
										50
									
								
								private/server/api/methods/blog/GetBlogArticles.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								private/server/api/methods/blog/GetBlogArticles.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||||
|  |     }; | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user