1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-09 15:35:25 +02:00
This commit is contained in:
gohabereg@gmail.com 2018-08-15 20:41:51 +03:00
parent 23f06eae76
commit f3cff0973b
4 changed files with 201 additions and 2 deletions

View file

@ -1,10 +1,27 @@
const model = require('../models/page');
/**
* @class Pages
* @classdesc Pages controller
*/
class Pages {
/**
* @static
* Fields required for page model creation
*
* @returns {['title', 'body']}
*/
static get REQUIRED_FIELDS () {
return ['title', 'body'];
}
/**
* @static
* Find and return page model with passed id
*
* @param {string} id - page id
* @returns {Promise<Page>}
*/
static async get (id) {
const page = await model.get(id);
@ -15,10 +32,21 @@ class Pages {
return page;
}
/**
* Return all pages
*
* @returns {Promise<Page[]>}
*/
static async getAll() {
return await model.getAll();
}
/**
* Create new page model and save it in the database
*
* @param {PageData} data
* @returns {Promise<Page>}
*/
static async insert (data) {
if (!Pages.validate(data)) {
throw new Error('Invalid request format')
@ -29,10 +57,23 @@ class Pages {
return page.save();
}
/**
* Check PageData object for required fields
*
* @param {PageData} data
* @returns {boolean}
*/
static validate (data) {
return Pages.REQUIRED_FIELDS.every(field => typeof data[field] !== 'undefined');
}
/**
* Update page with given id in the database
*
* @param {string} id - page id
* @param {PageData} data
* @returns {Promise<Page>}
*/
static async update (id, data) {
const page = await model.get(id);
@ -45,6 +86,12 @@ class Pages {
return page.save()
}
/**
* Remove page with given id from the database
*
* @param {string} id - page id
* @returns {Promise<Page>}
*/
static async remove (id) {
const page = await model.get(id);

View file

@ -1,11 +1,29 @@
const pages = require('./pages');
/**
* @class Database
* @classdesc Simple decorator class to work with nedb datastore
*
* @property db - nedb Datastore object
*/
class Database {
/**
* @constructor
*
* @param {Object} nedbInstance - nedb Datastore object
*/
constructor (nedbInstance) {
this.db = nedbInstance;
}
/**
* Insert new document into the database
* @see https://github.com/louischatriot/nedb#inserting-documents
*
* @param {Object} doc - object to insert
* @returns {Promise<Object|Error>} - inserted doc or Error object
*/
async insert (doc) {
return new Promise((res, rej) => this.db.insert(doc, (err, newDoc) => {
if (err) {
@ -16,6 +34,14 @@ class Database {
}));
}
/**
* Find documents that match passed query
* @see https://github.com/louischatriot/nedb#finding-documents
*
* @param {Object} query - query object
* @param {Object} projection - projection object
* @returns {Promise<Array<Object>|Error>} - found docs or Error object
*/
async find (query, projection) {
const cbk = (res, rej) => (err, docs) => {
if (err) {
@ -34,6 +60,14 @@ class Database {
});
}
/**
* Find one document matches passed query
* @see https://github.com/louischatriot/nedb#finding-documents
*
* @param {Object} query - query object
* @param {Object} projection - projection object
* @returns {Promise<Object|Error>} - found doc or Error object
*/
async findOne (query, projection) {
const cbk = (res, rej) => (err, doc) => {
if (err) {
@ -52,6 +86,15 @@ class Database {
});
}
/**
* Update document matches query
* @see https://github.com/louischatriot/nedb#updating-documents
*
* @param {Object} query - query object
* @param {Object} update - fields to update
* @param {Object} options
* @returns {Promise<number|Error>} - number of updated rows or Error object
*/
async update (query, update, options = {}) {
return new Promise((res, rej) => this.db.update(query, update, options, (err, result) => {
if (err) {
@ -62,6 +105,14 @@ class Database {
}));
}
/**
* Remove document matches passed query
* @see https://github.com/louischatriot/nedb#removing-documents
*
* @param {Object} query - query object
* @param {Object} options
* @returns {Promise<number|Error>} - number of removed rows or Error object
*/
async remove (query, options = {}) {
return new Promise((res, rej) => this.db.remove(query, options, (err, result) => {
if (err) {

View file

@ -1,19 +1,53 @@
const {pages} = require('../database');
/**
* @typedef {Object} PageData
* @property {string} _id - page id
* @property {string} title - page title
* @property {*} body - page body
* @property {string} parent - id of parent page
*
*/
/**
* @class Page
* @class Page model
*
* @property {string} _id - page id
* @property {string} title - page title
* @property {*} body - page body
* @property {string} _parent - id of parent page
*/
class Page {
/**
* Find and return model of page with given id
* @param {string} _id - page id
* @returns {Promise<Page>}
*/
static async get(_id) {
const data = await pages.findOne({_id});
return new Page(data);
}
/**
* Find all pages which match passed query object
*
* @param {Object} query
* @returns {Promise<Page[]>}
*/
static async getAll(query = {}) {
const docs = await pages.find(query);
return Promise.all(docs.map(doc => new Page(doc)));
}
/**
* @constructor
*
* @param {PageData} data
*/
constructor (data = {}) {
if (data === null) {
data = {};
@ -28,6 +62,11 @@ class Page {
this.data = data;
}
/**
* Set PageData object fields to internal model fields
*
* @param {PageData} pageData
*/
set data (pageData) {
const {title, body, parent} = pageData;
@ -36,6 +75,11 @@ class Page {
this._parent = parent || this._parent;
}
/**
* Return PageData object
*
* @returns {PageData}
*/
get data () {
return {
_id: this._id,
@ -45,20 +89,40 @@ class Page {
}
}
/**
* Link given page as parent
*
* @param {Page} parentPage
*/
set parent (parentPage) {
this._parent = parentPage._id;
}
/**
* Return parent page model
*
* @returns {Promise<Page>}
*/
get parent () {
return this.db.findOne({_id: this._parent})
.then(data => new Page(data));
}
/**
* Return child pages models
*
* @returns {Promise<Page[]>}
*/
get children () {
return this.db.find({parent: this._id})
.then(data => data.map(page => new Page(page)));
}
/**
* Save or update page data in the database
*
* @returns {Promise<Page>}
*/
async save () {
if (!this._id) {
const insertedRow = await this.db.insert(this.data);
@ -71,6 +135,11 @@ class Page {
return this;
}
/**
* Remove page data from the database
*
* @returns {Promise<Page>}
*/
async destroy () {
await this.db.remove({_id: this._id});
@ -79,6 +148,11 @@ class Page {
return this;
}
/**
* Return readable page data
*
* @returns {PageData}
*/
toJSON () {
return this.data;
}

View file

@ -3,6 +3,11 @@ const router = express.Router();
const multer = require('multer')();
const Pages = require('../controllers/pages');
/**
* GET /page/:id
*
* Return PageData of page with given id
*/
router.get('/page/:id', async (req, res) => {
try {
const page = await Pages.get(req.params.id);
@ -19,6 +24,11 @@ router.get('/page/:id', async (req, res) => {
}
});
/**
* GET /pages
*
* Return PageData for all pages
*/
router.get('/pages', async (req, res) => {
try {
const pages = await Pages.getAll();
@ -35,9 +45,15 @@ router.get('/pages', async (req, res) => {
}
});
/**
* PUT /page
*
* Create new page in the database
*/
router.put('/page', multer.any(), async (req, res,) => {
try {
const page = await Pages.insert(req.body);
const {title, body, parent} = req.body
const page = await Pages.insert({title, body, parent});
res.json({
success: true,
result: page
@ -51,11 +67,17 @@ router.put('/page', multer.any(), async (req, res,) => {
});
/**
* POST /page/:id
*
* Update page data in the database
*/
router.post('/page/:id', multer.any(), async (req, res) => {
const {id} = req.params;
try {
const page = await Pages.update(id, req.body);
const {title, body, parent} = req.body
const page = await Pages.update(id, {title, body, parent});
res.json({
success: true,
@ -69,6 +91,11 @@ router.post('/page/:id', multer.any(), async (req, res) => {
}
});
/**
* DELETE /page/:id
*
* Remove page from the database
*/
router.delete('/page/:id', async (req, res) => {
try {
const page = await Pages.remove(req.params.id);