mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-20 13:49:41 +02:00
Frontent build system is ready (#3)
* Frontent build system is ready * Set up linter for frontend part * move code to /src * update db * upd test * remove db * ignore .db
This commit is contained in:
parent
3762ea3791
commit
248558a11f
27 changed files with 3003 additions and 219 deletions
|
@ -1,106 +0,0 @@
|
|||
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);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all pages
|
||||
*
|
||||
* @returns {Promise<Page[]>}
|
||||
*/
|
||||
static async getAll() {
|
||||
return 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');
|
||||
}
|
||||
|
||||
const page = new Model(data);
|
||||
|
||||
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);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
page.data = data;
|
||||
|
||||
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);
|
||||
|
||||
if (!page._id) {
|
||||
throw new Error('Page with given id does not exist');
|
||||
}
|
||||
|
||||
return page.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Pages;
|
Loading…
Add table
Add a link
Reference in a new issue