mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-10 16:05:32 +02:00
Add docs
This commit is contained in:
parent
23f06eae76
commit
f3cff0973b
4 changed files with 201 additions and 2 deletions
|
@ -1,10 +1,27 @@
|
||||||
const model = require('../models/page');
|
const model = require('../models/page');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class Pages
|
||||||
|
* @classdesc Pages controller
|
||||||
|
*/
|
||||||
class Pages {
|
class Pages {
|
||||||
|
/**
|
||||||
|
* @static
|
||||||
|
* Fields required for page model creation
|
||||||
|
*
|
||||||
|
* @returns {['title', 'body']}
|
||||||
|
*/
|
||||||
static get REQUIRED_FIELDS () {
|
static get REQUIRED_FIELDS () {
|
||||||
return ['title', 'body'];
|
return ['title', 'body'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @static
|
||||||
|
* Find and return page model with passed id
|
||||||
|
*
|
||||||
|
* @param {string} id - page id
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
static async get (id) {
|
static async get (id) {
|
||||||
const page = await model.get(id);
|
const page = await model.get(id);
|
||||||
|
|
||||||
|
@ -15,10 +32,21 @@ class Pages {
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all pages
|
||||||
|
*
|
||||||
|
* @returns {Promise<Page[]>}
|
||||||
|
*/
|
||||||
static async getAll() {
|
static async getAll() {
|
||||||
return await model.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) {
|
static async insert (data) {
|
||||||
if (!Pages.validate(data)) {
|
if (!Pages.validate(data)) {
|
||||||
throw new Error('Invalid request format')
|
throw new Error('Invalid request format')
|
||||||
|
@ -29,10 +57,23 @@ class Pages {
|
||||||
return page.save();
|
return page.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check PageData object for required fields
|
||||||
|
*
|
||||||
|
* @param {PageData} data
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
static validate (data) {
|
static validate (data) {
|
||||||
return Pages.REQUIRED_FIELDS.every(field => typeof data[field] !== 'undefined');
|
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) {
|
static async update (id, data) {
|
||||||
const page = await model.get(id);
|
const page = await model.get(id);
|
||||||
|
|
||||||
|
@ -45,6 +86,12 @@ class Pages {
|
||||||
return page.save()
|
return page.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove page with given id from the database
|
||||||
|
*
|
||||||
|
* @param {string} id - page id
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
static async remove (id) {
|
static async remove (id) {
|
||||||
const page = await model.get(id);
|
const page = await model.get(id);
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,29 @@
|
||||||
const pages = require('./pages');
|
const pages = require('./pages');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class Database
|
||||||
|
* @classdesc Simple decorator class to work with nedb datastore
|
||||||
|
*
|
||||||
|
* @property db - nedb Datastore object
|
||||||
|
*/
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
*
|
||||||
|
* @param {Object} nedbInstance - nedb Datastore object
|
||||||
|
*/
|
||||||
constructor (nedbInstance) {
|
constructor (nedbInstance) {
|
||||||
this.db = 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) {
|
async insert (doc) {
|
||||||
return new Promise((res, rej) => this.db.insert(doc, (err, newDoc) => {
|
return new Promise((res, rej) => this.db.insert(doc, (err, newDoc) => {
|
||||||
if (err) {
|
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) {
|
async find (query, projection) {
|
||||||
const cbk = (res, rej) => (err, docs) => {
|
const cbk = (res, rej) => (err, docs) => {
|
||||||
if (err) {
|
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) {
|
async findOne (query, projection) {
|
||||||
const cbk = (res, rej) => (err, doc) => {
|
const cbk = (res, rej) => (err, doc) => {
|
||||||
if (err) {
|
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 = {}) {
|
async update (query, update, options = {}) {
|
||||||
return new Promise((res, rej) => this.db.update(query, update, options, (err, result) => {
|
return new Promise((res, rej) => this.db.update(query, update, options, (err, result) => {
|
||||||
if (err) {
|
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 = {}) {
|
async remove (query, options = {}) {
|
||||||
return new Promise((res, rej) => this.db.remove(query, options, (err, result) => {
|
return new Promise((res, rej) => this.db.remove(query, options, (err, result) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -1,19 +1,53 @@
|
||||||
const {pages} = require('../database');
|
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 {
|
class Page {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find and return model of page with given id
|
||||||
|
* @param {string} _id - page id
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
static async get(_id) {
|
static async get(_id) {
|
||||||
const data = await pages.findOne({_id});
|
const data = await pages.findOne({_id});
|
||||||
|
|
||||||
return new Page(data);
|
return new Page(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all pages which match passed query object
|
||||||
|
*
|
||||||
|
* @param {Object} query
|
||||||
|
* @returns {Promise<Page[]>}
|
||||||
|
*/
|
||||||
static async getAll(query = {}) {
|
static async getAll(query = {}) {
|
||||||
const docs = await pages.find(query);
|
const docs = await pages.find(query);
|
||||||
|
|
||||||
return Promise.all(docs.map(doc => new Page(doc)));
|
return Promise.all(docs.map(doc => new Page(doc)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
*
|
||||||
|
* @param {PageData} data
|
||||||
|
*/
|
||||||
constructor (data = {}) {
|
constructor (data = {}) {
|
||||||
if (data === null) {
|
if (data === null) {
|
||||||
data = {};
|
data = {};
|
||||||
|
@ -28,6 +62,11 @@ class Page {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set PageData object fields to internal model fields
|
||||||
|
*
|
||||||
|
* @param {PageData} pageData
|
||||||
|
*/
|
||||||
set data (pageData) {
|
set data (pageData) {
|
||||||
const {title, body, parent} = pageData;
|
const {title, body, parent} = pageData;
|
||||||
|
|
||||||
|
@ -36,6 +75,11 @@ class Page {
|
||||||
this._parent = parent || this._parent;
|
this._parent = parent || this._parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return PageData object
|
||||||
|
*
|
||||||
|
* @returns {PageData}
|
||||||
|
*/
|
||||||
get data () {
|
get data () {
|
||||||
return {
|
return {
|
||||||
_id: this._id,
|
_id: this._id,
|
||||||
|
@ -45,20 +89,40 @@ class Page {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Link given page as parent
|
||||||
|
*
|
||||||
|
* @param {Page} parentPage
|
||||||
|
*/
|
||||||
set parent (parentPage) {
|
set parent (parentPage) {
|
||||||
this._parent = parentPage._id;
|
this._parent = parentPage._id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return parent page model
|
||||||
|
*
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
get parent () {
|
get parent () {
|
||||||
return this.db.findOne({_id: this._parent})
|
return this.db.findOne({_id: this._parent})
|
||||||
.then(data => new Page(data));
|
.then(data => new Page(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return child pages models
|
||||||
|
*
|
||||||
|
* @returns {Promise<Page[]>}
|
||||||
|
*/
|
||||||
get children () {
|
get children () {
|
||||||
return this.db.find({parent: this._id})
|
return this.db.find({parent: this._id})
|
||||||
.then(data => data.map(page => new Page(page)));
|
.then(data => data.map(page => new Page(page)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save or update page data in the database
|
||||||
|
*
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
async save () {
|
async save () {
|
||||||
if (!this._id) {
|
if (!this._id) {
|
||||||
const insertedRow = await this.db.insert(this.data);
|
const insertedRow = await this.db.insert(this.data);
|
||||||
|
@ -71,6 +135,11 @@ class Page {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove page data from the database
|
||||||
|
*
|
||||||
|
* @returns {Promise<Page>}
|
||||||
|
*/
|
||||||
async destroy () {
|
async destroy () {
|
||||||
await this.db.remove({_id: this._id});
|
await this.db.remove({_id: this._id});
|
||||||
|
|
||||||
|
@ -79,6 +148,11 @@ class Page {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return readable page data
|
||||||
|
*
|
||||||
|
* @returns {PageData}
|
||||||
|
*/
|
||||||
toJSON () {
|
toJSON () {
|
||||||
return this.data;
|
return this.data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,11 @@ const router = express.Router();
|
||||||
const multer = require('multer')();
|
const multer = require('multer')();
|
||||||
const Pages = require('../controllers/pages');
|
const Pages = require('../controllers/pages');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /page/:id
|
||||||
|
*
|
||||||
|
* Return PageData of page with given id
|
||||||
|
*/
|
||||||
router.get('/page/:id', async (req, res) => {
|
router.get('/page/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const page = await Pages.get(req.params.id);
|
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) => {
|
router.get('/pages', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const pages = await Pages.getAll();
|
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,) => {
|
router.put('/page', multer.any(), async (req, res,) => {
|
||||||
try {
|
try {
|
||||||
const page = await Pages.insert(req.body);
|
const {title, body, parent} = req.body
|
||||||
|
const page = await Pages.insert({title, body, parent});
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
result: page
|
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) => {
|
router.post('/page/:id', multer.any(), async (req, res) => {
|
||||||
const {id} = req.params;
|
const {id} = req.params;
|
||||||
|
|
||||||
try {
|
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({
|
res.json({
|
||||||
success: true,
|
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) => {
|
router.delete('/page/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const page = await Pages.remove(req.params.id);
|
const page = await Pages.remove(req.params.id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue