From 7e152520c2301dbd8a721a79f471d38fdff9404c Mon Sep 17 00:00:00 2001 From: LordMathis Date: Thu, 26 Dec 2019 21:22:18 +0100 Subject: [PATCH] Restructure data holder --- src/server.js | 11 +++++- src/utils/dataHolder.js | 81 +++++--------------------------------- src/utils/scanner.js | 7 +--- src/utils/storage/file.js | 69 ++++++++++++++++++++++++++++++++ src/utils/storage/mongo.js | 35 ++++++++++++++++ 5 files changed, 124 insertions(+), 79 deletions(-) create mode 100644 src/utils/storage/file.js create mode 100644 src/utils/storage/mongo.js diff --git a/src/server.js b/src/server.js index 6abf151..0b051af 100644 --- a/src/server.js +++ b/src/server.js @@ -8,6 +8,8 @@ import jsonfile from 'jsonfile' import { ServerRenderer } from './utils/serverRender' import { Scanner } from './utils/scanner' import { DataHolder } from './utils/dataHolder' +import { FileStorage } from './utils/storage/file' +import { MongoStorage } from './utils/storage/mongo' const port = process.env.PORT || 3000 const app = express() @@ -45,7 +47,14 @@ if (head == null) { } } -const dataHolder = new DataHolder(config) +let storage +if (config.storage === 'file') { + storage = new FileStorage() +} else if (config.storage === 'mongo') { + storage = new MongoStorage() +} + +const dataHolder = new DataHolder(storage) const scanner = new Scanner(config, dataHolder) const serverRenderer = new ServerRenderer(head, config, dataHolder) diff --git a/src/utils/dataHolder.js b/src/utils/dataHolder.js index df226ed..86dc285 100644 --- a/src/utils/dataHolder.js +++ b/src/utils/dataHolder.js @@ -1,85 +1,22 @@ -import fs from 'fs' -import jsonfile from 'jsonfile' -import path from 'path' -import mongoose from 'mongoose' export class DataHolder { - constructor (config) { - this.config = config - - if (this.config.storage === 'file') { - this.data = { - posts: [], - other: {} - } - } else if (this.config.storage === 'mongo') { - this.Post = mongoose.model('Post', { - filename: String, - published: String, - title: String, - summary: String, - link: String, - body: String - }) - - this.Other = mongoose.model('Other', { - filename: String, - body: String - }) - } - } - - getData (reqPath) { - if (this.config.storage === 'file') { - return this.getDataFromFile(reqPath) - } - } - - getDataFromFile (reqPath) { - if (reqPath === '') { - return Promise.resolve(this.data) - } else if (reqPath === 'resume') { - const fileName = path.join(process.cwd(), 'content', reqPath + '.md') - return this.readFile(fileName, 'resume', 'utf8') - } else { - const fileName = path.join(process.cwd(), 'content', reqPath + '.md') - return this.readFile(fileName, 'post', 'utf8') - } - } - - readFile (fileName, type, options) { - return new Promise(function (resolve, reject) { - fs.readFile(fileName, options, (err, data) => { - err ? reject(err) : resolve({ - type: type, - data: data - }) - }) - }) - } - - readJson (dataPath) { - return new Promise(function (resolve, reject) { - jsonfile.readFile(dataPath, (err, data) => { - err ? reject(err) : resolve(data) - }) - }) + constructor (storage) { + this.storage = storage } addPost (post) { - if (this.config.storage === 'file') { - delete post.body - this.data.posts.push(post) - } + this.storage.addPost(post) } addOther (filename, data) { - this.data.other[filename] = data + this.storage.addOther(filename, data) } deleteFile (filepath) { - this.data.posts = this.data.posts.filter((post) => - post.filename !== filepath - ) + this.storage.deleteFile(filepath) + } + + getData (reqPath) { + return this.storage.getData(reqPath) } } diff --git a/src/utils/scanner.js b/src/utils/scanner.js index 9509a39..59433c5 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -24,7 +24,7 @@ export class Scanner { }) .on('change', filepath => { console.log(`[Scanner] File ${filepath} has been changed`) - this.updateFile(filepath) + this.addFile(filepath) }) .on('unlink', filepath => { console.log(`[Scanner] File ${filepath} has been removed`) @@ -42,11 +42,6 @@ export class Scanner { } } - updateFile (filepath) { - this.deleteFile(filepath) - this.addFile(filepath) - } - deleteFile (filepath) { this.dataHolder.deleteFile(filepath) } diff --git a/src/utils/storage/file.js b/src/utils/storage/file.js new file mode 100644 index 0000000..a8e9572 --- /dev/null +++ b/src/utils/storage/file.js @@ -0,0 +1,69 @@ +import fs from 'fs' +import path from 'path' + +export class FileStorage { + + constructor () { + this.data = { + posts: [], + other: {} + } + } + + addPost (post) { + delete post.body + const postIndex = this._findWithAttr(this.data.posts, 'filename', post.filename) + + if (postIndex === -1) { + this.data.posts.push(post) + } else { + this.data.posts[postIndex] = post + } + } + + addOther (filename, data) { + this.data.other[filename] = data + } + + deleteFile (filepath) { + this.data.posts = this.data.posts.filter((post) => + post.filename !== filepath + ) + } + + getData (reqPath) { + return this._getDataFromFile(reqPath) + } + + _getDataFromFile (reqPath) { + if (reqPath === '') { + return Promise.resolve(this.data) + } else if (reqPath === 'resume') { + const fileName = path.join(process.cwd(), 'content', reqPath + '.md') + return this._readFile(fileName, 'resume', 'utf8') + } else { + const fileName = path.join(process.cwd(), 'content', reqPath + '.md') + return this._readFile(fileName, 'post', 'utf8') + } + } + + _readFile (fileName, type, options) { + return new Promise(function (resolve, reject) { + fs.readFile(fileName, options, (err, data) => { + err ? reject(err) : resolve({ + type: type, + data: data + }) + }) + }) + } + + _findWithAttr (array, attr, value) { + for (let i = 0; i < array.length; i += 1) { + if (array[i][attr] === value) { + return i + } + } + return -1 + } +} diff --git a/src/utils/storage/mongo.js b/src/utils/storage/mongo.js new file mode 100644 index 0000000..1051b8e --- /dev/null +++ b/src/utils/storage/mongo.js @@ -0,0 +1,35 @@ +import mongoose from 'mongoose' + +export class MongoStorage { + constructor () { + this.Post = mongoose.model('Post', { + filename: String, + published: String, + title: String, + summary: String, + link: String, + body: String + }) + + this.Other = mongoose.model('Other', { + filename: String, + body: String + }) + } + + addPost (post) { + + } + + addOther (filename, data) { + + } + + deleteFile (filepath) { + + } + + getData (reqPath) { + + } +}